iphone開發 SQLite資料庫使用

2021-05-28 07:27:03 字數 3196 閱讀 1041

我現在要使用sqlite3.0建立乙個資料庫,然後在資料庫中建立乙個**。

首先要引入sqlite3.0的lib庫。然後包含標頭檔案#import

開啟資料庫,如果沒有,那麼建立乙個

sqlite3* database_;

-(bool) open

return yes; }

if(sqlite3_open([path utf8string], &database_) == sqlite_ok) else

return no; }

建立**

//建立**,假設有五個字段,(id,cid,title,imagedata ,imagelen )

//說明一下,id為**的主鍵,必須有。

//cid,和title都是字串,imagedata是二進位制資料,imagelen 是該二進位制資料的長度。

- (bool) createchannelstable:(sqlite3*)db

int success = sqlite3_step(statement);

sqlite3_finalize(statement);

if ( success != sqlite_done)

nslog(@"create table 'channels' successed.");

return yes; }

向**中插入一條記錄

假設channle是乙個資料結構體,儲存了一條記錄的內容。

- (bool) insertonechannel:(channel*)channel

//這裡的數字1,2,3,4代表第幾個問號

sqlite3_bind_text(statement, 1, [channel.id_ utf8string], -1, sqlite_transient);

sqlite3_bind_text(statement, 2, [channel.title_ utf8string], -1, sqlite_transient);

sqlite3_bind_blob(statement, 3, [imagedata bytes], imagelen, sqlite_transient);

sqlite3_bind_int(statement, 4, imagelen);    

success = sqlite3_step(statement);

sqlite3_finalize(statement);

if (success == sqlite_error)  

nslog(@"insert one channel#############:id = 

%@",channel.id_);

return yes; }

資料庫查詢

這裡獲取**中所有的記錄,放到陣列fchannels中。

- (void) getchannels:(nsmutablearray*)fchannels

//查詢結果集中一條一條的遍歷所有的記錄,這裡的數字對應的是列值。

while (sqlite3_step(statement) == sqlite_row)

[fchannels addobject:channel];

[channel release]; }

sqlite3_finalize(statement); }

iphone訪問本地資料庫sqlite3

首先需要在專案中引用sqlite 3的開發包,下面是在iphone sdk 3.0下的目錄:

/developer/platforms/iphoneos.platform/developer/sdks/iphoneos3.0.sdk/usr/lib/libsqlite3.0.dylib

到這裡你需要事先用命令來建立sqlite 3的資料庫檔案,並在其中建立自己的表等等,然後作為資源檔案新增到專案,然後在程式第一次執行的時候複製到程式下的documents或其他目錄下,關於sqlite 3的基本操作網上已經有不少文章,這裡就不重複了。

在iphone中使用sqlite 3主要步驟如下:

1 首先獲取iphone上sqlite 3的資料庫檔案的位址

2 開啟sqlite 3的資料庫檔案

3 定義sql文

4 邦定執行sql所需要的引數

5 執行sql文,並獲取結果

6 釋放資源

7 關閉sqlite 3資料庫。

下面結合**來示範一下。

// 首先獲取iphone上sqlite3的資料庫檔案的位址

nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes);  

nsstring *documentsdirectory = [paths objectatindex:0];  

// 開啟sqlite3的資料庫檔案

sqlite3 *database;  

sqlite3_open([path utf8string], &database);  

// 定義sql文

sqlite3_stmt *stmt;  

const

char *sql = "select * from table_name where pk=? and name=?";  

sqlite3_prepare_v2(database, sql, -1, &stmt, null);  

// 邦定第乙個int引數

sqlite3_bind_int(stmt, 1, 1);  

// 邦定第二個字串引數

sqlite3_bind_text(stmt, 2, [title utf8string], -1, sqlite_transient);  

// 執行sql文,並獲取結果

sqlite3_step(stmt);  

// 釋放資源

sqlite3_finalize(stmt);  

// 關閉sqlite3資料庫

sqlite3_close(database);  

iphone開發之SQLite使用詳解

原文 sqlite是乙個開源的嵌入式關聯式資料庫,它在2000年由d.richard hipp發布,它的減少應用程式管理資料的開銷,sqlite可移植性好,很容易使用,很小,高效而且可靠。sqlite嵌入到使用它的應用程式中,它們共用相同的程序空間,而不是單獨的乙個程序。從外部看,它並不像乙個rdb...

iphone開發 SQLite資料庫使用

我現在要使用sqlite3.0建立乙個資料庫,然後在資料庫中建立乙個 首先要引入sqlite3.0的lib庫。然後包含標頭檔案 import 開啟資料庫,如果沒有,那麼建立乙個 sqlite3 database bool open return yes if sqlite3 open path ut...

iphone開發 SQLite資料庫使用

我現在要使用sqlite3.0建立乙個資料庫,然後在資料庫中建立乙個 首先要引入sqlite3.0的lib庫。然後包含標頭檔案 import 開啟資料庫,如果沒有,那麼建立乙個 sqlite3 database 找到資料庫檔案mydb.sql if find return yes if sqlite...