标签:
在应用sqlite之前需要添加sqlite库,那么我们就会发现有3和3.0的区别,开始我也并不懂,后才知道:
实际上libsqlite3.dylib本身是个链接,它指向libsqlite3.0.dylib。
也就是说在项目里如果你添加libsqlite3.dylib和添加libsqlite3.0.dylib其实是添加了同一个文件,从而使得该库常新。
为了方便管理数据库,并且使之唯一不出现混乱,我们尝尝将sqlite定义一个单例
1 static MySqliteManager *manager = nil; 2 +(MySqliteManager *)shareManager{ 3 static dispatch_once_t onceToken; 4 dispatch_once(&onceToken, ^{ 5 manager = [[MySqliteManager alloc]init]; 6 }); 7 return manager; 8 }
sqlite数据库可以分为以下几部分:
1、打开数据库
(1)如果没有该文件则创建空的sqlite后缀文件。
(2)允许对数据进行操作
- (void)open{ //确定数据库的存放路径 NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; // sqlite路径 NSString *sqlitePath = [documentPath stringByAppendingString:@"dataBase.sqlite"]; NSLog(@"path == %@",sqlitePath); //打开数据库 int result = sqlite3_open(sqlitePath.UTF8String, &db);//参数1:C中字符串,(使用点方法UTF8String转换 , ) // 判断打开是否成功 if (result == SQLITE_OK) { UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"打开成功" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil]; [alertView show]; }else{ UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"打开失败" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil]; [alertView show]; } }
2、关闭数据库
(1)禁止对数据进行任何操作
1 - (void)close{ 2 int result = sqlite3_close(db); 3 if (result == SQLITE_OK) { 4 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"关闭成功" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil]; 5 [alertView show]; 6 }else{ 7 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"关闭失败" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil]; 8 [alertView show]; 9 10 } 11 }
3、创建表
(1)创建对应的数据结构
(2)其中 primary key 指唯一,不可重复
1 - (void)create{ 2 //sql语句 3 NSString *sqlString = @"create table Person ( id integer primary key, name text ,age integer)"; 4 // 执行sql语句 5 6 char *error; 7 sqlite3_exec(db, sqlString.UTF8String, nil, nil, &error); 8 if (error == nil) { 9 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"创建表成功" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil]; 10 [alertView show]; 11 }else{ 12 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"创建表失败" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil]; 13 [alertView show]; 14 } 15 }
4、插入数据
(1)添加具体的数据实例
1 //sql语句 2 NSString *sqlString = @"insert into Person (‘name‘,‘age‘) values (‘张三‘,18)"; 3 // 执行语句 4 char *error = nil; 5 sqlite3_exec(db, sqlString.UTF8String, nil, nil, &error); 6 if (error == nil) { 7 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"插入成功" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil]; 8 [alertView show]; 9 }else{ 10 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"插入失败" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil]; 11 [alertView show]; 12 }
5、更新数据
(1)修改数据
1 //sql语句 2 NSString *sqlString = @"update Person set ‘name‘ = ‘李四‘ where id = 1"; 3 //执行sql语句 4 5 char *error = nil; 6 sqlite3_exec(db, sqlString.UTF8String, nil, nil, &error); 7 if (error == nil) { 8 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"更新成功" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil]; 9 [alertView show]; 10 }else{ 11 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"更新失败" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil]; 12 [alertView show]; 13 }
6、删除数据
1 //sql语句 2 NSString *sqlString = @"delete from Person where id = 1"; 3 //执行sql语句 4 char *error = nil; 5 sqlite3_exec(db, sqlString.UTF8String, nil, nil, &error); 6 if (error == nil) { 7 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"删除成功" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil]; 8 [alertView show]; 9 }else{ 10 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"数据库执行结果" message:@"删除失败" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil]; 11 [alertView show]; 12 }
7、查询数据
//sql语句 NSString *sqlString = @"select *from Person"; //准备sql语句 sqlite3_stmt *stmt = nil;//数据库管理指针 sqlite3_prepare(db, sqlString.UTF8String, -1/*-1代表自动计算语句长度*/, &stmt, nil); // 单步执行语句 while (sqlite3_step(stmt) == SQLITE_ROW) { int ID = sqlite3_column_int(stmt, 0);//第二个参数是参数位置 const unsigned char * name = sqlite3_column_text(stmt, 1); NSString *nameString = [NSString stringWithUTF8String:(const char *)name]; int age= sqlite3_column_int(stmt, 2); NSLog(@"id = %d \n name = %@ \n age = %d",ID,nameString,age); } // 释放管理语句 sqlite3_finalize(stmt);
常用语句:
1 首先获取iPhone上sqlite3的数据库文件的地址
2 打开iPhone上的sqlite3的数据库文件
3 准备sql文---sql语句
4 邦定参数
5 执行sql文
6 释放sql文资源
7 关闭iPhone上的sqlite3的数据库
标签:
原文地址:http://www.cnblogs.com/tig666666/p/5462713.html