http://www.youtube.com/watch?v=A2NuI31Idls
may this is helpful this is part 111 till 124 so awesome tutorials I strongly recommend to you watch it
Question
In my android activity, I am trying to retrieve data from my existing database. I copied the DB in assets and tried to access it but it is showing error an that no such table exists while compiling the table columns. I tried some tutorials also but none of them worked. What should I do?
Below is the code which is causing problem.
DataBase Adapter class:
public static final String DATABASE_NAME="Wetrip";
public static final String DATABASE_TABLE="tour";
public static final int DATABASE_VERSION=1;
public final String TAG = "DBAdapter";
public static final String KEY_ID = "_id";
public static final String KEY_TOUR = "tour_name";
public static final String KEY_DAYS = "days";
public static final String KEY_NIGHTS = "nights";
final Context ctx;
DataBaseHelper dbhelper;
SQLiteDatabase sqlitedb;
public ToursOpenHelper(Context context) {
this.ctx = context;
dbhelper = new DataBaseHelper(context);
}
private static class DataBaseHelper extends SQLiteOpenHelper{
DataBaseHelper(Context ctxt) {
// TODO Auto-generated constructor stub
super(ctxt, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
//--open the DB
public ToursOpenHelper open() throws SQLException{
sqlitedb = dbhelper.getWritableDatabase();
return this;
}
public void close() {
dbhelper.close();
}
public Cursor getAllData() {
return sqlitedb.query(DATABASE_TABLE, new String[] {KEY_ID,KEY_TOUR,KEY_DAYS,KEY_NIGHTS}, null,null,null,null,null);
}
DatabaseActivity:
ToursOpenHelper toh = new ToursOpenHelper(this);
try{
String destPath = "/data/data/"+getPackageName()+"/databases";
File f = new File(destPath);
if(!f.exists()){
f.mkdirs();
f.createNewFile();
//--copy db from assets
CopyDB(getBaseContext().getAssets().open("wetrip"), new FileOutputStream(destPath+"/Wetrip"));
}
}
catch(FileNotFoundException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
//--get all data
toh.open();
Cursor c = toh.getAllData();
if(c.moveToFirst()){
do{
displayContact(c);
}while(c.moveToNext());
}
toh.close();
}
public void CopyDB(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int length;
while((length = in.read(buffer)) >0){
out.write(buffer,0,length);
}
in.close();
out.close();
}
public void displayContact(Cursor cursor) {
Toast.makeText(this, "id: "+cursor.getString(0)+"\n"+"Name: "+cursor.getString(2)+"\n"+"Days: "+cursor.getString(4)+"\n"+"Nights: "+cursor.getString(3)+"\n", Toast.LENGTH_LONG).show();
}
Logcat Output:
08-10 14:19:41.423: E/AndroidRuntime(12764): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.travelplanner/com.example.travelplanner.DatabaseActivity}: android.database.sqlite.SQLiteException: no such table: tour: , while compiling: SELECT _id, tour_name, days, nights FROM tour
08-10 14:19:41.423: E/AndroidRuntime(12764): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
08-10 14:19:41.423: E/AndroidRuntime(12764): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
08-10 14:19:41.423: E/AndroidRuntime(12764): at android.app.ActivityThread.access$600(ActivityThread.java:127)
08-10 14:19:41.423: E/AndroidRuntime(12764): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
08-10 14:19:41.423: E/AndroidRuntime(12764): at android.os.Handler.dispatchMessage(Handler.java:99)
08-10 14:19:41.423: E/AndroidRuntime(12764): at android.os.Looper.loop(Looper.java:137)
08-10 14:19:41.423: E/AndroidRuntime(12764): at android.app.ActivityThread.main(ActivityThread.java:4448)
08-10 14:19:41.423: E/AndroidRuntime(12764): at java.lang.reflect.Method.invokeNative(Native Method)
08-10 14:19:41.423: E/AndroidRuntime(12764): at java.lang.reflect.Method.invoke(Method.java:511)
08-10 14:19:41.423: E/AndroidRuntime(12764): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
08-10 14:19:41.423: E/AndroidRuntime(12764): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
08-10 14:19:41.423: E/AndroidRuntime(12764): at dalvik.system.NativeStart.main(Native Method)
08-10 14:19:41.423: E/AndroidRuntime(12764): Caused by: android.database.sqlite.SQLiteException: no such table: tour: , while compiling: SELECT _id, tour_name, days, nights FROM tour
08-10 14:19:41.423: E/AndroidRuntime(12764): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
08-10 14:19:41.423: E/AndroidRuntime(12764): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
08-10 14:19:41.423: E/AndroidRuntime(12764): at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
08-10 14:19:41.423: E/AndroidRuntime(12764): at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
08-10 14:19:41.423: E/AndroidRuntime(12764): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:127)
08-10 14:19:41.423: E/AndroidRuntime(12764): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:94)
08-10 14:19:41.423: E/AndroidRuntime(12764): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:53)
08-10 14:19:41.423: E/AndroidRuntime(12764): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
08-10 14:19:41.423: E/AndroidRuntime(12764): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1570)
08-10 14:19:41.423: E/AndroidRuntime(12764): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1453)
08-10 14:19:41.423: E/AndroidRuntime(12764): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1409)
08-10 14:19:41.423: E/AndroidRuntime(12764): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1489)
08-10 14:19:41.423: E/AndroidRuntime(12764): at com.example.travelplanner.ToursOpenHelper.getAllData(ToursOpenHelper.java:59)
08-10 14:19:41.423: E/AndroidRuntime(12764): at com.example.travelplanner.DatabaseActivity.onCreate(DatabaseActivity.java:46)
08-10 14:19:41.423: E/AndroidRuntime(12764): at android.app.Activity.performCreate(Activity.java:4465)
08-10 14:19:41.423: E/AndroidRuntime(12764): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-10 14:19:41.423: E/AndroidRuntime(12764): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
08-10 14:19:41.423: E/AndroidRuntime(12764): ... 11 more
Solution
http://www.youtube.com/watch?v=A2NuI31Idls
may this is helpful this is part 111 till 124 so awesome tutorials I strongly recommend to you watch it
OTHER TIPS
You didn't create your table. You use following codes in Database Adapter class.
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " KEY_TOUR + " TEXT NOT NULL, " + KEY_DAYS + " TEXT NOT NULL, " + KEY_NIGHTS + " TEXT NOT NULL);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion <= newVersion)
return;
db.execSQL("DROP TABLE IF EXISTS " + "CREATE TABLE " + DATABASE_TABLE + " (" KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " KEY_TOUR + " TEXT NOT NULL, " + KEY_DAYS + " TEXT NOT NULL, " + KEY_NIGHTS + " TEXT NOT NULL);");
onCreate(db);
EDIT :
You can change your open method in database adapter
public ToursOpenHelper open() throws SQLException{
sqLiteHelper = new SQLiteHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
sqlitedb = dbhelper.getWritableDatabase();
return this;
}