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.
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.
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.
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)
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.
The correct Request is:
ALTER TABLE mytable ADD time VARCHAR NOT NULL DEFAULT ''
(without BEFORE, AFTER, because WebSQL does't support this syntax)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With