0

我正在使用以下代码在 SQLite 中打开数据库并执行插入查询。

// Open the database and store the handle as a data member
if (sqlite3_open([databaseFile UTF8String], &databaseHandle) == SQLITE_OK)
{

    const char *sqlStatement = [insertStmt cStringUsingEncoding:NSUTF8StringEncoding];
    char *error;

    if (sqlite3_exec(databaseHandle, sqlStatement, NULL, NULL, &error) == SQLITE_OK)
    {
        NSLog(@"Config table values inserted.");
        registration_done = YES;
    } 
    else 
    {
        NSLog(@"Error: %s", error);
        [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]]; 

        if (sqlite3_exec(databaseHandle, sqlStatement, NULL, NULL, &error) == SQLITE_OK) {
            NSLog(@"Config table values inserted 2.");
            registration_done = YES;
        } 
        else {
            NSLog(@"Error: %s", error);
        }
    }
}
else{

    sqlite3_close(databaseHandle);
}

sqlite3_close(databaseHandle);

但我收到以下错误,之后应用程序终止:

-[__NSCFType UTF8String]:无法识别的选择器发送到实例 0x15d9a0

当我使用 Phonegap(2.1.0) 时,数据库文件和表是在 /Caches/ 目录中创建的。在调用上述 Objective-C 代码之前,Phonegap 正在正确执行查询,这意味着在 /Caches/ 目录中成功创建了表。

4

2 回答 2

0

只需使用

const char *sqlStatement = [insertStmt UTF8String];
于 2012-10-03T06:50:09.610 回答
0

@clint:- 请阅读此网址

或试试这个例子,这真的很有效

<!DOCTYPE html>
<html>
  <head>
    <title>Contact Example</title>

    <script type="text/javascript" charset="utf-8" src="phonegap-1.2.0.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Wait for PhoneGap to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // Populate the database 
    //
    function populateDB(tx) {
        tx.executeSql('DROP TABLE IF EXISTS DEMO');
        tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
        tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
        tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');
    }

    // Query the database
    //
    function queryDB(tx) {
        tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);
    }

    // Query the success callback
    //
    function querySuccess(tx, results) {
        var len = results.rows.length;
        console.log("DEMO table: " + len + " rows found.");
        for (var i=0; i<len; i++){
            console.log("Row = " + i + " ID = " + results.rows.item(i).id + " Data =  " + results.rows.item(i).data);
        }
    }

    // Transaction error callback
    //
    function errorCB(err) {
        console.log("Error processing SQL: "+err.code);
    }

    // Transaction success callback
    //
    function successCB() {
        var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
        db.transaction(queryDB, errorCB);
    }

    // PhoneGap is ready
    //
    function onDeviceReady() {
        var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
        db.transaction(populateDB, errorCB, successCB);
    }

    </script>
  </head>
  <body>
    <h1>Example</h1>
    <p>Database</p>
  </body>
</html>
于 2012-10-03T11:26:27.860 回答