Assuming that you want to update the TableC table, the recommended practice is to:
1) Rename the TableC to Tablec_temp
The SQL statement can be written like this: ALERT TABLE TableC RENAME to Tablec_temp;
2) Create a new TableC table
3) inserting data from the tablec_temp into the TableC table
SQL statements can be written like this: INSERT into TableC (Col1, Col2, Col3) SELECT (Col1, Col2, Col3) from Tablec_temp;
After these three steps, the TableC has completed the update, and the data in the original table has been preserved.
Attention:
In the Onupgrade () method, when you delete a table, pay attention to using transactions so that the modifications can immediately react to the database file.
protected voidupgradetables (sqlitedatabase db, String tableName, string columns) {Try{db.begintransaction (); //1, Rename table. String temptablename = tableName + "_temp"; String SQL= "ALTER TABLE" + tableName + "RENAME to" +Temptablename; EXECSQL (DB, SQL,NULL); //2, Create table. oncreatetable (DB); //3, Load datasql = "INSERT into" + TableName + "(" + Columns + ")" + "select" + Columns + "FR OM "+Temptablename; EXECSQL (DB, SQL,NULL); //4, Drop the temporary table. Execsql (db, "DROP TABLE IF EXISTS" + temptablename,NULL); Db.settransactionsuccessful (); } Catch(SQLException e) {e.printstacktrace (); } Catch(Exception e) {e.printstacktrace (); } finally{db.endtransaction (); } }
Android Database upgrade, data not lost solution