About Sqlite iPad

Source: Internet
Author: User

AboutSqlite iPadThese are the content to be introduced in this article, mainly to learnIpadMediumSqliteFirst, addSqliteThe operation library ibsqlite3.0.dylib is used in the project.

The location is as follows:

 
 
  1. /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS${VER}.sdk/usr/lib/libsqlite3.0.dylib 
 
 
  1. sqlite3 *database;  
  2. NSArray *paths= NSSearchPathForDirectoriesInDomains(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 exists in the sandbox, so name and password are not required when opening, but because the character format is not used, you need to use [nsString, UTF8String] for conversion.

 
 
  1. Sqlite3_prepare_v2 (database, [query UTF8String],-1, & statement, nil), which is the command for executing SQL statements. Statement record status.
  2.  
  3. Sqlite3_column _ * (statement, 0); Return Field Value
  4. Sqlite3_finalize (statement); End and exit
  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 specified ents 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 stringwithuf8string :( char *) sqlite3_column_text (compiledStatement, 1)];
  89. NSString * adeiterator = [NSString stringwithuf8string :( char *) sqlite3_column_text (compiledStatement, 2)];
  90. NSString * aImageUrl = [NSString stringwithuf8string :( 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

None of the following two statements can be executed

 
 
  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; 

Summary: AboutSqlite iPadThe content of those things has been introduced, and I hope this article will help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.