Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with WebSQL. How to add new column to existing table?

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.

like image 325
sergey_c Avatar asked Jun 05 '13 07:06

sergey_c


People also ask

How do I add a column to an existing table?

In Object Explorer, right-click the table to which you want to add columns and choose Design. Select the first blank cell in the Column Name column. Type the column name in the cell. The column name is a required value.

How do you add a new column to an existing column in SQL?

In Microsoft SQL Server, we can change the order of the columns and can add a new column by using ALTER command. ALTER TABLE is used to add, delete/drop or modify columns in the existing table. It is also used to add and drop various constraints on the existing table.

How do I add a column to an EMP table?

If we want to add multiple columns to the existing table using any single statement, we can use the below syntax: ALTER TABLE table_name (Name of the table) ADD [COLUMN] column_definition, (for adding column)

How do I add a column to an existing table in MySQL?

The syntax to add a column in a table in MySQL (using the ALTER TABLE statement) is: ALTER TABLE table_name ADD new_column_name column_definition [ FIRST | AFTER column_name ]; table_name. The name of the table to modify.


1 Answers

The correct Request is:

ALTER TABLE mytable ADD time VARCHAR NOT NULL DEFAULT '' 

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

like image 65
sergey_c Avatar answered Sep 28 '22 09:09

sergey_c