#import "ViewController.h"

@interface ViewController ()
@property(nonatomic,retain)PLSqliteDatabase *db;
@end @implementation ViewController #pragma mark -生命周期方法
-(void)dealloc{
self.db=nil;
[super dealloc];
} - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self OpenDb];
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} #pragma mark -事件
- (IBAction)createTable:(id)sender {
[self createTable];
} - (IBAction)ExtistTable:(id)sender {
} - (IBAction)Insert:(id)sender {
[self InsertData];
} - (IBAction)update:(id)sender {
[self UpdateData];
} - (IBAction)delete1:(id)sender {
[self deletedata];
}
- (IBAction)select1:(id)sender {
[self selectdata1];
} #pragma mark--数据库操作
-(void)OpenDb{
NSString *path= [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
path=[path stringByAppendingPathComponent:@"test.db"];
//open db
PLSqliteDatabase *sqldb=[[PLSqliteDatabase alloc] initWithPath:path];//没有数据库,会创建一个的 if(![sqldb open]){
NSLog(@"没有打开数据库"); }
self.db=sqldb; [sqldb release];
} -(void)createTable{
if (![self.db tableExists:@"TEST"]) {
if(![self.db executeUpdate:@"CREATE TABLE TEST (id integer primary key autoincrement,name text,age integer)"]){
NSLog(@"create table faild");
}
}
else{
NSLog(@"表已经存在");
} } -(void)InsertData{
NSString *sql=@"INSERT INTO TEST(name,age) values(?,?)"; if([self.db executeUpdate:sql,@"gcb",@]){//注意参数必须是oc对象
NSLog(@"insert success");
}
else{
NSLog(@"insert failed");
} } -(void)UpdateData{
NSString *sql=@"UPDATE TEST SET name=?,age=? where id=?";
if([self.db executeUpdate:sql,@"ios",@,@]){//注意参数必须是oc对象
NSLog(@"update success");
}
else{
NSLog(@"updata failed");
}
} -(void)selectdata{
NSString *sql=@"SELECT id,name,age FROM TEST WHERE id=?";
id<PLResultSet> result=[self.db executeQuery:sql,@];
while ([result next]) {
int pid=[result intForColumn:@"id"];
NSString *name=[result stringForColumn:@"name"];
int32_t age=[result intForColumn:@"age"];
NSLog(@"id=%zi,name=%@,age=%zi",pid,name,age);
}
} -(void)selectdata1{
// NSString *sql=@"SELECT id,name,age FROM TEST WHERE (id like '%%%@%%' OR age like '%%@%%' ";
NSString *sql=[NSString stringWithFormat:@"SELECT id,name,age FROM TEST WHERE (id like'%%%@%%' OR age like '%%%@%%')",@,@]; id<PLResultSet> result=[self.db executeQuery:sql];
while ([result next]) {
int pid=[result intForColumn:@"id"];
NSString *name=[result stringForColumn:@"name"];
int32_t age=[result intForColumn:@"age"];
NSLog(@"id=%zi,name=%@,age=%zi",pid,name,age);
}
} -(void)deletedata{
NSString *sql=@"DELETE FROM TEST WHERE id=?";
if([self.db executeUpdate:sql,@]){
NSLog(@"delete success");
}
else{
NSLog(@"delete failed");
}
}

插入的另一种方法(如果是要封装,可以考虑下面)

