0

我正在使用cordova 2.1.0 框架在IOS 中创建一个应用程序。我正在执行以下操作来创建数据库:

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;
}

然后,在 cordova 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() 中,查询失败。为什么查询失败。Objective-c 中的数据库创建过程是否存在缺陷?其次,klb_db.sqlite 文件的用途是什么。创建新数据库时,是否需要为空。以及如何创建 .sqlite 文件。

4

1 回答 1

0

使用核心数据,它将为您节省时间

http://developer.apple.com/library/mac/documentation/cocoa/Conceptual/CoreData/cdProgrammingGuide.html

于 2013-04-12T12:00:05.507 回答