Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename table in sqlite

Tags:

android

sqlite

I am trying to rename a table in my app's sqlite database. For that I am using the following command:

ourDatabase.rawQuery("ALTER TABLE " + oldName + " RENAME TO " + newName,
                null);

where oldName is old name of the table, newName is the new name of table and ourDatabase is an instance of SQLiteDatabase. But this is not working.

What's the mistake ?

Thanks.

like image 703
Rajat Avatar asked Dec 14 '13 09:12

Rajat


People also ask

How do I alter a table in SQLite?

Summary. Use the ALTER TABLE statement to modify the structure of an existing table. Use ALTER TABLE table_name RENAME TO new_name statement to rename a table. Use ALTER TABLE table_name ADD COLUMN column_definition statement to add a column to a table.

How do I rename a SQLite database?

The rename table option allows the user to type in a new name for the table being renamed. The tool then generates and can execute the SQL to rename the SQLite database table.

How do I edit a SQLite database?

Introduction to SQLite UPDATE statement In this syntax: First, specify the table where you want to update after the UPDATE clause. Second, set new value for each column of the table in the SET clause. Third, specify rows to update using a condition in the WHERE clause.


2 Answers

Try execSQL instead of rawQuery like:

ourDatabase.execSQL("ALTER TABLE " + original_table_name + " RENAME TO " + new_table_name);
like image 93
Shailendra Madda Avatar answered Oct 23 '22 10:10

Shailendra Madda


Try this code:

    db.beginTransaction();
    try{
        db.execSQL("ALTER TABLE " + oldName + " RENAME TO " + newName+";");
        db.setTransactionSuccessful();
    } finally{
        db.endTransaction();
    }
like image 7
Vladislav Avatar answered Oct 23 '22 10:10

Vladislav