DetailsIPhoneConnectionSqlite DatabaseInstance operations are the content of this article, mainly based on code implementation.IPhoneConnectionSqlite DatabaseFor details.
I believe that database operations are required in N multiple applications.
- 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
Summary: DetailsIPhoneConnectionSqlite DatabaseI hope this article will help you with the introduction of instance operations!