0

我有一个数据库,我将它导入我的项目(android 移动应用程序)它可以工作并从表中检索数据,但是当我通过 sqlite 数据库浏览器添加表或行来修改数据库时,我会清理项目并导入新数据库并从我的设备上卸载项目并再次运行它会强制停止应用程序,并在日志部分告诉它没有这样的表..

即使我创建新数据库并将其导入项目,它也无法正常工作。

10-11 13:55:03.152: E/SQLiteLog(10632): (1) no such table: facilities
10-11 13:55:03.162: D/AndroidRuntime(10632): Shutting down VM
10-11 13:55:03.162: W/dalvikvm(10632): threadid=1: thread exiting with uncaught exception (group=0x419b6700)
10-11 13:55:03.182: E/AndroidRuntime(10632): FATAL EXCEPTION: main
10-11 13:55:03.182: E/AndroidRuntime(10632): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.buttontest/com.example.buttontest.WhereToGo}: android.database.sqlite.SQLiteException: no such table: facilities (code 1): , while compiling: SELECT category as '_id' FROM facilities
10-11 13:55:03.182: E/AndroidRuntime(10632):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-11 13:55:03.182: E/AndroidRuntime(10632):    at android.app.ActivityThread.startActivityNow(ActivityThread.java:2054)
10-11 13:55:03.182: E/AndroidRuntime(10632):    at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
10-11 13:55:03.182: E/AndroidRuntime(10632):    at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
10-11 13:55:03.182: E/AndroidRuntime(10632):    at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
10-11 13:55:03.182: E/AndroidRuntime(10632):    at android.widget.TabHost.setCurrentTab(TabHost.java:413)
10-11 13:55:03.182: E/AndroidRuntime(10632):    at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:154)
10-11 13:55:03.182: E/AndroidRuntime(10632):    at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:546)
10-11 13:55:03.182: E/AndroidRuntime(10632):    at android.view.View.performClick(View.java:4240)
10-11 13:55:03.182: E/AndroidRuntime(10632):    at android.view.View$PerformClick.run(View.java:17721)
10-11 13:55:03.182: E/AndroidRuntime(10632):    at android.os.Handler.handleCallback(Handler.java:730)
10-11 13:55:03.182: E/AndroidRuntime(10632):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-11 13:55:03.182: E/AndroidRuntime(10632):    at android.os.Looper.loop(Looper.java:137)
10-11 13:55:03.182: E/AndroidRuntime(10632):    at android.app.ActivityThread.main(ActivityThread.java:5103)
10-11 13:55:03.182: E/AndroidRuntime(10632):    at java.lang.reflect.Method.invokeNative(Native Method)
10-11 13:55:03.182: E/AndroidRuntime(10632):    at java.lang.reflect.Method.invoke(Method.java:525)
10-11 13:55:03.182: E/AndroidRuntime(10632):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-11 13:55:03.182: E/AndroidRuntime(10632):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-11 13:55:03.182: E/AndroidRuntime(10632):    at dalvik.system.NativeStart.main(Native Method)
10-11 13:55:03.182: E/AndroidRuntime(10632): Caused by: android.database.sqlite.SQLiteException: no such table: facilities (code 1): , while compiling: SELECT category as '_id' FROM facilities
10-11 13:55:03.182: E/AndroidRuntime(10632):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
10-11 13:55:03.182: E/AndroidRuntime(10632):    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
10-11 13:55:03.182: E/AndroidRuntime(10632):    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
10-11 13:55:03.182: E/AndroidRuntime(10632):    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
10-11 13:55:03.182: E/AndroidRuntime(10632):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
10-11 13:55:03.182: E/AndroidRuntime(10632):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
10-11 13:55:03.182: E/AndroidRuntime(10632):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
10-11 13:55:03.182: E/AndroidRuntime(10632):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
10-11 13:55:03.182: E/AndroidRuntime(10632):    at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
10-11 13:55:03.182: E/AndroidRuntime(10632):    at com.example.buttontest.WhereToGo.listNotes(WhereToGo.java:34)
10-11 13:55:03.182: E/AndroidRuntime(10632):    at com.example.buttontest.WhereToGo.onStart(WhereToGo.java:28)
10-11 13:55:03.182: E/AndroidRuntime(10632):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
10-11 13:55:03.182: E/AndroidRuntime(10632):    at android.app.Activity.performStart(Activity.java:5143)
10-11 13:55:03.182: E/AndroidRuntime(10632):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
10-11 13:55:03.182: E/AndroidRuntime(10632):    ... 18 more
10-11 13:55:04.832: I/Process(10632): Sending signal. PID: 10632 SIG: 9

