iPhone開(kāi)發(fā)中數(shù)據(jù)庫(kù)使用教程
iPhone開(kāi)發(fā)中數(shù)據(jù)庫(kù)使用是本文要介紹的內(nèi)容,iPhone使用的是sqlite數(shù)據(jù)庫(kù),我用了firefox的插件Sqlite Manager來(lái)管理sqlite,這個(gè)插件很好用,可以很方便的進(jìn)行視圖化的創(chuàng)建以及管理sqlite。廢話(huà)不多說(shuō),進(jìn)入正題。
要使用sqlite首先要在Frameworks中引入libsqlite3.0.dylib這個(gè)文件,具體步驟我就略過(guò)了,然后創(chuàng)建數(shù)據(jù)庫(kù),建好后將數(shù)據(jù)庫(kù)添加到Resources目錄下(記得勾選Copy items into ...這個(gè)選項(xiàng)),現(xiàn)在準(zhǔn)備工作都已經(jīng)做好,下面進(jìn)入代碼編寫(xiě)。
為了方便使用,以及以后的維護(hù),我在這里創(chuàng)建了一個(gè)類(lèi)將數(shù)據(jù)庫(kù)的相關(guān)代碼進(jìn)行了封裝。創(chuàng)建一個(gè)NSObject類(lèi),我在這里取名為GADatabase,然后在實(shí)現(xiàn)文件中添加以下代碼:
- #import <sqlite3.h>
 - id getColValue(sqlite3_stmt *stmt,int iCol) {
 - int type = sqlite3_column_type(stmt, iCol);
 - switch (type) {
 - case SQLITE_INTEGER:
 - return [NSNumber numberWithInt:sqlite3_column_int(stmt, iCol)];
 - break;
 - case SQLITE_FLOAT:
 - return [NSNumber numberWithDouble:sqlite3_column_double(stmt, iCol)];
 - break;
 - case SQLITE_TEXT:
 - return [NSString stringWithUTF8String:sqlite3_column_text(stmt, iCol)];
 - break;
 - case SQLITE_BLOB:
 - return [NSData dataWithBytes:sqlite3_column_blob(stmt, iCol) length:sqlite3_column_bytes(stmt, iCol)];
 - break;
 - case SQLITE_NULL:
 - return @"";
 - break;
 - default:
 - return @"NONE";
 - break;
 - }
 - }
 
在這里,我使用了C風(fēng)格的定義,并且定義在了@implementation之外以方便使用,有了這個(gè)函數(shù)在以后的數(shù)據(jù)提取時(shí)很方便,其中的數(shù)據(jù)類(lèi)型可根據(jù)實(shí)際情況進(jìn)行增減。OK,現(xiàn)在在@implementation與@end之間添加下面的代碼以獲取iphone中sqlite的地址:
- + (NSString *)pathForDatabase {
 - NSArray *arrayOfPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 - NSString *path = [arrayOfPaths objectAtIndex:0];
 - path = [path stringByAppendingPathComponent:@"yourDatabaseName.sqlite"];
 - NSLog(path);
 - NSFileManager *fileManager = [NSFileManager defaultManager];
 - if(![fileManager fileExistsAtPath:path]){
 - NSString *databaseSource = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"yourDatabaseName.sqlite"];
 - if(![fileManager copyItemAtPath:databaseSource toPath:path error:nil]){
 - return nil;
 - }
 - }
 - return path;
 - }
 
好了,現(xiàn)在可以寫(xiě)sql語(yǔ)句了,我簡(jiǎn)單舉幾個(gè)例子:
查詢(xún)某一個(gè)字段:
- - (NSString *)select:(NSString *)Parameter {
 - sqlite3 *database;
 - sqlite3_stmt *stm;
 - NSString *result = [NSString string];
 - NSString *sql = [NSString stringWithFormat:@"SELECT columnName FROM table WHERE columnName='%@'", Parameter];
 - if(sqlite3_open([[GADatabase pathForDatabase] UTF8String], &database) == SQLITE_OK) {
 - if(sqlite3_prepare_v2(database, [sql UTF8String], -1, &stm, NULL) == SQLITE_OK) {
 - if(sqlite3_step(stm) == SQLITE_ROW) {
 - result = getColValue(stm, 0);
 - }
 - }
 - sqlite3_finalize(stm);
 - }
 - sqlite3_close(database);
 - return result;
 - }
 
