在我的android活动中,我正在尝试从现有数据库中检索数据。我在资产中复制了数据库并试图访问它,但是在编译表列时显示错误,表示不存在这样的表。我也尝试了一些教程,但没有一个有用。我该怎么办?
以下是导致问题的代码。
DataBase Adapter类:
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输出:
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
答案 0 :(得分:2)
http://www.youtube.com/watch?v=A2NuI31Idls
这可能是有用的这是第111部分到124这么棒的教程我强烈建议你看它
答案 1 :(得分:0)
您没有创建表格。您在Database Adapter类中使用以下代码。
@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);
编辑:
您可以在数据库适配器中更改open方法
public ToursOpenHelper open() throws SQLException{
sqLiteHelper = new SQLiteHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
sqlitedb = dbhelper.getWritableDatabase();
return this;
}