ios中通用的sqlite数据库的版本是sqlite3,它具有移植性强,可靠性高,小而容易使用等特点。在ios工程中如果需要使用sqlite数据库,那么需要添加库 sqlite3.dylib或者 sqlite3.0.dylib。
代码演示:
1、创建表:
-(voID) createDB
{
Nsstring *documentDirectory = [NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory,NSUserDomainMask,YES) lastObject];
Nsstring *DBPath = [documentDirectory stringByAppendingString:@"DB"];
if(sqlite3_open([DBPath UTF8String],&_db) != sqlITE_OK)
{
sqlite3_close(_db);
NSLog(@"数据库打开失败");
}else{
char *err;
Nsstring *createsql = [Nsstring stringWithFormat:@"CREATE table IF NOT EXISTS STU (NO TEXT primary key,name TEXT)"];
if(sqlite3_exec(_db,[createsql UTF8String],NulL,&err) != sqlITE_OK){
sqlite3_close(_db);
NSLog(@"创建表失败");
}else {
NSLog(@"创建表成功");
}
sqlite3_close(_db);
}
}
2、查询数据
-(Stu *) findByNo:(Stu *) model{
Nsstring *documentDirectory = [NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory,&_db) != sqlITE_OK)
{
sqlite3_close(_db);
NSLog(@"数据可打开失败");
}else{
Nsstring *qsql = @"select no,name from STU where no = ?";
sqlite3_stmt *statement;
//预处理
if(sqlite3_prepare_v2(_db,[qsql UTF8String],-1,&statement,NulL) == sqlITE_OK){
//绑定参数
sqlite3_bind_text(statement,1,"123",NulL);
}
//开始查询
if(sqlite3_step(statement) == sqlITE_ROW){
char *no = (char *)sqlite3_column_text(statement,0);
Nsstring *stringNO = [[Nsstring alloc]initWithUTF8String:no];
char *name = (char *) sqlite3_column_text(statement,1);
Nsstring *stringname = [[Nsstring alloc] initWithUTF8String:name];
Stu *s = [[Stu alloc]init];
s.no = stringNO;
s.name = stringname;
sqlite3_finalize(statement);
return s;
}
}
return nil;
}
3、插入式数据
-(voID) insertData{
Nsstring *documentDirectory = [NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory,&_db) != sqlITE_OK){
sqlite3_close(_db);
NSLog(@"打开表失败");
}else{
Nsstring *insetString = @"insert into STU(no,name) values(?,?)";
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(_db,[insetString UTF8String],NulL) == sqlITE_OK){
sqlite3_bind_text(statement,[@"123" UTF8String],NulL);
sqlite3_bind_text(statement,2,[@"xIEguangqian" UTF8String],NulL);
if(sqlite3_step(statement) != sqlITE_OK){
NSLog(@"插入数据成功");
}else{
NSLog(@"插入数据失败");
}
sqlite3_close(_db);
}
}
}
4、删除数据
-(voID)remove{
Nsstring *documentDirectory = [NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory,YES) lastObject];
Nsstring *DBPath = [documentDirectory stringByAppendingString:@"DB"];
if(sqlite3_open([DBPath UTF8String],&_db) != sqlITE_OK){
sqlite3_close(_db);
NSLog(@"--remove---打开数据库失败");
}else {
Nsstring *sqlde = @"delete from STU where no = ?";
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(_db,[sqlde UTF8String],NulL);
if(sqlite3_step(statement) == sqlITE_DONE){
NSLog(@"已删除");
}
}
sqlite3_close(_db);
}
}
5 、修改数据
-(voID) modify{ Nsstring *documentDirectory = [NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory,YES) lastObject]; Nsstring *DBPath = [documentDirectory stringByAppendingString:@"DB"]; if(sqlite3_open([DBPath UTF8String],&_db) != sqlITE_OK){ sqlite3_close(_db); NSLog(@"数据库打开失败"); }else { Nsstring *modifysql = @"update STU set name = ? where no = ?"; sqlite3_stmt *statement; if(sqlite3_prepare_v2(_db,[modifysql UTF8String],NulL) == sqlITE_OK){ sqlite3_bind_text(statement,[@"zhangsisi" UTF8String],NulL); sqlite3_bind_text(statement,NulL); if(sqlite3_step(statement) != sqlITE_DONE){ NSLog(@"修改失败"); } sqlite3_finalize(statement); sqlite3_close(_db); }else{ NSLog(@"12345"); } } }
总结以上是内存溢出为你收集整理的sqlite数据库全部内容,希望文章能够帮你解决sqlite数据库所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)