Issue
My code
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
if (oldVersion < 15) {
String sql = "CREATE TEMPORARY TABLE temp_table(_id INTEGER UNIQUE, vendor_code TEXT UNIQUE, name TEXT, amount DOUBLE, price DOUBLE, unit TEXT, description TEXT, 'group' INTEGER); " +
"INSERT INTO temp_table(_id, name, amount, price, unit, description) " +
"SELECT _id, name, amount, price, unit, description FROM products_table; " +
"DROP TABLE products_table; " +
"CREATE TABLE products_table(_id INTEGER UNIQUE, vendor_code TEXT UNIQUE, name TEXT, amount DOUBLE, price DOUBLE, unit TEXT, description TEXT, 'group' INTEGER); " +
"INSERT INTO products_table SELECT _id, vendor_code, name, amount, price, unit, description, 'group' FROM temp_table; " +
"DROP TABLE temp_table; "
database.execSQL(sql);
}
}
The code is working but it doesn't create errors and doesn't alter the table. Although in DB Browser for SQLite it works right. Help me pls
Solution
For execSQL()
from: https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase#execSQL(java.lang.String)
Multiple statements separated by semicolons are not supported.
You are trying to execute multiple statements separated by semicolons in a single execSql()
call.
Split the sql statement in 6 or more if needed parts and execute each with a separate execSql()
call.
Answered By - forpas
Answer Checked By - David Goodson (JavaFixing Volunteer)