若查詢(xún)多字段,可以用一個(gè)數(shù)組并結(jié)合一個(gè)自定義類(lèi)來(lái)存儲(chǔ),例:
- - (NSMutableArray *)selectUsers {
 - sqlite3 *database;
 - sqlite3_stmt *stm;
 - NSMutableArray *result = [[NSMutableArray alloc] init];
 - NSString *sql = @"SELECT * FROM users";
 - if(sqlite3_open([[GADatabase pathForDatabase] UTF8String], &database) == SQLITE_OK) {
 - if(sqlite3_prepare_v2(database, [sql UTF8String], -1, &stm, NULL) == SQLITE_OK) {
 - while(sqlite3_step(stm) == SQLITE_ROW) {
 - GAData *userObj = [[GAData alloc] init];
 - userObj.rId = getColValue(stm, 0);
 - userObj.userName = getColValue(stm, 1);
 - userObj.passWord = getColValue(stm, 2);
 - [result addObject:userObj];
 - [userObj release];
 - }
 - }
 - sqlite3_finalize(stm);
 - }
 - sqlite3_close(database);
 - return result;
 - }
 
GAData的類(lèi)定義如下:
- #import <Foundation/Foundation.h>
 - @interface GAData : NSObject {
 - NSNumber *rId;
 - NSString *userName;
 - NSString *passWord;
 - }
 - @property(nonatomic, retain)NSNumber *rId;
 - @property(nonatomic, retain)NSString *userName;
 - @property(nonatomic, retain)NSString *passWord;
 - @end
 - #import "GAData.h"
 - @implementation GAData
 - @synthesize rId;
 - @synthesize userName;
 - @synthesize passWord;
 - @end
 
向數(shù)據(jù)庫(kù)添加數(shù)據(jù):
- - (void)addUser:(GAData *)addUserObj {
 - sqlite3 *database;
 - NSString *sql = [NSString stringWithFormat:@"INSERT INTO users (userName, passWord) VALUES('%@','%@')",
 - addUserObj.userName, addUserObj.passWord];
 - int status = sqlite3_open([[GADatabase pathForDatabase] UTF8String], &database);
 - if(status != SQLITE_OK) {
 - return;
 - }
 - status = sqlite3_exec(database, [sql UTF8String], 0, 0, NULL);
 - if(status != SQLITE_OK) {
 - return;
 - }
 - sqlite3_close(database);
 - }
 
刪除,修改與添加類(lèi)似,無(wú)非是sql語(yǔ)句的不同,就不再舉例了,下面我再說(shuō)下sqlite中的時(shí)間函數(shù),由于目前為止我還只用到了關(guān)于計(jì)算天數(shù)的函數(shù),所以其他的我就不介紹了,大家可以在網(wǎng)上搜索一下,看下面這條sql語(yǔ)句:
- SELECT columnName FROM table WHERE (julianday(date(columnName))-julianday(date('now')))>10
 
julianday()這個(gè)函數(shù)會(huì)返回一個(gè)天數(shù),從格林威治時(shí)間公元前4714年11月24號(hào)開(kāi)始算起。date()函數(shù)返回一個(gè)以“YYYY-MM-DD”為格式的日期。因此上面那條語(yǔ)句也就不難理解了。
關(guān)于數(shù)據(jù)庫(kù)方面的我就暫時(shí)寫(xiě)這么多了,有不夠好的地方或者錯(cuò)誤的地方歡迎大家指出來(lái),大家一起學(xué)習(xí)嘛。最后,我再提醒一句,如果修改了Resources目錄下的數(shù)據(jù)庫(kù)內(nèi)容,需要將原先編譯好的程序刪除掉,重新編譯,或者找到程序運(yùn)行時(shí)的路徑,刪掉Documents文件夾下的數(shù)據(jù)庫(kù)文件,再重新編譯運(yùn)行,只有這樣,你程序中的數(shù)據(jù)庫(kù)才會(huì)更新,切記!
小結(jié):iPhone開(kāi)發(fā)中數(shù)據(jù)庫(kù)使用教程的內(nèi)容介紹完了,希望本文對(duì)你有所幫助!















 
 
 
 
 
 
 