SQLite最新的版本是3.0,使用之前应该先导入libsqlite3.0.dylib
1.导入流程
2.iOS中操作数据库的流程
打开数据库
准备SQL数据库
执行SQL数据库
语句完结
关闭数据库
3.SQLite()使用的是C的函数接口
4.创建数据库
-(void)createSQL{ NSFileManager *manager = [NSFileManager defaultManager]; if ([manager fileExistsAtPath:kDataBaseFilePath]) { NSLog(@"数据库不存在"); return; } NSLog(@"%@",kDataBaseFilePath); [manager createFileAtPath:kDataBaseFilePath contents:nil attributes:nil]; sqlite3 *sql =NULL; int openDbResult = sqlite3_open([kDataBaseFilePath UTF8String], &sql); if (openDbResult==SQLITE_OK) { NSLog(@"数据库打开成功"); } else{ NSLog(@"打开不成功"); [manager removeItemAtPath:kDataBaseFilePath error:nil]; return; } NSString *sqlString = @"CREATE TABLE user(id integer PRIMARY KEY AUTOINCREMENT, username text NOT NULL UNIQUE, password text NOT NULL);"; char *errmsg = NULL; int exeResult = sqlite3_exec(sql, [sqlString UTF8String], NULL, NULL, &errmsg); if (exeResult == SQLITE_OK) { NSLog(@"表格成功被创建"); } else{ NSLog(@"失败"); sqlite3_close(sql); [manager removeItemAtPath:kDataBaseFilePath error:nil]; return; } sqlite3_close(sql); }
5.数据库的插入数据库
-(void)createSQL{ NSFileManager *manager = [NSFileManager defaultManager]; if ([manager fileExistsAtPath:kDataBaseFilePath]) { NSLog(@"数据库不存在"); return; } NSLog(@"%@",kDataBaseFilePath); [manager createFileAtPath:kDataBaseFilePath contents:nil attributes:nil]; sqlite3 *sql =NULL; int openDbResult = sqlite3_open([kDataBaseFilePath UTF8String], &sql); if (openDbResult==SQLITE_OK) { NSLog(@"数据库打开成功"); } else{ NSLog(@"打开不成功"); [manager removeItemAtPath:kDataBaseFilePath error:nil]; return; } NSString *sqlString = @"CREATE TABLE user(id integer PRIMARY KEY AUTOINCREMENT, username text NOT NULL UNIQUE, password text NOT NULL);"; char *errmsg = NULL; int exeResult = sqlite3_exec(sql, [sqlString UTF8String], NULL, NULL, &errmsg); if (exeResult == SQLITE_OK) { NSLog(@"表格成功被创建"); } else{ NSLog(@"失败"); sqlite3_close(sql); [manager removeItemAtPath:kDataBaseFilePath error:nil]; return; } sqlite3_close(sql); }
时间: 2024-10-03 23:15:11