2013-12-23 50 views
-2

沒有這樣的表我已經嘗試了很多時間,但無法得到如何得到這個問題,SQL Broswser一切正常,SQL查詢運行沒有得到但當運行的問題,比得到引起:android.database.sqlite.SQLiteException:沒有這樣的表在Android的sqlite

Caused by: android.database.sqlite.SQLiteException: no such table 

。請告訴,這裏發生了什麼事。

public class DatabaseHelper extends SQLiteOpenHelper { 
private Context mycontext; 

private String DB_PATH = "/data/data/com.tosts.yummi/databases/"; 
private static String DB_NAME = "hotss"; 
private static String DB_TABLE = "kathas"; 

public SQLiteDatabase myDataBase; 

private int id; 
private static final int DATABASE_VERSION = 3; 
private String TAG = "horror"; 

public DatabaseHelper(Context context) throws IOException { 
    super(context, DB_NAME, null, DATABASE_VERSION); 
    this.mycontext = context; 
    boolean dbexist = checkdatabase(); 
    if (dbexist) { 
     // System.out.println("Database exists"); 
     opendatabase(); 
    } else { 
     // System.out.println("Database doesn't exist"); 
     createdatabase(); 
    } 
} 

public void createdatabase() throws IOException { 
    boolean dbexist = checkdatabase(); 
    if (dbexist) { 
     System.out.println(" Database exists."); 
    } else { 
     this.getReadableDatabase(); 
     try { 
      copydatabase(); 
      //myDataBase.close(); 
     } catch (IOException e) { 
      throw new Error("Error copying database"); 
     } 
    } 
} 

private boolean checkdatabase() { 
    // SQLiteDatabase checkdb = null; 
    boolean checkdb = false; 
    try { 
     String myPath = DB_PATH + DB_NAME; 
     File dbfile = new File(myPath); 
     checkdb = SQLiteDatabase.openDatabase(myPath, null, 
       SQLiteDatabase.OPEN_READWRITE) != null; 
     checkdb = dbfile.exists(); 

    } catch (SQLiteException e) { 
     System.out.println("Database doesn't exist"); 
    } 

    return checkdb; 
} 

private void copydatabase() throws IOException { 
    // Open your local db as the input stream 
    InputStream myinput = mycontext.getAssets().open(DB_NAME); 
    String outfilename = DB_PATH + DB_NAME; 
    OutputStream myoutput = new FileOutputStream(outfilename); 
    Log.d(TAG, "COpt Test->"+"InputStream->"+myinput+"--?"+outfilename+"---"+myoutput); 
    byte[] buffer = new byte[1024];// transfer byte to inputfile to 
            // outputfile 
    int length; 
    while ((length = myinput.read(buffer)) > 0) { 
     myoutput.write(buffer, 0, length); 
    } 
    myoutput.flush();// Close the streams 
    myoutput.close(); 
    myinput.close(); 
} 

public void opendatabase() throws SQLException { 
    // Open the database 
    String mypath = DB_PATH + DB_NAME; 
    myDataBase = SQLiteDatabase.openDatabase(mypath, null, 
      SQLiteDatabase.OPEN_READONLY); 

} 

public synchronized void close() { 
    if (myDataBase != null) { 
     myDataBase.close(); 
    } 
    super.close(); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    //db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE); 
    //onCreate(db); 
} 

// This will return a cursor containing database records 
public Cursor data() { 

    Cursor c; 
    c = myDataBase.query(DB_TABLE, null, null, null, null, null, null); 
    return c; 
} 

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

} 

    public List<String> getAllKatha(int category){ 
     List<String> labels = new ArrayList<String>(); 
     //List<String> ids = new ArrayList<String>(); 
     // Select All Query 
     String selectQuery = "SELECT * FROM kathas where category =" + category; 
     Log.d(TAG, "--->>>"+selectQuery); 
     myDataBase = this.getReadableDatabase(); 
     Cursor cursor = myDataBase.rawQuery(selectQuery, null); 

     // looping through all rows and adding to list 
     if (cursor.moveToFirst()) { 
      do { 
       labels.add(cursor.getString(1)); 
       //ids.add(cursor.getString(0)); 
      } while (cursor.moveToNext()); 
     } 

     // closing connection 
     cursor.close(); 
     myDataBase.close(); 

     // returning lables 
     return labels; 
    } 

我越來越喜歡這個日誌上的貓:

12-23 19:37:51.503: E/AndroidRuntime(1527): Caused by: android.database.sqlite.SQLiteException: no such table: kathas (code 1): , while compiling: SELECT * FROM kathas where category =3 
12-23 19:37:51.503: E/AndroidRuntime(1527):  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
12-23 19:37:51.503: E/AndroidRuntime(1527):  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882) 
12-23 19:37:51.503: E/AndroidRuntime(1527):  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493) 
12-23 19:37:51.503: E/AndroidRuntime(1527):  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 
12-23 19:37:51.503: E/AndroidRuntime(1527):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58) 
12-23 19:37:51.503: E/AndroidRuntime(1527):  at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37) 
12-23 19:37:51.503: E/AndroidRuntime(1527):  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) 
12-23 19:37:51.503: E/AndroidRuntime(1527):  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314) 
12-23 19:37:51.503: E/AndroidRuntime(1527):  at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253) 
12-23 19:37:51.503: E/AndroidRuntime(1527):  at com.nepal.nepalisexstories.database.DatabaseHelper.getAllKatha(DatabaseHelper.java:139) 
12-23 19:37:51.503: E/AndroidRuntime(1527):  at com.nepal.nepalisexstories.DisplayTitle.onCreate(DisplayTitle.java:70) 

其實這樣做對MainActivity.java

titles = db.getAllKatha(category); ///(DisplayTitle.java:70) 

在此先感謝。

+0

在Databasehelper類中,我聲明有問題。 –

回答

0

錯誤是安靜的顯式,表「kathas」沒有被創建,這是因爲你沒有在SQLiteOpenHelper的「onCreate」方法,並且你用來創建數據庫的方法出了問題,這對於數據庫創建來說是一個糟糕的實現,你應該利用在SQLiteOpenHelper的onCreate方法中傳遞的SQLiteDatabase參數,同時我注意到你正在使用onUpgrade方法,它調用沒有任何東西的「onCreate」所以如果你更新了數據庫,實際上並沒有發生。

+0

是的,我知道,但已經創建了資產文件夾中的數據庫,我就像這樣:http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/ –

+0

這是一個可怕的實施先生,你應該堅持特別的Android標準,因爲該方法不允許你升級你的模式,看看你的帖子上的評論,人們把它帶來了...... –

+0

@PrAndroid閱讀我的答案 - 你明確地刪除表 - 當然,當你執行你的查詢時,它不會被發現 –

0

您的onCreate方法中沒有任何內容,但這並不是很糟糕,因爲您正在複製現有的數據庫。什麼是真正糟糕的是,在你的onUpgrade方法你執行:

DROP TABLE IF EXISTS kathas 

然後你打電話你onCreate它什麼都不做。

顯然,您的表沒有找到 - 因爲您明確地刪除它。

相關問題