建立數據庫語句sql
-(void)creatData數據庫
{spa
sqlite3 *sqlite = nil;sqlite
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/data.file" ];string
//打開數據庫it
int result = sqlite3_open([filePath UTF8String], &sqlite);編譯
if (result !=SQLITE_OK) {class
NSLog(@"建立失敗!!!");email
return ;file
}
//建立表的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(@"數據插入成功!!!");
}