简体   繁体   English

如何使用JavaScript创建多个SQlite表?

[英]How to create multiple SQlite tables using JavaScript?

So far in my app development (phonegap/JQuery Mobile) I have setup a database and have a table created to which I have implemented a few functions that interact with the table for different events eg user registration. 到目前为止,在我的应用程序开发(phonegap / JQuery Mobile)中,我已经建立了一个数据库并创建了一个表,并针对该表实现了一些功能,这些功能可以针对不同事件(例如用户注册)与该表进行交互。

However, I need to create another table within the same database however I'm not sure how to go about this. 但是,我需要在同一数据库中创建另一个表,但是我不确定如何执行此操作。

I attempted to created another function (createEvent(tx) {) that creates a table however that isn't working. 我试图创建另一个函数(createEvent(tx){),该函数创建一个表,但是不起作用。

Please see my javascript below which builds the database and creates the table with. 请参阅下面的我的JavaScript,它可以构建数据库并使用该表创建表。

  document.addEventListener("deviceready", onDeviceReady, false);

                     var db;
                     function onDeviceReady() {
                         db = window.openDatabase("SoccerEarth", "2.0", "SoccerEarthDB", 2*1024*1024);
                         db.transaction(createDB, createEvents, errorCB, successCB);

                     }
                     function createDB(tx) {
                         tx.executeSql('CREATE TABLE IF NOT EXISTS SoccerEarth (UserName text, FirstName text, LastName text, Email text, Password text, CPass text)');

                     }
                     function createEvents(tx) {
                     tx.execute2('CREATE TABLE IF NOT EXISTS SoccerEvents (Title text, Location text, NoPeople text, Date text, Description text)');
                                              }

                     function errorCB(err) {
                         alert("Error processing SQL: "+err.code);
                     }

                     function successCB() {
                  alert("Database Ready!");
                     }

I would do something like this, just for simplicity: 为了简单起见,我会这样做:

db.transaction(function (tx) {            
tx.executeSql('CREATE TABLE IF NOT EXISTS SoccerEarth (UserName text, FirstName text, LastName text, Email text, Password text, CPass text)');
tx.executeSql('CREATE TABLE IF NOT EXISTS SoccerEvents (Title text, Location text, NoPeople text, Date text, Description text)');
 }, errorCB, successCB);

You can pass the SQL statement as an array of string something. 您可以将SQL语句作为字符串数组进行传递。 and execute each statement in the array. 并执行数组中的每个语句。

var sql = [ "CREATE TABLE IF NOT EXISTS LEAD (ID VARCHAR(35) PRIMARY KEY NOT NULL, PROCESS VARCHAR (32), VERSION VARCHAR (8), CREATED_BY VARCHAR (128), CREATED_ON VARCHAR (32), RECIPIENT VARCHAR (32), STATUS VARCHAR (8))",
         "CREATE TABLE IF NOT EXISTS DOCUMENT (ID INTEGER PRIMARY KEY AUTOINCREMENT, LEAD_ID VARCHAR(35) REFERENCES LEAD (ID) NOT NULL, NAME VARCHAR (255) NOT NULL, TYPE VARCHAR (32) NOT NULL, CONTENT BLOB NOT NULL, RETRY INTEGER NOT NULL, STATUS VARCHAR (8))",
         "CREATE TABLE IF NOT EXISTS FUNDATA(ID INTEGER PRIMARY KEY AUTOINCREMENT, FUN_ID INTEGER REFERENCES LEAD (ID), NAME VARCHAR(128) NOT NULL, VALUE VARCHAR (255) NOT NULL)"  ];

now pass all the sql to function which would execute , also pass your database object . 现在将所有的sql传递给将要执行的函数,也传递您的数据库对象。 Callback can be any other function. 回调可以是任何其他函数。

function write(database, sql){
    this.sql = sql;
    database.transaction(
      function(tx){
      for(var i=0; i<this.sql.length; i++){
      console.log("execute sql : " + this.sql[i]);
      tx.executeSql(this.sql[i]);
    }    
      },function(error){
    console.log("error call back : " + JSON.stringify(error));
    console.log(error);
  },
     function(){
    console.log("transaction complete call back ");
  }
    );  
  }

Hope it provides a better way of doing things and making the code more robust .You could also execute Insert statement like this. 希望它提供了一种更好的处理方式并使代码更健壮。您还可以像这样执行Insert语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM