標籤:
建立資料庫語句
-(void)creatData
{
sqlite3 *sqlite = nil;
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/data.file" ];
//開啟資料庫
int result = sqlite3_open([filePath UTF8String], &sqlite);
if (result !=SQLITE_OK) {
NSLog(@"建立失敗!!!");
return ;
}
//建立表的SQL語句
NSString *sql = @"CREATE TABLE IF NOT EXISTS UserTable(userName text PRIMARY KEY ,password text,email text)";
//執行SQL語句
char *error;
result = sqlite3_exec(sqlite, [sql UTF8String], NULL, NULL, &error);
if (result != SQLITE_OK) {
NSLog(@"建立資料庫失敗:%s",error);
return ;
}
//插如入一條資料
//INSERT OR REPLACE INTO UserTable (userName,password,email) VALUES(?,?,?);
//更新一條資料
//UPDATE UserTable set password = ‘‘ where userName = ‘‘;
//查詢資料
//SELECT userName ,password,eamil FROM UserTable where username = ‘‘;
//刪除資料
// DELETE FROM UserTable WHERE username =‘‘;
//關閉資料庫
sqlite3_close(sqlite);
}
**************************
-(void)editData
{
sqlite3 *sqlite = nil;
//控制代碼語句
sqlite3_stmt *stmt =nil;
//資料庫
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/data.file"];
int result = sqlite3_open([filePath UTF8String], &sqlite);
if (result !=SQLITE_OK) {
NSLog(@"開啟資料庫失敗!!!");
return ;
}
//建立SQL 陳述式
NSString *sql = @" INSERT INTO UserTable (userName,password,email) VALUES (? ,?, ?)";
//編譯SQL語句
sqlite3_prepare_v2(sqlite, [sql UTF8String], -1, &stmt, NULL);
NSString *userName = @"張三";
NSString *password = @"123456";
NSString *email = @"mxyd.qq";
//綁定填充SQL語句
sqlite3_bind_text(stmt, 1, [userName UTF8String], -1, NULL);
sqlite3_bind_text(stmt, 2, [password UTF8String], -1, NULL);
sqlite3_bind_text(stmt, 3, [email UTF8String], -1, NULL);
SQL編輯語句
//執行SQL語句
result = sqlite3_step(stmt);
if (result == SQLITE_ERROR || result == SQLITE_MISUSE) {
NSLog(@"編譯資料庫出錯!!!");
return;
}
//關閉控制代碼語句
sqlite3_finalize(stmt);
//關閉資料庫
sqlite3_close(sqlite);
NSLog(@"資料插入成功!!!");
}
SQLite 的建立與編輯