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, NULL, &err) != SQLITE_OK){
sqlite3_close(_db);
NSLog(@”创建表失败”);
}else {
NSLog(@”创建表成功”);
}
sqlite3_close(_db);
}
}
2、查询数据
-(Stu *) findByNo:(Stu *) model{
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{
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″ , -1, 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, NSUserDomainMask, YES) lastObject];
NSString *DBPath = [documentDirectory stringByAppendingString:@”DB”];
if(sqlite3_open([DBPath UTF8String], &_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], -1, &statement, NULL) == SQLITE_OK){
sqlite3_bind_text(statement, 1, [@”123″ UTF8String] , -1, NULL);
sqlite3_bind_text(statement, 2, [@”xieguangqian” UTF8String], -1, NULL);
if(sqlite3_step(statement) != SQLITE_OK){
NSLog(@”插入数据成功”);
}else{
NSLog(@”插入数据失败”);
}
sqlite3_close(_db);
}
}
}
4、删除数据
-(void)remove{
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, 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], -1, &statement, NULL) == SQLITE_OK){
sqlite3_bind_text(statement, 1, [@”123″ UTF8String], -1, NULL);
if(sqlite3_step(statement) == SQLITE_DONE){
NSLog(@”已删除”);
}
}
sqlite3_close(_db);
}
}
5 、修改数据
-(void) modify{
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 {
NSString *modifySql = @”update STU set name = ? where no = ?”;
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(_db, [modifySql UTF8String], -1, &statement, NULL) == SQLITE_OK){
sqlite3_bind_text(statement, 1, [@”zhangsisi” UTF8String], -1, NULL);
sqlite3_bind_text(statement, 2, [@”123″ UTF8String], -1, NULL);
if(sqlite3_step(statement) != SQLITE_DONE){
NSLog(@”修改失败”);
}
sqlite3_finalize(statement);
sqlite3_close(_db);
}else{
NSLog(@”12345″);
}
}
}