IOS开发之数据sqlite使用
一、引入工具包
,该工具包为C语言工具包。
二、代码操作数据库
1、创建并且链接数据库
- (void) _connectDB{
//1>获取沙盒路径作为数据库创建时候的初始化路径
NSString * path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
path=[path ponent:@""];
NSLog(@"%@",path);
//2>如果存在则打开当前链接,如果不存在则创建
if(SQLITE_OK==sqlite3_open(, &sqlite)){
NSLog(@"数据库创建成功!");
}else {
NSLog(@"数据库创建失败!");
}
}
2、操作数据库
/**
* 创建表
*/
- (void) _createTable{
NSString *create=@" create table if not exists t_Person (id integer primary key autoincrement,name text,age integer,tel text)";
[self _execSql:create andTip:@"创建表操作"];
}
/**
* 插入数据操作
*
* ***@param name 姓名
* ***@param age 年龄
* ***@param tel 电话
*/
- (void) _insertName:(NSString *) name andAge:(int )age andTel:(NSString *) tel{
NSString * insert=[NSString stringWithFormat:@" insert into t_Person(name,age,tel) values('%@',%d,'%@')",name,age,tel];
[self _execSql:insert andTip:@"插入操作"];
}
/**
* 执行数据库操作
*
* ***@param sql 要执行的sql
* ***@param tip 要执行的操作标题
*/
- (void) _execSql:(NSString *) sql andTip:(NSString *) tip{
char * result;
if(SQLITE_OK==sqlite3_exec(sqlite, , NULL, NULL, &result)){
NSLog(@"%@成功!",tip);
}else{
NSLog(@"%@失败!",tip);
}
}
3、查询数据库
/**
* 读取数据
*/
- (void)_readData{
//1> 定义sql语句
NSString * sql=@"select id,name
IOS开发之数据sqlite使用 来自淘豆网m.daumloan.com转载请标明出处.