2013-04-12 97 views
0

我正在用cordova 2.1.0框架在IOS中創建應用程序。我做以下命令來創建數據庫:sqlite db創建+ objective-c +科爾多瓦

NSFileManager *fileManager = [NSFileManager defaultManager]; 
//NSError *error; 
if (![fileManager fileExistsAtPath:self.databaseFile]) { 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    self.databaseFile = [documentsDirectory stringByAppendingPathComponent:@"klb_db.sqlite"]; 

    [self createConfigTable]; 
} else { 
    NSLog(@"fail to create database"); 
} 


// to create config table 
-(void) createConfigTable{ 

    NSString *createStmt = @"CREATE TABLE IF NOT EXISTS config (id INTEGER PRIMARY KEY AUTOINCREMENT,key text,value text);"; 

    // Open the database and or create it 
    if (sqlite3_open_v2([self.databaseFile UTF8String], &databaseHandle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE , NULL) == SQLITE_OK) 
    { 
     NSLog(@"database created"); 

     const char *sqlStatement = [createStmt UTF8String]; 

     char *error; 

     if (sqlite3_exec(databaseHandle, sqlStatement, NULL, NULL, &error) == SQLITE_OK) 
     { 
      NSLog(@"Config table created."); 
     } 
    } 
    else{ 

     sqlite3_close(databaseHandle); 
    } 

    sqlite3_close(databaseHandle); 

    return; 
} 

然後,在科爾多瓦的index.html,我試圖運行上面創建的數據庫查詢爲:

dbName = 'Database'; 

//to insert username, password into db 
db = window.openDatabase(dbName, "1.0", gAppConfig.dbMessage, 200000); 

db.transaction(querySelectConfig, errorQuery); 


//Get Messages from the DB 
      function querySelectConfig(tx) { 

       alert('select config') 
       tx.executeSql('SELECT * FROM "'+gAppConfig.configTable+'";', [], querySelectSuccess, errorQuery); 
      } 

在功能querySelectConfig( ),查詢失敗。爲什麼查詢失敗。目標-c中的數據庫創建過程是否有缺陷。其次,klb_db.sqlite文件的用途是什麼。當創建新的數據庫時,它是否需要空白。以及.sqlite文件是如何創建的。

回答