-(void)InsertData1{
NSString *sql=@"INSERT INTO TEST(name,age) values(:name,:age)"; id<PLPreparedStatement> stm=[self.db prepareStatement:sql];
NSMutableDictionary *dic=[NSMutableDictionary dictionary];
[dic setObject:@"tt" forKey:@"name"];
[dic setObject:@ forKey:@"age"];
[stm bindParameterDictionary:dic]; if([stm executeUpdate]){
NSLog(@"ss");
}
-(void) selectdate2{
NSString *sql=@"SELECT id,name,age FROM TEST WHERE id=:id";
id<PLPreparedStatement> stmp=[self.db prepareStatement:sql];
NSMutableDictionary *dic=[NSMutableDictionary dictionary];
[dic setObject:@ forKey:@"id"];
[stmp bindParameterDictionary:dic];
id<PLResultSet> result=[stmp executeQuery];
while ([result next]) {
int pid=[result intForColumn:@"id"];
NSString *name=[result stringForColumn:@"name"];
int age=[result intForColumn:@"age"];
NSLog(@"name=%@,age=%zi,id=%zi",name,age,pid);
}
}

ios中Pldatabase的用法(3)的更多相关文章

  1. ios中Pldatabase的用法

    将PLDATABASE加入到工程 下载PLDatabase 的dmg文件 将PLDatabase的framework复制到工程根目录在工程中加入该framework使用该framework进行数据库操 ...

  2. ios中Pldatabase的用法(4)

    封装成DAO@implementation SqlHelper +(BOOL)InsertSql:(NSString *)sql paramet:(NSMutableDictionary *)parm ...

  3. ios中Pldatabase的用法(2)

    @implementation AppGlobal static NSString* strHostName; static NSString* strVersion; static PLSqlite ...

  4. iOS中block的用法 以及和函数用法的区别

    ios中block的用法和函数的用法大致相同 但是block的用法的灵活性更高: 不带参数的block: void ^(MyBlock)() = ^{}; 调用的时候  MyBlock(); 带参数的 ...

  5. iOS中Block的用法,举例,解析与底层原理(这可能是最详细的Block解析)

    1. 前言 Block:带有自动变量(局部变量)的匿名函数.它是C语言的扩充功能.之所以是拓展,是因为C语言不允许存在这样匿名函数. 1.1 匿名函数 匿名函数是指不带函数名称函数.C语言中,函数是怎 ...

  6. ios中图片拉伸用法

    - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCap ...

  7. iOS中的CocoaPods用法及常用命令

     CocoaPods是什么? ***CocoaPods的使用场景:*** 1. 当你开发iOS应用时,会经常使用到很多第三方开源类库,比如JSONKit,AFNetWorking等等.可能某个类库又用 ...

  8. iOS中NSScanner 的用法

    NSScanner是一个类,用于在字符串中扫描指定的字符,尤其是把它们翻译/转换为数字和别的字符串.可以创建NSScanner时制定他的String属性,然后scanner会按照你的要求从头到尾扫描这 ...

  9. IOS中NSUserDefaults的用法(轻量级本地数据存储)

    NSUserDefaults适合存储轻量级的本地数据,比如要保存一个登陆界面的数据,用户名.密码之类的,个人觉得使用NSUserDefaults是首选.下次再登陆的时候就可以直接从NSUserDefa ...

随机推荐

  1. [leetcode]Word Ladder @ Python

    原题地址:https://oj.leetcode.com/problems/word-ladder/ 题意: Given two words (start and end), and a dictio ...

  2. RxJava RxLifecycle 生命周期 内存泄漏 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  3. 华为推送 简介 集成 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  4. spring boot整合mybatis+mybatis-plus

    Spring boot对于我来说是一个刚接触的新东西,学习过程中,发现这东西还是很容易上手的,Spring boot没配置时会默认使用Spring data jpa,这东西可以说一个极简洁的工具,可是 ...

  5. DOM之通俗易懂讲解

    DOM是所有前端开发每天打交道的东西,但是随着jQuery等库的出现,大大简化了DOM操作,导致大家慢慢的“遗忘”了它的本来面貌.不过,要想深入学习前端知识,对DOM的了解是不可或缺的,所以本文力图系 ...

  6. POJ 2280 Amphiphilic Carbon Molecules 极角排序 + 扫描线

    从TLE的暴力枚举 到 13313MS的扫描线  再到 1297MS的简化后的扫描线,简直感觉要爽翻啦.然后满怀欣喜的去HDU交了一下,直接又回到了TLE.....泪流满面 虽说HDU的时限是2000 ...

  7. 【转】vim中多标签和多窗口的使用

    原文:https://my.oschina.net/kutengshe/blog/464602 ---------------------------------------------------- ...

  8. 【转】以太网帧、IP报文格式

    原文:https://www.cnblogs.com/yongren1zu/p/6274460.html https://blog.csdn.net/gufachongyang02/article/d ...

  9. VC++ MFC应用程序项目文件2.cpp

    //GameServer.cpp: 定义应用程序的入口点. // #include "stdafx.h" #include "GameServer.h" #de ...

  10. Visual Studio 开始支持编写 Android 程序并自带 Android 模拟器【转载】

    原文地址 本文内容 为什么需要一个 Android 模拟器 针对 Visual Studio Android 模拟器的调试 Visual Studio Android 模拟器的传感器模拟和其他功能 A ...