Rethinavel Pillai Rethinavel Pillai - 26 days ago 8
Android Question

How to copy all the record from one table to the another table in the same database in android Sqlite

I just want to copy the table from table1 to table2 in the same database. I surfed around and i got some errors. Here is my source code :

I followed this link : Copy table

public class SelectedCandidatesDB {

public static final String KEY_NAME1 = "name1";
public static final String KEY_NAME2 = "name2";
public static final String KEY_NAME3 = "name3";
public static final String KEY_NAME4 = "name4";
public static final String KEY_NAME5 = "name5";
public static final String KEY_ID = "_id";
private final Context mCtx;
private static final String TAG = "CountriesDbAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;

// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "selectedcandidates.db";
// Contacts table name
private static final String TABLE_SELECTED_CAND = "selectedcandids";
private static final String TABLE_RECOUNT1 = "rec1";
private static final String TABLE_RECOUNT2 = "recountedsecond";
private static final String TABLE_RECOUNT3 = "recountedthird";

private static final String CREATE_SELECTED_CAND_TABLE = " CREATE TABLE if not exists "
+ TABLE_SELECTED_CAND
+ "("
+ KEY_ID
+ " integer PRIMARY KEY autoincrement,"
+ KEY_NAME1
+ " TEXT,"
+ KEY_NAME2
+ " TEXT,"
+ KEY_NAME3
+ " TEXT,"
+ KEY_NAME4
+ " TEXT," + KEY_NAME5 + " TEXT" + ")";

private static final String CREATE_SELECTED_CAND_RECOUNT1 = " CREATE TABLE if not exists "
+ TABLE_RECOUNT1
+ "("
+ KEY_ID
+ " integer PRIMARY KEY autoincrement,"
+ KEY_NAME1
+ " TEXT,"
+ KEY_NAME2
+ " TEXT,"
+ KEY_NAME3
+ " TEXT,"
+ KEY_NAME4
+ " TEXT," + KEY_NAME5 + " TEXT" + ")";
private static final String CREATE_SELECTED_CAND_RECOUNT2 = " CREATE TABLE if not exists "
+ TABLE_RECOUNT2
+ "("
+ KEY_ID
+ " integer PRIMARY KEY autoincrement,"
+ KEY_NAME1
+ " TEXT,"
+ KEY_NAME2
+ " TEXT,"
+ KEY_NAME3
+ " TEXT,"
+ KEY_NAME4
+ " TEXT," + KEY_NAME5 + " TEXT" + ")";
private static final String CREATE_SELECTED_CAND_RECOUNT3 = " CREATE TABLE if not exists "
+ TABLE_RECOUNT3
+ "("
+ KEY_ID
+ " integer PRIMARY KEY autoincrement,"
+ KEY_NAME1
+ " TEXT,"
+ KEY_NAME2
+ " TEXT,"
+ KEY_NAME3
+ " TEXT,"
+ KEY_NAME4
+ " TEXT," + KEY_NAME5 + " TEXT" + ")";

private static class DatabaseHelper extends SQLiteOpenHelper {

DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

db.execSQL(CREATE_SELECTED_CAND_RECOUNT1);
db.execSQL(CREATE_SELECTED_CAND_TABLE);

Log.d("Selected Candidate DB ",
"The Table recount 1 has been created :");
db.execSQL(CREATE_SELECTED_CAND_RECOUNT2);
db.execSQL(CREATE_SELECTED_CAND_RECOUNT3);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + CREATE_SELECTED_CAND_RECOUNT1);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_SELECTED_CAND);

db.execSQL("DROP TABLE IF EXISTS " + CREATE_SELECTED_CAND_RECOUNT2);
db.execSQL("DROP TABLE IF EXISTS " + CREATE_SELECTED_CAND_RECOUNT3);
onCreate(db);
}
}

public SelectedCandidatesDB(Context ctx) {
this.mCtx = ctx;
}

public SelectedCandidatesDB open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}

public void close() {
if (mDbHelper != null) {
mDbHelper.close();
}
}


// **************


public void copyToRecount1() {

mDb.execSQL(" INSERT INTO " + TABLE_RECOUNT1 + " SELECT * FROM "
+ TABLE_SELECTED_CAND);
}


and the logs are :

I/SqliteDatabaseCpp(12637): sqlite returned: error code = 1, msg = no
such table: rec1, db=xxx D/AndroidRuntime(12637): Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0x40c501f8)
E/AndroidRuntime(12637):





FATAL EXCEPTION: main
E/AndroidRuntime(12637): android.database.sqlite.SQLiteException: ***no such table: rec1: , while compiling: INSERT INTO rec1 SELECT * FROM selectedcandids***
E/AndroidRuntime(12637): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
E/AndroidRuntime(12637): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
E/AndroidRuntime(12637): at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
E/AndroidRuntime(12637): at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
E/AndroidRuntime(12637): at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:260)
E/AndroidRuntime(12637): at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:84)
E/AndroidRuntime(12637): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:2028)
E/AndroidRuntime(12637): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1968)
E/AndroidRuntime(12637): at com.dana.db.SelectedCandidatesDB.copyToRecount1(SelectedCandidatesDB.java:264)
E/AndroidRuntime(12637): at com.dana.votecount.VoteRecount$2$1.onClick(VoteRecount.java:91)
E/AndroidRuntime(12637): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)

Answer

Error says you don't have table 'rec1'. Maybe you added this table later after creating the database. Uninstall your app and run again.It will create db and all the table again.