2

Cordova /Phonegap2.2.0を使用して小さなテストアプリを構築しています

アプリはデータベースを初期化し、5つのレコードを入力します。各INSERTは関数を介して実行され、関数はループから呼び出されるため、レコード数を簡単に変更できます。テストの目的で、論理条件により、レコードのIDが奇数か偶数かが決定され、詳細フィールドに適切な文字列が入力されます。したがって、次のように詳細を含む5つのレコードを挿入する必要があります。

(id) (detail)
(1)  (This record is odd)
(2)  (This record is even)
(3)  (This record is odd)
(4)  (This record is even)
(5)  (This record is odd)

ただし、個々のレコードまたはすべてのレコードのデータを取得すると、詳細については常に「このレコードは奇数です」と表示されます。

(1)  (This record is odd)
(2)  (This record is odd)
(3)  (This record is odd)
(4)  (This record is odd)
(5)  (This record is odd)

ループを変更して6つのレコードを挿入すると、すべての結果に「このレコードは偶数」と表示されます。

つまり、最後に挿入された詳細値がすべてのレコードに適用されているかのようになります。それか、結果セットを適切に処理していません。

誰かが助けることができるならば、以下のサンプルコード。

どうもありがとう

// Create a reference to the database
function getDatabase() {
    return window.openDatabase("productDB", "1.0", "Product Database", 200000);

// Run the onDeviceReady method
onDeviceReady();
populateDatabase();

// PhoneGap is ready
function onDeviceReady() {
    db = getDatabase();
    db.transaction(function(tx) {
      tx.executeSql('DROP TABLE IF  EXISTS products');
    }, databaseError);
    db.transaction(function(tx) {
      tx.executeSql('CREATE TABLE IF NOT EXISTS products (id INTEGER PRIMARY KEY AUTOINCREMENT, product_text)');
    }, databaseError);
}

// Run a select statement to pull out all records
function getAllItems() {
    db.transaction(function(tx) {
    tx.executeSql('SELECT * FROM products', [], queryAllSuccess, databaseError);
    }, databaseError);
}

// Process the SQLResultSet
function queryAllSuccess(tx, results) {
    var len = results.rows.length;
    var output = '';
    for (i=0; i<len; i++){
        output = output + '<h2> ID ' + results.rows.item(i).id + '</h2>';
        output = output + '<p>' + results.rows.item(i).product_text + '</p>';
    }
    messageElement.html(output);
}

// Populate the database
function populateDatabase() {
    for (i=1; i<=5; i++)
    {
      insertProductItem(i);
    }                            
}

// Insert record into the database
function insertProductItem(itemId) {
    if ( itemId % 2) {
        var productText = "<p>Item " + itemId + " - This is an odd record</p>";
    } else {
        var productText = "<p>Item " + itemId + " - This is an even record</p>";
    }
    sql = 'INSERT INTO products (product_text) VALUES ("' + productText + '")';
    db.transaction(function(tx) {
        tx.executeSql(sql);
    },databaseError);
}
4

1 に答える 1

0

insertステートメントをいじる必要があります。ここでinsert構文の詳細を確認してください。

次のコードを試してください:

// Insert record into the database
function insertProductItem(itemId) {
    if ( itemId % 2) {
        var productText = "<p>Item " + itemId + " - This is an odd record</p>";
    } else {
        var productText = "<p>Item " + itemId + " - This is an even record</p>";
    }
    sql = 'INSERT INTO products (product_text) VALUES (?)'; 
    db.transaction(function(tx) { tx.executeSql(sql, [productText]); },
       databaseError);
    }
于 2012-11-26T11:51:23.557 に答える