標籤:
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); }
iOS資料庫操作流程