博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ios中Pldatabase的用法(3)
阅读量:7249 次
发布时间:2019-06-29

本文共 4086 字,大约阅读时间需要 13 分钟。

#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",@12]){
//注意参数必须是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",@2013,@2]){
//注意参数必须是oc对象 NSLog(@"update success"); } else{ NSLog(@"updata failed"); }}-(void)selectdata{ NSString *sql=@"SELECT id,name,age FROM TEST WHERE id=?"; id
result=[self.db executeQuery:sql,@1]; 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 '%%%@%%')",@1,@2]; id
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,@1]){ NSLog(@"delete success"); } else{ NSLog(@"delete failed"); }}

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

-(void)InsertData1{    NSString *sql=@"INSERT INTO TEST(name,age) values(:name,:age)";        id
stm=[self.db prepareStatement:sql]; NSMutableDictionary *dic=[NSMutableDictionary dictionary]; [dic setObject:@"tt" forKey:@"name"]; [dic setObject:@12 forKey:@"age"]; [stm bindParameterDictionary:dic]; if([stm executeUpdate]){ NSLog(@"ss"); }

 

-(void) selectdate2{    NSString *sql=@"SELECT id,name,age FROM TEST WHERE id=:id";    id
stmp=[self.db prepareStatement:sql]; NSMutableDictionary *dic=[NSMutableDictionary dictionary]; [dic setObject:@1 forKey:@"id"]; [stmp bindParameterDictionary:dic]; id
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); }}

 

转载于:https://www.cnblogs.com/gcb999/p/3200309.html

你可能感兴趣的文章
PPT of "SharePoint 2007 网站性能优化"
查看>>
爪哇国新游记之三十四----Dom4j的XPath操作
查看>>
node17
查看>>
Java程序性能优化4
查看>>
第一次负责项目总结
查看>>
Azure Redis Cache (2) 创建和使用Azure Redis Cache
查看>>
python统计ES存储空间占用的代码
查看>>
成就连自己都惊讶的未来
查看>>
依赖倒置(DIP)与依赖注入(DI)
查看>>
mysql数据库授权
查看>>
Microstation
查看>>
深入浅出的英语口语700句zz
查看>>
linux编译安装php
查看>>
再谈奶牛问题
查看>>
第一个java程序------hello world!
查看>>
C#学习安排表
查看>>
在LINUX上创建GIT服务器【转】
查看>>
Linux内核跟踪之trace框架分析【转】
查看>>
XCode v9.6.2017.0830
查看>>
ES不设置副本是非常脆弱的,整个文章告诉了你为什么
查看>>