关于Sqlite iPad那些事

移动开发 iOS
关于Sqlite iPad那些事是本文要介绍的内容,主要是来学习ipad中Sqlite数据库的内容,首先添加APPLE提供的 sqlite 操作用程序库 ibsqlite3.0.dylib 到工程中。

关于Sqlite iPad那些事是本文要介绍的内容,主要是来学习ipadSqlite数据库的内容,首先添加APPLE提供的 sqlite 操作用程序库 ibsqlite3.0.dylib 到工程中。

位置如下

  1. /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS${VER}.sdk/usr/lib/libsqlite3.0.dylib 
  1. sqlite3 *database;  
  2. NSArray *pathsNSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  3. NSString *documentsDirectory = [paths objectAtIndex:0];  
  4. NSString *strPaths =  [documentsDirectory stringByAppendingPathComponent:kFilename];  
  5. if (sqlite3_open([strPaths UTF8String], &database) != SQLITE_OK) {  
  6.         sqlite3_close(database);  
  7.         NSAssert(0, @"Failed to open databse");  
  8.     }  
  9. NSString *createSQL = @"CREATE TABLE IF NOT EXISTS FIELDS (ROW INTEGER PRIMARY KEY, FIELD_DATA TEXT)";  
  10. if(sqlite3_exec(database, [createSQL UTF8String], NULL, NULL, &errorMsg) != SQLITE_OK){  
  11.         sqlite3_close(database);  
  12.         NSAssert1(1, @"Error create table :%s", errorMsg);  
  13.     }  
  14. NSString *query = @"SELECT ROW ,FIELD_DATA FROM FIELDS ORDER BY ROW";  
  15. sqlite3_stmt *statement;  
  16.  
  17. if(sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil) == SQLITE_OK){  
  18.         while (sqlite3_step(statement) == SQLITE_ROW) {  
  19.             int row = sqlite3_column_int(statement, 0);  
  20.             char *rowData = (char *)sqlite3_column_text(statement, 1);  
  21.               
  22.             NSString *fieldName = [[NSString alloc] initWithFormat:@"field%d", row];  
  23.             NSString *fieldValue = [[NSString alloc] initWithUTF8String:rowData];  
  24.               
  25.             UITextField *field = [self valueForKey:fieldName];  
  26.             field.text = fieldValue;  
  27.             [fieldName release];  
  28.             //[fieldName release];  
  29.             [fieldValue release];  
  30.         }  
  31.         sqlite3_finalize (statement);  
  32.     } 

sqllite存在沙盒内,所以打开的时候不需要name和password,但由于字符的格式不用,所以需要通过,[nsString, UTF8String]来转换。

  1. sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil),这是执行sql语句的命令。statement记录状态。  
  2.  
  3. sqlite3_column_*(statement, 0);返回字段值  
  4. sqlite3_finalize (statement);结束退出  
  5.  
  6. #import "SQLiteTutorialAppDelegate.h"  
  7. #import "RootViewController.h"  
  8. #import "Animal.h" // Import the animal object header  
  9.  
  10. @implementation SQLiteTutorialAppDelegate  
  11.  
  12. @synthesize window;  
  13. @synthesize navigationController;  
  14. @synthesize animals; // Synthesize the aminals array  
  15.  
  16. - (void)applicationDidFinishLaunching:(UIApplication *)application {  
  17. // Setup some globals  
  18. databaseName = @"AnimalDatabase.sql";  
  19.  
  20. // Get the path to the documents directory and append the databaseName  
  21. NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  22. NSString *documentsDir = [documentPaths objectAtIndex:0];  
  23. databasePath = [documentsDir stringByAppendingPathComponent:databaseName];  
  24.  
  25. // Execute the "checkAndCreateDatabase" function  
  26. [self checkAndCreateDatabase];  
  27.  
  28. // Query the database for all animal records and construct the "animals" array  
  29. [self readAnimalsFromDatabase];  
  30.  
  31. // Configure and show the window  
  32. [window addSubview:[navigationController view]];  
  33. [window makeKeyAndVisible];  
  34. }  
  35.  
  36. - (void)applicationWillTerminate:(UIApplication *)application {  
  37. // Save data if appropriate  
  38. }  
  39.  
  40. - (void)dealloc {  
  41. [animals release];  
  42. [navigationController release];  
  43. [window release];  
  44. [super dealloc];  
  45. }  
  46.  
  47. -(void) checkAndCreateDatabase{  
  48. // Check if the SQL database has already been saved to the users phone, if not then copy it over  
  49. BOOL success;  
  50.  
  51. // Create a FileManager object, we will use this to check the status  
  52. // of the database and to copy it over if required  
  53. NSFileManager *fileManager = [NSFileManager defaultManager];  
  54.  
  55. // Check if the database has already been created in the users filesystem  
  56. success = [fileManager fileExistsAtPath:databasePath];  
  57.  
  58. // If the database already exists then return without doing anything  
  59. if(success) return;  
  60.  
  61. // If not then proceed to copy the database from the application to the users filesystem  
  62.  
  63. // Get the path to the database in the application package  
  64. NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];  
  65.  
  66. // Copy the database from the package to the users filesystem  
  67. [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];  
  68.  
  69. [fileManager release];  
  70. }  
  71.  
  72. -(void) readAnimalsFromDatabase {  
  73. // Setup the database object  
  74. sqlite3 *database;  
  75.  
  76. // Init the animals Array  
  77. animals = [[NSMutableArray alloc] init];  
  78.  
  79. // Open the database from the users filessytem  
  80. if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {  
  81. // Setup the SQL Statement and compile it for faster access  
  82. const char *sqlStatement = "select * from animals";  
  83. sqlite3_stmt *compiledStatement;  
  84. if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {  
  85. // Loop through the results and add them to the feeds array  
  86. while(sqlite3_step(compiledStatement) == SQLITE_ROW) {  
  87. // Read the data from the result row  
  88. NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];  
  89. NSString *aDescription = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];  
  90. NSString *aImageUrl = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];  
  91.  
  92. // Create a new animal object with the data from the database  
  93. Animal *animal = [[Animal alloc] initWithName:aName description:aDescription url:aImageUrl];  
  94. // Add the animal object to the animals Array  
  95. [animals addObject:animal];  
  96. [animal release];  
  97. }  
  98. }  
  99. // Release the compiled statement from memory  
  100. sqlite3_finalize(compiledStatement);  
  101. }  
  102. sqlite3_close(database);  
  103. }  
  104. @end 

