标签:
SQLite最新的版本是3.0,使用之前应该先导入libsqlite3.0.dylib
打开数据库
准备SQL数据库
执行SQL数据库
语句完结
关闭数据库
-(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); }
-(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); }
标签:
原文地址:http://www.cnblogs.com/Moshimol/p/5697053.html