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 ...
随机推荐
- oracle的行级触发器使用
行级触发器: 当触发器被触发时,要使用被插入.更新或删除的记录中的列值,有时要使用操作前.后列的值. :NEW 修饰符访问操作完成后列的值 :OLD 修饰符访问操作完成前列的值 例1: 建立一个触发器 ...
- 层叠顺序与堆栈上下文、font-family字体定义顺序的
1.层叠顺序与堆栈上下文 z-index 看上去其实很简单,根据 z-index 的高低决定层叠的优先级,实则深入进去,会发现内有乾坤. 问题背景:拥有共同父容器的两个 DIV 重叠在一起,是 dis ...
- JS的scrollIntoView简单使用
scrollIntoView方法滚动当前元素,进入浏览器的可见区域 el.scrollIntoView(); // 等同于el.scrollIntoView(true) el.scrollIntoVi ...
- eclipse 创建普通maven项目
- 用迁移学习创造的通用语言模型ULMFiT,达到了文本分类的最佳水平
https://www.jqr.com/article/000225 这篇文章的目的是帮助新手和外行人更好地了解我们新论文,我们的论文展示了如何用更少的数据自动将文本分类,同时精确度还比原来的方法高. ...
- 【收藏】介绍RCU的好文章
RCU原理: RCU(Read-Copy Update),顾名思义就是读-拷贝修改,它是基于其原理命名的.对于被RCU保护的共享数据结构,读者不需要获得任何锁就可以访问它,但写者在访问它时首先拷贝一个 ...
- LeetCode 292 Nim Game(Nim游戏)
翻译 你正在和你的朋友们玩以下这个Nim游戏:桌子上有一堆石头.每次你从中去掉1-3个.谁消除掉最后一个石头即为赢家.你在取出石头的第一轮. 你们中的每个人都有着聪明的头脑和绝佳的策略.写一个函数来确 ...
- template.helper 多参数
<script type="text/html" id="text4"> {{detail name classInfo schoolInfo}} ...
- Python爬虫学习系列教程
最近想学一下Python爬虫与检索相关的知识,在网上看到这个教程,觉得挺不错的,分享给大家. 来源:http://cuiqingcai.com/1052.html 一.Python入门 1. Pyth ...
- Eclipse导入git上的maven web项目 部署 - lpshou
http://www.tuicool.com/articles/fqm2Qf 推酷 文章 微博 主题 站点 活动 应用 周刊 登录 Eclipse导入git上的maven web项目 部署 - ...