#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<PLResultSet> 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<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,@1]){ 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:@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<PLPreparedStatement> stmp=[self.db prepareStatement:sql]; NSMutableDictionary *dic=[NSMutableDictionary dictionary]; [dic setObject:@1 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); } }