IOS中使用輕量級數據庫git
目錄github
sqlite的方法sql
數據庫的實用操做數據庫
FMDatabase框架
概述函數
IOS中的輕量級數據庫spa
sqlite的方法orm
sqlite3 *db, 數據庫句柄,跟文件句柄FILE很相似sqlite
sqlite3_stmt *stmt, 這個至關於ODBC的Command對象,用於保存編譯好的SQL語句對象
sqlite3_open(), 打開數據庫,沒有數據庫時建立。
sqlite3_exec(), 執行非查詢的sql語句
Sqlite3_step(), 在調用sqlite3_prepare後,使用這個函數在記錄集中移動。
Sqlite3_close(), 關閉數據庫文件
還有一系列的函數,用於從記錄集字段中獲取數據,如
sqlite3_column_text(), 取text類型的數據。
sqlite3_column_blob(),取blob類型的數據
sqlite3_column_int(), 取int類型的數據
數據庫的實用操做
操做數據庫的準備
導入libsqlite3.0.dylib/libsqlite3.dylib框架
#import <sqlite3.0.h>
聲明數據庫句柄:sqlite3 *db;
宏定義Document路徑和數據庫文件路徑
宏定義表名、表字段名
打開數據庫
- (void)openDatabase{
if (sqlite3_open([database_path UTF8String], &db) != SQLITE_OK) {
sqlite3_close(db);
NSLog(@"數據庫打開失敗!");
}else{
NSLog(@"數據庫打開成功!");
}
}
關閉數據庫
- (void)closeDatabase{
sqlite3_close(db);
NSLog(@"關閉數據庫!");
}
執行非查詢語句
- (void)execSql:(NSString *)sql{
[self openDatabase];
char *error;
if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &error) != SQLITE_OK) {
NSLog(@"數據庫操做失敗!");
}else{
NSLog(@"數據庫操做成功!");
}
[self closeDatabase];
}
執行查詢語句(顯示全部表數據爲例)
- (void)showTable:(NSString *)tableName{
[self openDatabase];
NSString *sqlQuery = [NSString stringWithFormat:@"SELECT * FROM %@",tableName];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
char *std_id = (char *)sqlite3_column_text(statement, 0); //學號
int age = sqlite3_column_int(statement, 1); //年齡
char *gender = (char *)sqlite3_column_text(statement, 2); //性別
NSString *std_id_str = [NSString stringWithUTF8String:std_id];
NSString *std_gender = [NSString stringWithUTF8String:gender];
NSLog(@" std_id : %@ \n std_age : %d \n std_gender : %@",std_id_str,age,std_gender);
}
}
[self closeDatabase];
}
建立數據表
-(IBAction)createNewTable{
NSString *sqlCreateTable = @"CREATE TABLE IF NOT EXISTS PERSONINFO(name TEXT,age INTEGER,address TEXT)";
[self execSql:sqlCreateTable];
}
插入數據
-(void)insertData{
NSString *sql1 = [NSString stringWithFormat:@"INSERT INTO '%@'('%@','%@','%@') VALUES('%@','%@','%@')",TABLENAME,NAME,AGE,ADDRESS,@"張三",@"23",@"西城區"];
NSString *sql2 = [NSString stringWithFormat:@"INSERT INTO '%@'('%@','%@','%@') VALUES('%@','%@','%@')",TABLENAME,NAME,AGE,ADDRESS,@"李四",@"20",@"東城區"];
[self execSql:sql1];
[self execSql:sql2];
}
刪除數據
-(IBAction)deleteData{
NSString *sqlDelete = [NSString stringWithFormat:@"DELETE FROM %@ WHERE NAME=\"%@\" AND AGE=%@ AND ADDRESS=\"%@\"",TABLENAME,@"張三",@"23",@"西城區"];
[self execSql:sqlDelete];
}
刪除表
-(IBAction)deleteTable{
NSString *deleteTable = [NSString stringWithFormat:@"DROP TABLE %@",TABLENAME];
[self execSql:deleteTable];
}
第三方類庫
FMDatabase
建立數據庫實例
FMDatabase *db = [FMDatabase databaseWithPath:dbPath];
if (![db open]){
NSLog(@"Could not open db.");
return;
}
[db close];
注:數據庫文件能夠爲.db或者.sqlite格式,若指定目錄裏面沒有該文件,則會自動進行建立;注意數據庫的開和關
執行Update(建表、插入、刪除)
[db executeUpdate:@"CREATE TABLE IF NOT EXISTS MyTable(aa float,bb text,cc int)"]; //返回BOOL值
執行Query語句
FMResultSet *rs = [db executeQuery:@"SELECT * FROM User WHERE Age = ?",@"19"];
while ([rs next]) {
NSString *name = [rs stringForColumn:@"Name"];
NSString *age = [rs stringForColumn:@"Age"];
NSLog(@"name is %@ , age is %@",name,age);
}
詳細用法參考
https://github.com/ccgus/fmdb