这是我的代码:

数据库助手.java

public class DataBaseHelper extends SQLiteOpenHelper
{ 
private static String TAG = "DataBaseHelper"; // Tag just for the LogCat window 
//destination path (location) of our database on device 
private static String DB_PATH = "";  
private static String DB_NAME ="btm_";// Database name 
private SQLiteDatabase mDataBase;  
private final Context mContext;

public DataBaseHelper(Context context)  
{ 
super(context, DB_NAME, null, 1);// 1? its Database Version 
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/"; 
this.mContext = context; 
}    

public void createDataBase() throws IOException 
{ 
//If database not exists copy it from the assets 

boolean mDataBaseExist = checkDataBase(); 
if(!mDataBaseExist) 
{ 
    SQLiteDatabase db = this.getReadableDatabase();
    if (db.isOpen()){
        db.close();
    }
    this.close(); 
    try  
    { 
        //Copy the database from assests 
        copyDataBase(); 
        Log.e(TAG, "createDatabase database created"); 
    }  
    catch (IOException mIOException)  
    { 
        throw new Error("ErrorCopyingDataBase"); 
    } 
    } 
    } 
    //Check that the database exists here: /data/data/your package/databases/Da Name 
    private boolean checkDataBase() 
    { 
    File dbFile = new File(DB_PATH + DB_NAME); 
    //Log.v("dbFile", dbFile + "   "+ dbFile.exists()); 
    return dbFile.exists(); 
    } 

    //Copy the database from assets 
    private void copyDataBase() throws IOException 
    { 
    InputStream mInput = mContext.getAssets().open(DB_NAME); 
    String outFileName = DB_PATH + DB_NAME; 
    OutputStream mOutput = new FileOutputStream(outFileName); 
    byte[] mBuffer = new byte[1024]; 
    int mLength; 
    while ((mLength = mInput.read(mBuffer))>0) 
    { 
        mOutput.write(mBuffer, 0, mLength); 
    } 
    mOutput.flush(); 
    mOutput.close(); 
    mInput.close(); 
    } 

    //Open the database, so we can query it 
    public boolean openDataBase() throws SQLException 
    { 
    String mPath = DB_PATH + DB_NAME; 
    //Log.v("mPath", mPath); 
    mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY); 
    return mDataBase != null; 
    } 

    @Override 
    public synchronized void close()  
    { 
    if(mDataBase != null) 
        mDataBase.close(); 
        super.close(); 
    }
    public Cursor getData() {  
      String myPath = DB_PATH + DB_NAME;  
      mDataBase = SQLiteDatabase.openDatabase(myPath, null,  
        SQLiteDatabase.OPEN_READONLY);  
      Cursor c = mDataBase.rawQuery("SELECT * FROM category", null);  

      return c;  
     }  
    public Cursor getFacilityData() {  
      String myPath = DB_PATH + DB_NAME;  
      mDataBase = SQLiteDatabase.openDatabase(myPath, null,  
        SQLiteDatabase.OPEN_READONLY);  
      Cursor c = mDataBase.rawQuery("SELECT _id,name FROM facilities", null);  

      return c;  
     }  
@Override
public void onCreate(SQLiteDatabase arg0) {
    // TODO Auto-generated method stub

}

And Here is new class with calling the database code:
private void listNotes(){
     SQLiteDatabase db = db_con.getWritableDatabase();
     try {
         Cursor c = db.rawQuery("SELECT category as '_id' " +
                 "FROM facilities ",null );
         final ListAdapter noteAdapter = new
SimpleCursorAdapter(this,android.R.layout.simple_list_item_2, c,new String[] {"_id"},new int[]
{android.R.id.text1});
         this.setListAdapter(noteAdapter);
     } finally {
     db.close();
     }
    }
4

