最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Working with WebSQL. How to add new column to existing table? - Stack Overflow

programmeradmin3浏览0评论

I have simple WebSQL database with 1 table and 3 columns. I would like to add one more column, but I can't do it if the database already exist. It's only possible after I have cleaned it in the cache, but then I'm loosing all the data.

How can I add new column to a table without removing the database?

// database creating
MYDB.init.open = function(){
       MYDB.init.db = openDatabase("MYDB","1.0"," super-data-base",1024*1024*5);
}


//table creating    
MYDB.init.createTable = function(){
           var database = MYDB.init.db;
           database.transaction(function(tx){
                  tx.executeSql("CREATE TABLE IF NOT EXISTS mytable (ID INTEGER PRIMARY KEY ASC,item TEXT,description TEXT)", []);
           });
    }

//
//a lot of code for adding datas and reading datas
//

//database updating that does not work
MYDB.init.updateTable = function(){
       var database = MYDB.init.db;
       database.transaction(function(tx){
              tx.executeSql("ALTER TABLE mytable ADD time VARCHAR NOT NULL BEFORE description");
       });
}

All other UPDATE functions are working well.

I have simple WebSQL database with 1 table and 3 columns. I would like to add one more column, but I can't do it if the database already exist. It's only possible after I have cleaned it in the cache, but then I'm loosing all the data.

How can I add new column to a table without removing the database?

// database creating
MYDB.init.open = function(){
       MYDB.init.db = openDatabase("MYDB","1.0"," super-data-base",1024*1024*5);
}


//table creating    
MYDB.init.createTable = function(){
           var database = MYDB.init.db;
           database.transaction(function(tx){
                  tx.executeSql("CREATE TABLE IF NOT EXISTS mytable (ID INTEGER PRIMARY KEY ASC,item TEXT,description TEXT)", []);
           });
    }

//
//a lot of code for adding datas and reading datas
//

//database updating that does not work
MYDB.init.updateTable = function(){
       var database = MYDB.init.db;
       database.transaction(function(tx){
              tx.executeSql("ALTER TABLE mytable ADD time VARCHAR NOT NULL BEFORE description");
       });
}

All other UPDATE functions are working well.

Share Improve this question edited Jun 5, 2013 at 8:31 Andreas 6,4752 gold badges39 silver badges47 bronze badges asked Jun 5, 2013 at 7:32 sergey_csergey_c 7619 silver badges15 bronze badges 2
  • have you found a solution to this i am working on the exact same thing – Prakash Chennupati Commented Dec 5, 2013 at 18:58
  • Yes, this is a correct Request ALTER TABLE mytable ADD time VARCHAR NOT NULL DEFAULT '' (just don't use BEFORE, AFTER, because sqlite-based bases does not support this syntax) Let me know about a result! – sergey_c Commented Dec 12, 2013 at 8:17
Add a ment  | 

1 Answer 1

Reset to default 6

The correct Request is:

ALTER TABLE mytable ADD time VARCHAR NOT NULL DEFAULT '' 

(without BEFORE, AFTER, because WebSQL does't support this syntax)

发布评论

评论列表(0)

  1. 暂无评论