歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> iPhone開發之SQLite使用詳解

iPhone開發之SQLite使用詳解

日期:2017/3/1 10:18:39   编辑:Linux編程

SQLite是一個開源的嵌入式關系數據庫,它在2000年由D. Richard Hipp發布,它的減少應用程序管理數據的開銷,SQLite可移植性好,很容易使用,很小,高效而且可靠。

SQLite嵌入到使用它的應用程序中,它們共用相同的進程空間,而不是單獨的一個進程。從外部看,它並不像一個RDBMS,但在進程內部,它卻是完整的,自包含的數據庫引擎。

嵌入式數據庫的一大好處就是在你的程序內部不需要網絡配置,也不需要管理。因為客戶端和服務器在同一進程空間運行。SQLite 的數據庫權限只依賴於文件系統,沒有用戶帳戶的概念。SQLite 有數據庫級鎖定,沒有網絡服務器。它需要的內存,其它開銷很小,適合用於嵌入式設備。你需要做的僅僅是把它正確的編譯到你的程序。

ios下使用sqlite首先導入SQLite3.0的lib庫。然後包含頭文件#import <sqlite3.h>

下面的代碼主要對常用的數據庫操作如查詢、插入、刪除、更新等進行封裝,方便以後使用。

頭文件代碼如下:

  1. #import <Foundation/Foundation.h>
  2. #import <sqlite3.h>
  3. #import "User.h"
  4. #define kFileName @"database.sqlite"
  5. @interface SQLite3Util : NSObject{
  6. sqlite3_stmt *stmt;
  7. sqlite3 *database;
  8. }
  9. - (NSString *)dataFilePath;
  10. - (int)getCountOfDB;
  11. - (BOOL)insertOrUpdateUser:(NSString *)sql;
  12. - (BOOL)deleteUser:(NSInteger)userId;
  13. - (NSMutableArray *)getUsers;
  14. - (User *)getUser:(NSInteger)userId;
  15. - (BOOL)openDatabase;
  16. - (void)closeDatabase;
  17. @end

實現如下:

  1. #import "SQLite3Util.h"
  2. @implementation SQLite3Util
  3. //Return Database path
  4. - (NSString *)dataFilePath{
  5. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUSErDomainMask, YES);
  6. NSString *documentsDirectory = [paths objectAtIndex:0];
  7. return [documentsDirectory stringByAppendingPathComponent:kFileName];
  8. }
  9. //Insert or update data
  10. - (BOOL)insertOrUpdateUser:(NSString *)sql{
  11. if ([self openDatabase]) {
  12. if (sqlite3_exec(database, [sql UTF8String], nil, &stmt, nil) != SQLITE_OK) {
  13. NSLog(@"Insert or update is failed!");
  14. return NO;
  15. }else{
  16. // sqlite3_finalize(stmt);
  17. NSLog(@"Insert or update successfully!");
  18. return YES;
  19. }
  20. sqlite3_close(database);
  21. }
  22. return NO;
  23. }
  24. //Delete a user from database
  25. - (BOOL)deleteQuestion:(NSInteger)userId{
  26. NSString *sql = [NSString stringWithFormat:@"Delete FROM User WHERE id = %i", userId];
  27. if ([self openDatabase]) {
  28. if (sqlite3_exec(database, [sql UTF8String], nil, &stmt, nil) != SQLITE_OK) {
  29. NSLog(@"Delete data is failed!");
  30. return NO;
  31. }else{
  32. sqlite3_finalize(stmt);
  33. NSLog(@"Delete data successfully!");
  34. return YES;
  35. }
  36. sqlite3_close(database);
  37. }
  38. return NO;
  39. }
  40. //Get users
  41. - (NSMutableArray *)getUsers{
  42. NSMutableArray *users = [[NSMutableArray alloc] init];
  43. NSString *sql = [NSString stringWithFormat:@"SELECT * FROM User];
  44. if ([self openDatabase]) {
  45. if (sqlite3_prepare_v2(database, [sql UTF8String], -1, &stmt, nil) == SQLITE_OK) {
  46. // NSLog(@"SQL is Prepared!");
  47. while (sqlite3_step(stmt) == SQLITE_ROW) {
  48. User *user = [[Question alloc] init];
  49. char *name = (char *)sqlite3_column_text(stmt, 0);
  50. [user setName:[NSString stringWithUTF8String:name]];
  51. char *index = (char *)sqlite3_column_text(stmt, 1);
  52. [user setId:[[NSString stringWithUTF8String:index] intValue]];
  53. [users addObject: user];
  54. }
  55. sqlite3_finalize(stmt);
  56. }
  57. }
  58. sqlite3_close(database);
  59. return users;
  60. }
  61. //Get count of the users
  62. - (int)getCountOfDB{
  63. int count = 0;
  64. NSString *sql = [NSString stringWithFormat:@"SELECT * FROM User"];
  65. if ([self openDatabase]) {
  66. if (sqlite3_prepare_v2(database, [sql UTF8String], -1, &stmt, nil) == SQLITE_OK) {
  67. while (sqlite3_step(stmt) == SQLITE_ROW) {
  68. count ++;
  69. }
  70. sqlite3_finalize(stmt);
  71. }
  72. }
  73. return count;
  74. }
  75. - (User *)getUser:(NSInteger)userId{
  76. User *user = [[User alloc] init];
  77. NSString *sql = [NSString stringWithFormat:@"SELECT * FROM User WHERE id = %i", userId];
  78. // NSLog(@"sql = %@", sql);
  79. if ([self openDatabase]) {
  80. if (sqlite3_prepare_v2(database, [sql UTF8String], -1, &stmt, nil) == SQLITE_OK) {
  81. NSLog(@"SQL is Prepared!");
  82. while (sqlite3_step(stmt) == SQLITE_ROW) {
  83. char *name = (char *)sqlite3_column_text(stmt, 0);
  84. [user setName:[NSString stringWithUTF8String:name]];
  85. [user setId:userId];
  86. }
  87. sqlite3_finalize(stmt);
  88. }
  89. }
  90. return user;
  91. }
  92. - (BOOL)openDatabase{
  93. if (sqlite3_open([[self dataFilePath] UTF8String], &database) == SQLITE_OK) {
  94. NSLog(@"SQL is Open!");
  95. return YES;
  96. }
  97. return NO;
  98. }
  99. - (void)closeDatabase{
  100. sqlite3_close(database);
  101. }
  102. @end

在以後的代碼中直接調用即可,如查詢一個用戶:

  1. SQlite3Util *sqlUtil = [[SQlite3Util alloc] init];
  2. NSIngeter userId = 15;
  3. User *user = [sqlUitl getUser:userId];
Copyright © Linux教程網 All Rights Reserved