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:
- /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS${VER}.sdk/usr/lib/libsqlite3.0.dylib
- sqlite3 *database;
- NSArray *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentsDirectory = [paths objectAtIndex:0];
- NSString *strPaths = [documentsDirectory stringByAppendingPathComponent:kFilename];
- if (sqlite3_open([strPaths UTF8String], &database) != SQLITE_OK) {
- sqlite3_close(database);
- NSAssert(0, @"Failed to open databse");
- }
- NSString *createSQL = @"CREATE TABLE IF NOT EXISTS FIELDS (ROW INTEGER PRIMARY KEY, FIELD_DATA TEXT)";
- if(sqlite3_exec(database, [createSQL UTF8String], NULL, NULL, &errorMsg) != SQLITE_OK){
- sqlite3_close(database);
- NSAssert1(1, @"Error create table :%s", errorMsg);
- }
- NSString *query = @"SELECT ROW ,FIELD_DATA FROM FIELDS ORDER BY ROW";
- sqlite3_stmt *statement;
-
- if(sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil) == SQLITE_OK){
- while (sqlite3_step(statement) == SQLITE_ROW) {
- int row = sqlite3_column_int(statement, 0);
- char *rowData = (char *)sqlite3_column_text(statement, 1);
-
- NSString *fieldName = [[NSString alloc] initWithFormat:@"field%d", row];
- NSString *fieldValue = [[NSString alloc] initWithUTF8String:rowData];
-
- UITextField *field = [self valueForKey:fieldName];
- field.text = fieldValue;
- [fieldName release];
- //[fieldName release];
- [fieldValue release];
- }
- sqlite3_finalize (statement);
- }
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.
- Sqlite3_prepare_v2 (database, [query UTF8String],-1, & statement, nil), which is the command for executing SQL statements. Statement record status.
-
- Sqlite3_column _ * (statement, 0); Return Field Value
- Sqlite3_finalize (statement); End and exit
-
- # Import "SQLiteTutorialAppDelegate. h"
- # Import "RootViewController. h"
- # Import "Animal. h" // Import the animal object header
-
- @ Implementation SQLiteTutorialAppDelegate
-
- @ Synthesize window;
- @ Synthesize navigationController;
- @ Synthesize animals; // Synthesize the aminals array
-
- -(Void) applicationDidFinishLaunching :( UIApplication *) application {
- // Setup some globals
- DatabaseName = @ "AnimalDatabase. SQL ";
-
- // Get the path to the specified ents directory and append the databaseName
- NSArray * documentPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
- NSString * documentsDir = [documentPaths objectAtIndex: 0];
- DatabasePath = [documentsDir stringByAppendingPathComponent: databaseName];
-
- // Execute the "checkAndCreateDatabase" function
- [Self checkAndCreateDatabase];
-
- // Query the database for all animal records and construct the "animals" array
- [Self readAnimalsFromDatabase];
-
- // Configure and show the window
- [Window addSubview: [navigationController view];
- [Window makeKeyAndVisible];
- }
-
- -(Void) applicationWillTerminate :( UIApplication *) application {
- // Save data if appropriate
- }
-
- -(Void) dealloc {
- [Animals release];
- [NavigationController release];
- [Window release];
- [Super dealloc];
- }
-
- -(Void) checkAndCreateDatabase {
- // Check if the SQL database has already been saved to the users phone, if not then copy it over
- BOOL success;
-
- // Create a FileManager object, we will use this to check the status
- // Of the database and to copy it over if required
- NSFileManager * fileManager = [NSFileManager defaultManager];
-
- // Check if the database has already been created in the users filesystem
- Success = [fileManager fileExistsAtPath: databasePath];
-
- // If the database already exists then return without doing anything
- If (success) return;
-
- // If not then proceed to copy the database from the application to the users filesystem
-
- // Get the path to the database in the application package
- NSString * databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: databaseName];
-
- // Copy the database from the package to the users filesystem
- [FileManager copyItemAtPath: databasePathFromApp toPath: databasePath error: nil];
-
- [FileManager release];
- }
-
- -(Void) readAnimalsFromDatabase {
- // Setup the database object
- Sqlite3 * database;
-
- // Init the animals Array
- Animals = [[NSMutableArray alloc] init];
-
- // Open the database from the users filessytem
- If (sqlite3_open ([databasePath UTF8String], & database) = SQLITE_ OK ){
- // Setup the SQL Statement and compile it for faster access
- Const char * sqlStatement = "select * from animals ";
- Sqlite3_stmt * compiledStatement;
- If (sqlite3_prepare_v2 (database, sqlStatement,-1, & compiledStatement, NULL) = SQLITE_ OK ){
- // Loop through the results and add them to the feeds array
- While (sqlite3_step (compiledStatement) = SQLITE_ROW ){
- // Read the data from the result row
- NSString * aName = [NSString stringwithuf8string :( char *) sqlite3_column_text (compiledStatement, 1)];
- NSString * adeiterator = [NSString stringwithuf8string :( char *) sqlite3_column_text (compiledStatement, 2)];
- NSString * aImageUrl = [NSString stringwithuf8string :( char *) sqlite3_column_text (compiledStatement, 3)];
-
- // Create a new animal object with the data from the database
- Animal * animal = [[Animal alloc] initWithName: aName description: aDescription url: aImageUrl];
- // Add the animal object to the animals Array
- [Animals addObject: animal];
- [Animal release];
- }
- }
- // Release the compiled statement from memory
- Sqlite3_finalize (compiledStatement );
- }
- Sqlite3_close (database );
- }
- @ End
None of the following two statements can be executed
- if (select count(*) from sqlite_master where table="TB_Clothing_Main")
- DROP TABLE "TB_Clothing_Main";
-
- if EXISTS (select count(*) from sqlite_master where name = 'TB_Clothing_Main')
- DROP TABLE 'TB_Clothing_Main';
-
- BEGIN;
-
- CREATE TABLE [TB_Clothing_Main] (
- [clothing_ID] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
- [clothing_who] INTEGER NOT NULL,
- [clothing_decription] NVARCHAR(128) NULL,
- [clothing_brend] INTEGER NOT NULL,
- [clothing_buy_location_ID] INTEGER NOT NULL,
- [clothing_store_location_ID] INTEGER NOT NULL,
- [clothing_size_shoulder] FLOAT NULL,
- [clothing_size_chest] FLOAT NULL,
- [clothing_size_waist] FLOAT NULL,
- [clothing_size_hip] FLOAT NULL,
- [clothing_size_length] FLOAT NULL,
- [clothing_type] INTEGER NOT NULL,
- [clothing_price] FLOAT NULL,
- [clothing_main_picture] NVARCHAR(128) NULL
- );
-
- 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');
- 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');
- INSERT INTO "TB_Clothing_Main" VALUES(2, 2, 'nautica blue coat', 0, 0, 2, 41, 49.5, 47.2, NULL, 60, 1, NULL, 'baba01');
- 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');
- INSERT INTO "TB_Clothing_Main" VALUES(4, 1, 'siwy jeans', 4, 1, 3, NULL, NULL, 78, 93 , 94, 3, 1380, 'siwy01');
-
- COMMIT;
Summary: AboutSqlite iPadThe content of those things has been introduced, and I hope this article will help you!