在當今智能手機的天下,我們很多數據都是保存在手機上,如備忘錄,通訊錄等,镔哥,告訴你,在各種文件格式在搜索與保存的速度都比不上數據庫快,畢竟數據庫在保存的時候經過特殊格式安排的。而不論是ios或者是android,其內部都是采用SQLite這個嵌入式數據庫作為解決方案。因為比起其他商業數據庫,它可就明顯小得多。
SQLite屬於輕量級別的數據庫,它不需要任何設置,更不需要架設任何服務器。這樣使得它特別適合用於內存寸土寸金的嵌入式設備中。
sqlite的創建數據庫,表,插入查看數據
iOS sqlite數據庫操作。步驟是:
先加入sqlite開發庫libsqlite3.dylib,
新建或打開數據庫,
創建數據表,
插入數據,
查詢數據並打印
1、新建項目sqliteDemo,添加使用sqlite的庫libsqlite3.dylib
2、sqlite 的方法
sqlite3 *db, 數據庫句柄,跟文件句柄FILE很類似<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+CnNxbGl0ZTNfc3RtdCAgICAgICpzdG10LCDV4rj2z+C1sdPaT0RCQ7XEQ29tbWFuZLbUz/OjrNPD09qxo7TmseDS67rDtcRTUUzT777kPGJyPgpzcWxpdGUzX29wZW4oKSwgICC08r+qyv2+3b/io6zDu9PQyv2+3b/iyrG0tL2ooaM8YnI+CnNxbGl0ZTNfZXhlYygpLCAgINa00NC3x7Lp0a+1xHNxbNPvvuQ8YnI+ClNxbGl0ZTNfc3RlcCgpLCDU2rX308NzcWxpdGUzX3ByZXBhcmW686OsyrnTw9XiuPa6r8r91Nq8x8K8vK/W0NLGtq+hozxicj4KU3FsaXRlM19jbG9zZSgpLCC52LHVyv2+3b/izsS8/jxicj4Ku7nT0NK7z7XB0LXEuq/K/aOs08PT2rTTvMfCvLyv19a2ztbQu/HIocr9vt2jrMjnPGJyPgpzcWxpdGUzX2NvbHVtbl90ZXh0KCksIMihdGV4dMDg0M21xMr9vt2hozxicj4Kc3FsaXRlM19jb2x1bW5fYmxvYqOoo6mjrMihYmxvYsDg0M21xMr9vt08YnI+CnNxbGl0ZTNfY29sdW1uX2ludCgpLCDIoWludMDg0M21xMr9vt08YnI+CjwvcD4KPHA+Cjxicj4KPC9wPgo8aDI+M6Giu/HIocmzutDEv8K8o6yyorS0vai78rTyv6rK/b7dv+KhozwvaDI+CjxwPgp2aWV3Q29udHJvbGxlci5ozbfOxLz+zO2809K7uPazydSxseTBv6OssqKw/LqszbfOxLz+c3FsaXRlMy5oPC9wPgo8cD4KPC9wPgoKCgo8c3Ryb25nPltjcHBdPC9zdHJvbmc+IHZpZXcKIHBsYWluY29weQoKCgoKPG9sIHN0YXJ0PQ=="1" class="dp-cpp">
#import
#import
@interface ViewController : UIViewController
{
sqlite3 *db;
}
@end
在.m文件 定義宏,方面後面使用
-
#define DBNAME @"personinfo.sqlite"
-
#define NAME @"name"
-
#define AGE @"age"
-
#define ADDRESS @"address"
-
#define TABLENAME @"PERSONINFO"
-
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
-
NSString *documents = [paths objectAtIndex:0];
-
NSString *database_path = [documents stringByAppendingPathComponent:DBNAME];
-
-
if (sqlite3_open([database_path UTF8String], &db) != SQLITE_OK) {
-
sqlite3_close(db);
-
NSLog(@"數據庫打開失敗");
-
}
sqlite3_open,如果數據不存在,則創建。運行。這是在沙盒目錄下能看到數據庫文件(如何打開模擬器沙盒目錄請參考:iOS學習之iOS沙盒(sandbox)機制和文件操作(一))
4、創建數據表
創建一個獨立的執行sql語句的方法,傳入sql語句,就執行sql語句
-
-(void)execSql:(NSString *)sql
-
{
-
char *err;
-
if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK) {
-
sqlite3_close(db);
-
NSLog(@"數據庫操作數據失敗!");
-
}
-
}
創建數據表PERSONINFO的語句
-
NSString *sqlCreateTable = @"CREATE TABLE IF NOT EXISTS PERSONINFO (ID INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, address TEXT)";
-
[self execSql:sqlCreateTable];
運行程序,數據表創建了。怎麼知道數據表創建了呢?我們用火狐的Sqlite Manager插件工具打開數據庫文件看看。可以在火狐浏覽器裡安裝這個插件。打開
四個字段都出現是表中了。
5、插入數據:
-
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];
運行程序,插入兩條數據,用火狐的sqlite工具查看
6、查詢數據庫並打印數據
-
NSString *sqlQuery = @"SELECT * FROM PERSONINFO";
-
sqlite3_stmt * statement;
-
-
if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
-
while (sqlite3_step(statement) == SQLITE_ROW) {
-
char *name = (char*)sqlite3_column_text(statement, 1);
-
NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
-
-
int age = sqlite3_column_int(statement, 2);
-
-
char *address = (char*)sqlite3_column_text(statement, 3);
-
NSString *nsAddressStr = [[NSString alloc]initWithUTF8String:address];
-
-
NSLog(@"name:%@ age:%d address:%@",nsNameStr,age, nsAddressStr);
-
}
-
}
-
sqlite3_close(db);
打印結果:
[cpp] view
plaincopy
-
2012-06-29 13:25:32.205 sqlitDemo[3587:f803] name:張三 age:23 address:西城區
-
2012-06-29 13:25:32.206 sqlitDemo[3587:f803] name:老六 age:20 addre