下面这2句都不可以执行

  1. if (select count(*) from sqlite_master where table="TB_Clothing_Main")  
  2. DROP TABLE "TB_Clothing_Main";  
  3.  
  4. if EXISTS (select count(*) from sqlite_master where name = 'TB_Clothing_Main')  
  5. DROP TABLE 'TB_Clothing_Main';  
  6.  
  7. BEGIN;  
  8.  
  9. CREATE TABLE [TB_Clothing_Main] (  
  10. [clothing_ID] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,  
  11. [clothing_who] INTEGER NOT NULL,  
  12. [clothing_decription] NVARCHAR(128) NULL,  
  13. [clothing_brend] INTEGER NOT NULL,  
  14. [clothing_buy_location_ID] INTEGER NOT NULL,  
  15. [clothing_store_location_ID] INTEGER NOT NULL,  
  16. [clothing_size_shoulder] FLOAT NULL,  
  17. [clothing_size_chest] FLOAT NULL,  
  18. [clothing_size_waist] FLOAT NULL,  
  19. [clothing_size_hip] FLOAT NULL,  
  20. [clothing_size_length] FLOAT NULL,  
  21. [clothing_type] INTEGER NOT NULL,  
  22. [clothing_price] FLOAT NULL,  
  23. [clothing_main_picture] NVARCHAR(128) NULL  
  24. );  
  25.  
  26. INSERT INTO "TB_Clothing_Main" VALUES(0, 1, 'marc jacobs blue short T', 1, 2, 1, 37.5, 45,   38,   NULL, 66, 0, NULL,   'mj01');  
  27. INSERT INTO "TB_Clothing_Main" VALUES(1, 1, 'marc jacobs pink short T', 1, 1, 0, 37,   43.5, 36,   NULL, 64, 0, NULL,   'mj02');  
  28. INSERT INTO "TB_Clothing_Main" VALUES(2, 2, 'nautica blue coat',        0, 0, 2, 41,   49.5, 47.2, NULL, 60, 1, NULL,   'baba01');  
  29. INSERT INTO "TB_Clothing_Main" VALUES(3, 1, 'juicy yellow coat',        3, 1, 3, 40,   45.1, 40.2, 47  , 62, 2, 1080.2, 'juicy01');  
  30. INSERT INTO "TB_Clothing_Main" VALUES(4, 1, 'siwy jeans',               4, 1, 3, NULL,   NULL, 78, 93  , 94, 3, 1380,   'siwy01');  
  31.  
  32. COMMIT; 

小结:关于Sqlite iPad那些事的内容介绍完了,希望通过本文的学习能对你有所帮助!

责任编辑:zhaolei 来源: 新浪博客
相关推荐

2012-01-02 19:30:22

iPad

2012-05-01 21:27:55

图标

2012-05-01 08:06:49

手机

2011-07-04 13:51:02

QT 对象 模型

2011-08-01 17:31:25

Xcode开发 Cocoa

2012-07-13 00:03:08

WEB前端开发WEB开发

2019-12-10 08:00:46

Kata容器Linux

2015-09-14 09:28:47

2011-07-19 15:33:57

iPhone

2011-06-30 10:59:43

2021-03-18 16:05:20

SSD存储故障

2009-02-19 10:21:00

路由多WAN口

2022-09-09 08:08:28

开源项目服务

2015-08-13 10:54:46

2024-03-18 00:00:05

Java服务JVM

2012-04-05 10:49:40

服务器SSL证书

2021-05-17 08:18:35

Java内存模型JMM

2015-08-19 09:10:37

程序员面试

2017-10-19 16:27:34

2015-07-23 13:10:38

服务器虚拟化
点赞
收藏

51CTO技术栈公众号