iOS嵌入式數(shù)據(jù)庫SQLite3使用簡析
1. Sqlite3數(shù)據(jù)類型及存儲類
每個存放在sqlite數(shù)據(jù)庫中(或者由這個數(shù)據(jù)庫引擎操作)的值都有下面中的一個存儲類:
(1)NULL,值是NULL
(2)INTEGER,值是有符號整形,根據(jù)值的大小以1,2,3,4,6或8字節(jié)存放
(3)REAL,值是浮點型值,以8字節(jié)IEEE浮點數(shù)存放
(4)TEXT,值是文本字符串,使用數(shù)據(jù)庫編碼(UTF-8,UTF-16BE或者UTF-16LE)存放
(5)BLOB,只是一個數(shù)據(jù)塊,完全按照輸入存放(即沒有準換)
注:①Sqlite沒有單獨的布爾存儲類型,它使用INTEGER作為存儲類 型,0為false,1為true
②Sqlite沒有另外為存儲日期和時間設(shè)定一個存儲類集,內(nèi)置的sqlite日期和時間函數(shù)能夠?qū)⑷掌诤蜁r間以TEXT,REAL或INTEGER形式存放
TEXT 作為IS08601字符串("YYYY-MM-DD HH:MM:SS.SSS")
REAL 從格林威治時間11月24日,4174 B.C中午以來的天數(shù)
INTEGER 從 1970-01-01 00:00:00 UTC以來的秒數(shù)
2. Sqlite3函數(shù)功能簡介
1)基本函數(shù)及結(jié)構(gòu)體
sqlite3 *pdb //數(shù)據(jù)庫句柄,跟文件句柄FILE很類似
sqlite3_stmt *stmt //這個相當于ODBC的Command對象,用于保存編譯好的SQL語句
sqlite3_open() //打開數(shù)據(jù)庫
sqlite3_exec() //執(zhí)行非查詢的sql語句
sqlite3_prepare() //準備sql語句,執(zhí)行select語句或者要使用parameter bind時 , 用這個函數(shù)(封裝了sqlite3_exec).
sqlite3_step() //在調(diào)用sqlite3_prepare后,使用這個函數(shù)在記錄集中移動。
sqlite3_close() //關(guān)閉數(shù)據(jù)庫文件
2)綁定函數(shù)
int sqlite3_bind_null(sqlite3_stmt*, int);
int sqlite3_bind_int(sqlite3_stmt*, int, int);
int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));
int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
3)取值函數(shù)
sqlite3_column_text(), 取text類型的數(shù)據(jù)
sqlite3_column_blob(),取blob類型的數(shù)據(jù)
sqlite3_column_int(), 取int類型的數(shù)據(jù)
3. Sqlite3使用步驟
1) 首先獲取iPhone上Sqlite 3 的數(shù)據(jù)庫文件的地址
2) 打開Sqlite 3 的數(shù)據(jù)庫文件
3) 定義SQL文
4) 邦定執(zhí)行SQL所需要的參數(shù)
5) 執(zhí)行SQL文,并獲取結(jié)果
6) 釋放資源
7) 關(guān)閉Sqlite 3 數(shù)據(jù)庫。
4. Sqlite3數(shù)據(jù)操作
由于整個例子代碼比較繁瑣,這里只列出數(shù)據(jù)庫操作的部分代碼作為參考:
1.添加開發(fā)包libsqlite3.0.dylib
首先是設(shè)置項目文件,在項目中添加iPhone版的sqlite3的數(shù)據(jù)庫的開發(fā)包,在項目下的Frameworks點擊右鍵,然后選擇libsqlite3.0.dylib文件。
libsqlite3.0.dylib文件地址:
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.2.sdk/usr/lib/libsqlite3.0.dylib
2.獲取sqlite3的數(shù)據(jù)庫文件地址:
- //數(shù)據(jù)庫路徑
- -(NSString*)databasePath
- {
- NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *pathname = [path objectAtIndex:0];
- return [pathname stringByAppendingPathComponent:@”database.sqlite3”];
- }
3.打開數(shù)據(jù)庫
- - (BOOL)openDatabase
- {
- if (sqlite3_open([[self databasePath] UTF8String],&database) != SQLITE_OK)
- {
- sqlite3_close(database);
- printf("failed to open the database");
- return NO;
- }
- else
- {
- printf("open the database successfully");
- return YES;
- }
- }
4.創(chuàng)建表
- //創(chuàng)建TimerTable表
- - (BOOL)createTimerTable
- {
- if ([self openDatabase]==YES)
- {
- char *erroMsg;
- NSString *createSQL = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@(timerid INTEGER PRIMARY KEY AUTOINCREMENT,time INTEGER,remaintime INTEGER,iconuri BLOB,vibrate INTEGER,status INTEGER,message TEXT)",TableName];
- if (sqlite3_exec(database, [createSQL UTF8String], NULL, NULL, &erroMsg)!= SQLITE_OK)
- {
- sqlite3_close(database);
- printf("create table faild");
- return NO;
- }
- else
- {
- printf("table was created");
- return YES;
- }
- }
- else
- return NO;
- }
5.添加數(shù)據(jù)
- //添加Timer
- - (BOOL)insertTimer:(TimerInfo *)timerInfo
- {
- bool isOpen=[self openDatabase];
- if (isOpen!=YES)
- {
- return NO;
- }
- sqlite3_stmt *statement;
- static char *insertTimerSql="INSERT INTO TimerTable(time,remaintime,iconuri,vibrate,status,message,type) VALUES (?,?,?,?,?,?)";
- if (sqlite3_prepare_v2(database,insertTimerSql,-1,&statement,NULL)!= SQLITE_OK)
- {
- NSLog(@"Error:Failed to insert timer");
- return NO;
- }
- sqlite3_bind_int(statement,1,timerInfo.time);//timerInfo是一個封裝了相關(guān)屬性的實體類對象
- sqlite3_bind_int(statement,2,timerInfo.remainTime);
- sqlite3_bind_text(statement,3,[timerInfo.iconuri UTF8String],-1,SQLITE_TRANSIENT);
- sqlite3_bind_int(statement,4,timerInfo.vibrate);
- sqlite3_bind_int(statement,5,timerInfo.status);
- sqlite3_bind_text(statement,6,[timerInfo.message UTF8String],-1,SQLITE_TRANSIENT);
- int success=sqlite3_step(statement);
- sqlite3_finalize(statement);
- if(success==SQLITE_ERROR)
- {
- NSLog(@"Error:fail to insert into the database with message.");
- return NO;
- }
- NSLog(@"inserted one timer");
- return YES;
- }
6.查詢數(shù)據(jù)
- //查詢數(shù)據(jù)庫中所有的TimerInfo,返回一個包含所有TimerInfo的可變數(shù)組
- -(NSMutableArray *)getAllTimers
- {
- NSMutableArray *arrayTimers=[[NSMutableArray alloc] init];
- NSString *queryStr=@"SELECT * FROM TimerTable";
- sqlite3_stmt *statement;
- if (sqlite3_prepare_v2(database, [queryStr UTF8String], -1, &statement, NULL)!=SQLITE_OK)
- {
- printf("Failed to get all timers!\n");
- }
- else
- {
- while (sqlite3_step(statement)==SQLITE_ROW)
- {
- TimerInfo *timerInfo=[[TimerInfo alloc] init];
- timerInfo.timerId =sqlite3_column_int(statement,0);
- timerInfo.time =sqlite3_column_int(statement,1);
- timerInfo.remainTime=sqlite3_column_int(statement,2);
- timerInfo.vibrate =sqlite3_column_int(statement,4);
- timerInfo.status = sqlite3_column_int(statement,5);
- char *messageChar=sqlite3_column_text(statement,6);
- if (messageChar==NULL)
- timerInfo.message=nil;
- else
- timerInfo.message =[NSString stringWithUTF8String:messageChar];
- [arrayTimers addObject:timerInfo];
- [timerInfo release];
- }
- }
- sqlite3_finalize(statement);
- NSLog(@"arrayTimersCount: %i",[arrayTimers count]);
- return arrayTimers;
- }
此外,還有數(shù)據(jù)庫數(shù)據(jù)的更新及其刪除操作就不再依依列出,它們之間只是執(zhí)行的SQL文不同,其他部分的代碼較為類似。
運行效果:(具體頁面控件及事件:略)
在這里只是做下小結(jié),其中難免有說的不對的地方,所以希望大家多多給予指正。