SQLite数据库具有默认值

时间:2013-02-18 07:02:22

标签: javascript sql sqlite cordova

我使用sqlite数据库作为phonegap应用程序。

我尝试这样的事情:

首次启动应用时创建表格:

tx.executeSql('CREATE TABLE IF NOT EXISTS SETTINGS (id unique,data)');

之后我会得到第一行项目:

var returnwert = results.rows.item(0).data;

该代码将产生以下错误:

Item index is out of range!

我知道原因,因为表中没有默认值。

如何创建具有dafult值的表?

类似的东西:

 tx.executeSql('CREATE TABLE IF NOT EXISTS SETTINGS (id unique 1, 2, 3,data defaultvalue="test")');

一个名为SETTINGS且有3行的表格就是这样:

id = 0 data = test
id = 1 data = test
id = 2 data = test

有人可以帮助我

3 个答案:

答案 0 :(得分:1)

你可能想要创建这样的表:

CREATE TABLE IF NOT EXISTS settings (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    data VARCHAR(128)
)

要使用某些值填充表,请使用以下语句:

REPLACE INTO settings VALUES (1, 'test');
REPLACE INTO settings VALUES (2, 'test');
REPLACE INTO settings VALUES (3, 'test');

如果您是第一次运行它,将创建id 1,2,3的行。

如果你第二次运行它,id 1,2,3的行将被覆盖。

如果您不想覆盖,请使用INSERT OR IGNORE,如下所示:

INSERT OR IGNORE INTO settings VALUES (1, 'test');
...

答案 1 :(得分:0)

问题在于创建sqlite表。

  tx.executeSql('CREATE TABLE "SETTINGS" ("id" INTEGER PRIMARY KEY  AUTOINCREMENT  NOT    NULL , "DATA" TEXT NOT NULL);',
                  null,
                  function(){/* to do on success */},
                  function(){/* to do on fail */}
);

然后在插入时,您应该使用此查询:

tx.executeSql("INSERT INTO \"book\" VALUES(1,'test');",null,function(){/* to do on success */},function(){/* to do on fail */});
tx.executeSql("INSERT INTO \"book\" VALUES(2,'test');",null,function(){/* to do on success */},function(){/* to do on fail */});
tx.executeSql("INSERT INTO \"book\" VALUES(3,'test');",null,function(){/* to do on success */},function(){/* to do on fail */});

您随时可以参考phonegap documentary

答案 2 :(得分:0)

请尝试以下代码打开新数据库,插入并读取其中的值。

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

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

    // Wait for Cordova 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) {
  console.log("Returned rows = " + results.rows.length);
  // this will be true since it was a select statement and so rowsAffected was 0
  if (!results.rowsAffected) {
    console.log('No rows affected!');
    return false;
  }
  // for an insert statement, this property will return the ID of the last inserted row
  console.log("Last inserted row ID = " + results.insertId);
    }

    // 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", "Cordova Demo", 200000);
        db.transaction(queryDB, errorCB);
    }

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

    </script>
  </head>
  <body>
    <h1>Example</h1>
    <p>Database</p>
  </body>
</html>