user5752271
user5752271

Reputation:

Copying data from one table to another in android

Am trying to copy all the values from table 1(ITEM_DETAIL_TABLE) to table 2 (ITEM_REFRESH_TABLE ). Below in my method. But while compiling am getting Syntax error. I don't know what is the problem.

public void ResetDbValues(){
                SQLiteDatabase db = MmpDBHelper.this.getWritableDatabase();
                 String sql1 = "INSERT INTO" + ITEM_REFRESH_TABLE + "SELECT * FROM " + ITEM_DETAIL_TABLE;
                 try {
                      Log.i("sql1=", sql1);

                      db.execSQL(sql1);


                    } catch (Exception exe) {
                        exe.printStackTrace();
                        db.endTransaction();
                        Log.e("Insertion failed",
                                "Transaction failure when inserting itemdet data.");
                        this.closeDatabase();
                        Log.i("DB closed", "Database closed successfully.");
                        errCode = "Err-DB-06";
                        LogFileCreator.appendLog(errCode + " : " + exe.getMessage());
                    }

}

Upvotes: 2

Views: 2777

Answers (2)

Joachim Isaksson
Joachim Isaksson

Reputation: 180917

You're missing spaces in your generated SQL;

String sql1 = "INSERT INTO" + ITEM_REFRESH_TABLE + "SELECT * FROM " + 
               ITEM_DETAIL_TABLE;

...generates the SQL;

INSERT INTORefreshItemSELECT * FROM itemDetail
          ^^         ^^ note the missing spaces.

The corrected line should be;

String sql1 = "INSERT INTO " + ITEM_REFRESH_TABLE + " SELECT * FROM " + 
                       // ^ space here               ^ and here
               ITEM_DETAIL_TABLE;            

Upvotes: 5

nkit
nkit

Reputation: 156

use

String sql1 = "INSERT INTO " + ITEM_REFRESH_TABLE + " SELECT * FROM " + ITEM_DETAIL_TABLE;

space before and after table name ITEM_REFRESH_TABLE is missing

Upvotes: 2

Related Questions