SQLite是一個開源的嵌入式關系數據庫,它在2000年由D. Richard Hipp發布,它的減少應用程序管理數據的開銷,SQLite可移植性好,很容易使用,很小,高效而且可靠。
SQLite嵌入到使用它的應用程序中,它們共用相同的進程空間,而不是單獨的一個進程。從外部看,它並不像一個RDBMS,但在進程內部,它卻是完整的,自包含的數據庫引擎。
嵌入式數據庫的一大好處就是在你的程序內部不需要網絡配置,也不需要管理。因為客戶端和服務器在同一進程空間運行。SQLite 的數據庫權限只依賴於文件系統,沒有用戶帳戶的概念。SQLite 有數據庫級鎖定,沒有網絡服務器。它需要的內存,其它開銷很小,適合用於嵌入式設備。你需要做的僅僅是把它正確的編譯到你的程序。
ios下使用sqlite首先導入SQLite3.0的lib庫。然後包含頭文件#import <sqlite3.h>
下面的代碼主要對常用的數據庫操作如查詢、插入、刪除、更新等進行封裝,方便以後使用。
頭文件代碼如下:
- #import <Foundation/Foundation.h>
- #import <sqlite3.h>
- #import "User.h"
-
- #define kFileName @"database.sqlite"
-
- @interface SQLite3Util : NSObject{
- sqlite3_stmt *stmt;
- sqlite3 *database;
- }
-
- - (NSString *)dataFilePath;
- - (int)getCountOfDB;
- - (BOOL)insertOrUpdateUser:(NSString *)sql;
- - (BOOL)deleteUser:(NSInteger)userId;
- - (NSMutableArray *)getUsers;
- - (User *)getUser:(NSInteger)userId;
- - (BOOL)openDatabase;
- - (void)closeDatabase;
- @end
實現如下:
- #import "SQLite3Util.h"
- @implementation SQLite3Util
-
-
- //Return Database path
- - (NSString *)dataFilePath{
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUSErDomainMask, YES);
- NSString *documentsDirectory = [paths objectAtIndex:0];
- return [documentsDirectory stringByAppendingPathComponent:kFileName];
- }
-
- //Insert or update data
- - (BOOL)insertOrUpdateUser:(NSString *)sql{
- if ([self openDatabase]) {
- if (sqlite3_exec(database, [sql UTF8String], nil, &stmt, nil) != SQLITE_OK) {
- NSLog(@"Insert or update is failed!");
- return NO;
- }else{
- // sqlite3_finalize(stmt);
- NSLog(@"Insert or update successfully!");
- return YES;
- }
- sqlite3_close(database);
-
- }
- return NO;
- }
-
- //Delete a user from database
- - (BOOL)deleteQuestion:(NSInteger)userId{
- NSString *sql = [NSString stringWithFormat:@"Delete FROM User WHERE id = %i", userId];
- if ([self openDatabase]) {
- if (sqlite3_exec(database, [sql UTF8String], nil, &stmt, nil) != SQLITE_OK) {
- NSLog(@"Delete data is failed!");
- return NO;
- }else{
- sqlite3_finalize(stmt);
- NSLog(@"Delete data successfully!");
- return YES;
- }
- sqlite3_close(database);
- }
- return NO;
- }
- //Get users
- - (NSMutableArray *)getUsers{
- NSMutableArray *users = [[NSMutableArray alloc] init];
- NSString *sql = [NSString stringWithFormat:@"SELECT * FROM User];
- if ([self openDatabase]) {
- if (sqlite3_prepare_v2(database, [sql UTF8String], -1, &stmt, nil) == SQLITE_OK) {
- // NSLog(@"SQL is Prepared!");
- while (sqlite3_step(stmt) == SQLITE_ROW) {
- User *user = [[Question alloc] init];
- char *name = (char *)sqlite3_column_text(stmt, 0);
- [user setName:[NSString stringWithUTF8String:name]];
- char *index = (char *)sqlite3_column_text(stmt, 1);
- [user setId:[[NSString stringWithUTF8String:index] intValue]];
- [users addObject: user];
- }
- sqlite3_finalize(stmt);
- }
- }
- sqlite3_close(database);
- return users;
- }
-
- //Get count of the users
- - (int)getCountOfDB{
- int count = 0;
- NSString *sql = [NSString stringWithFormat:@"SELECT * FROM User"];
- if ([self openDatabase]) {
- if (sqlite3_prepare_v2(database, [sql UTF8String], -1, &stmt, nil) == SQLITE_OK) {
- while (sqlite3_step(stmt) == SQLITE_ROW) {
- count ++;
- }
- sqlite3_finalize(stmt);
- }
- }
- return count;
- }
-
- - (User *)getUser:(NSInteger)userId{
- User *user = [[User alloc] init];
- NSString *sql = [NSString stringWithFormat:@"SELECT * FROM User WHERE id = %i", userId];
- // NSLog(@"sql = %@", sql);
- if ([self openDatabase]) {
- if (sqlite3_prepare_v2(database, [sql UTF8String], -1, &stmt, nil) == SQLITE_OK) {
- NSLog(@"SQL is Prepared!");
- while (sqlite3_step(stmt) == SQLITE_ROW) {
- char *name = (char *)sqlite3_column_text(stmt, 0);
- [user setName:[NSString stringWithUTF8String:name]];
- [user setId:userId];
- }
- sqlite3_finalize(stmt);
- }
- }
- return user;
- }
-
- - (BOOL)openDatabase{
- if (sqlite3_open([[self dataFilePath] UTF8String], &database) == SQLITE_OK) {
- NSLog(@"SQL is Open!");
- return YES;
- }
- return NO;
- }
-
- - (void)closeDatabase{
- sqlite3_close(database);
- }
- @end
在以後的代碼中直接調用即可,如查詢一個用戶:
- SQlite3Util *sqlUtil = [[SQlite3Util alloc] init];
- NSIngeter userId = 15;
- User *user = [sqlUitl getUser:userId];