ios中Pldatabase的用法(3)
#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)的更多相关文章
- ios中Pldatabase的用法
将PLDATABASE加入到工程 下载PLDatabase 的dmg文件 将PLDatabase的framework复制到工程根目录在工程中加入该framework使用该framework进行数据库操 ...
- ios中Pldatabase的用法(4)
封装成DAO@implementation SqlHelper +(BOOL)InsertSql:(NSString *)sql paramet:(NSMutableDictionary *)parm ...
- ios中Pldatabase的用法(2)
@implementation AppGlobal static NSString* strHostName; static NSString* strVersion; static PLSqlite ...
- iOS中block的用法 以及和函数用法的区别
ios中block的用法和函数的用法大致相同 但是block的用法的灵活性更高: 不带参数的block: void ^(MyBlock)() = ^{}; 调用的时候 MyBlock(); 带参数的 ...
- iOS中Block的用法,举例,解析与底层原理(这可能是最详细的Block解析)
1. 前言 Block:带有自动变量(局部变量)的匿名函数.它是C语言的扩充功能.之所以是拓展,是因为C语言不允许存在这样匿名函数. 1.1 匿名函数 匿名函数是指不带函数名称函数.C语言中,函数是怎 ...
- ios中图片拉伸用法
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCap ...
- iOS中的CocoaPods用法及常用命令
CocoaPods是什么? ***CocoaPods的使用场景:*** 1. 当你开发iOS应用时,会经常使用到很多第三方开源类库,比如JSONKit,AFNetWorking等等.可能某个类库又用 ...
- iOS中NSScanner 的用法
NSScanner是一个类,用于在字符串中扫描指定的字符,尤其是把它们翻译/转换为数字和别的字符串.可以创建NSScanner时制定他的String属性,然后scanner会按照你的要求从头到尾扫描这 ...
- IOS中NSUserDefaults的用法(轻量级本地数据存储)
NSUserDefaults适合存储轻量级的本地数据,比如要保存一个登陆界面的数据,用户名.密码之类的,个人觉得使用NSUserDefaults是首选.下次再登陆的时候就可以直接从NSUserDefa ...
随机推荐
- 利用blob对象实现大文件分片上传
首先说分片上传,我们在进行文件上传的时候,因为服务器的限制,会限制每一次上传到服务器的文件大小不会很大,这个时候我们就需要把一个需要上传的文件进行切割,然后分别进行上传到服务器. 假如需要做到这一步, ...
- 九度oj-1001-Java
题目描述: This time, you are supposed to find A+B where A and B are two matrices, and then count the ...
- 用php实现交互式工具——How do I write a command-line interactive PHP script?
I want to write a PHP script that I can use from the command line. I want it to prompt and accept in ...
- JPA(五):映射关联关系------映射单向多对一的关联关系
映射单向多对一的关联关系 新建Customer.java: package com.dx.jpa.singlemanytoone; import java.util.Date; import java ...
- "Value does not fall within the expected range" with managed metadata fields
From: http://geekswithblogs.net/claraoscura/archive/2011/01/21/143569.aspx The problem: I have an ...
- 从头来之【MAC下代码管理工具】
本人使用的是一款免费的GIT管理工具SOURCETREE.在appstore下载完后安装. 先选中改变的文件拉倒上面,[COMMIT]---->[push] 如有疑问请留言
- conEmu的使用笔记
1.如何让conEmu成为windows的默认控制台程序? 解决:选中settings > Integration > Default Term里的Force ConEmu as defa ...
- Solidworks如何打开swb文件
把swb文件拖放到Solidworks里面,会弹出窗口选择一个文件夹 随后会自动生成对应的文件,装配体
- 使用Feign时报错Service id not legal hostname
报错Service id not legal hostname的原因是服务名称不能带有下划线,可以使用中划线
- Android使用OKHttp库实现视频文件的上传到服务器
目录 1 服务器接口简介 2 Android端代码实现 2.1 xml布局文件 2.2 Activity类 2.3 Okhttp网络通信类 1 服务器接口简介 此处我使用的服务器接口是使用Flask编 ...