2 回答 2

1

数据库助手.java

public class DataBaseHelper extends SQLiteOpenHelper { 
    private static String TAG = "DataBaseHelper"; // Tag just for the LogCat window 
    //destination path (location) of our database on device 
    private static String DB_PATH = "";  
    private static String DB_NAME ="btm_";// Database name 
    private SQLiteDatabase mDataBase;  
    private final Context mContext;

    public DataBaseHelper(Context context) { 
        super(context, DB_NAME, null, 1);// 1? its Database Version 
        DB_PATH = "/data/data/" + context.getPackageName() + "/databases/"; 
        this.mContext = context; 
    }    

    public void createDataBase() throws IOException { 
        //If database not exists copy it from the assets 

        boolean mDataBaseExist = checkDataBase(); 
        if(!mDataBaseExist) { 
            SQLiteDatabase db = this.getReadableDatabase();
            if (db.isOpen()) {
                db.close();
            }
            this.close(); 
            try { 
                //Copy the database from assests 
                copyDataBase(); 
                Log.e(TAG, "createDatabase database created"); 
            } catch (IOException mIOException) { 
                throw new Error("ErrorCopyingDataBase"); 
            } 
        } 
    } 

    //Check that the database exists here: /data/data/your package/databases/Da Name 
    private boolean checkDataBase() { 
        File dbFile = new File(DB_PATH + DB_NAME); 
        //Log.v("dbFile", dbFile + "   "+ dbFile.exists()); 
        return dbFile.exists(); 
    } 

    //Copy the database from assets 
    private void copyDataBase() throws IOException { 
        InputStream mInput = mContext.getAssets().open(DB_NAME); 
        String outFileName = DB_PATH + DB_NAME; 
        OutputStream mOutput = new FileOutputStream(outFileName); 
        byte[] mBuffer = new byte[1024]; 
        int mLength; 
        while ((mLength = mInput.read(mBuffer))>0) { 
            mOutput.write(mBuffer, 0, mLength); 
        } 
        mOutput.flush(); 
        mOutput.close(); 
        mInput.close(); 
    } 

    //Open the database, so we can query it 
    public boolean openDataBase() throws SQLException { 
        String mPath = DB_PATH + DB_NAME; 
        //Log.v("mPath", mPath); 
        mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY); 
        return mDataBase != null; 
    } 

    @Override 
    public synchronized void close() { 
        if(mDataBase != null) 
            mDataBase.close(); 
        super.close(); 
    }

    public Cursor getData() {  
        String myPath = DB_PATH + DB_NAME;  
        mDataBase = SQLiteDatabase.openDatabase(myPath, null,  
            SQLiteDatabase.OPEN_READONLY);  
        Cursor c = mDataBase.rawQuery("SELECT * FROM category", null);  

        return c;  
    }  

    public Cursor getFacilityData() {  
        String myPath = DB_PATH + DB_NAME;  
        mDataBase = SQLiteDatabase.openDatabase(myPath, null,  
            SQLiteDatabase.OPEN_READONLY);  
        Cursor c = mDataBase.rawQuery("SELECT _id,name FROM facilities", null);  

        return c;  
    }

    @Override
    public void onCreate(SQLiteDatabase arg0) {
        // TODO Auto-generated method stub
    }
}

这是调用数据库代码的新类:

private void listNotes() {
    SQLiteDatabase db = db_con.getWritableDatabase();
    try {
        Cursor c = db.rawQuery("SELECT category as '_id' " +
                 "FROM facilities ",null );
        final ListAdapter noteAdapter = new
SimpleCursorAdapter(this,android.R.layout.simple_list_item_2, c,new String[] {"_id"},new int[]
{android.R.id.text1});
        this.setListAdapter(noteAdapter);
    } finally {
        db.close();
    }
}
于 2013-10-13T08:25:25.650 回答
0

当您打开数据库时,会调用 db.onCreate()。因此,现在如果它找到具有匹配名称的数据库,它将不会调用更新表所需的方法。所以我通常做的是物理删除数据库或重命名助手上的数据库。这样,当您尝试打开数据库时,db.onCreate() 方法将找不到数据库并会创建一个。

于 2013-10-11T12:37:51.073 回答