开发者

"no such table exception" Returned on Android using SQLite

in my app i use Using an existing sqlite database and i need to list out all records of the tables in the database. i have six tables in it.

my sqlite database file name is : festival.sqlite which stored in my assets folder.

for copying and list out the records i use the following code. but i get no such table exception while running this code. i know in the sqlite file have tables.

how to get records from tables. please help me. my code:

public class main extends SQLiteOpenHelper {

//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/com.sql.test/databases/";
private static String DB_NAME = "festival";
private SQLiteDatabase myDataBase; 
private final Context myContext;

public main(Context context) {

    super(context, DB_NAME, null, 1);
    this.myContext = context;
}

public void createDataBase() throws IOException{
    boolean dbExist = checkDataBase();
    if(dbExist){
    开发者_Go百科    //do nothing - database already exist
        Log.e("database", "database exist");
    }else{
        Log.e("database","not exist");
        this.getReadableDatabase().close();
        try {
            copyDataBase();

        } catch (IOException e) {
            //throw new Error("Error copying database");
            Log.e("exception-1",e.getMessage());
            Log.e("exception-1", "Error copying database");
        }
    }
}
private boolean checkDataBase(){
    SQLiteDatabase checkDB = null;
    try{
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
    }catch(SQLiteException e){
    }
    if(checkDB != null){
        checkDB.close();
    }
    return checkDB != null ? true : false;
}

private void copyDataBase() throws IOException{
    InputStream myInput = myContext.getAssets().open(DB_NAME);
    String outFileName = DB_PATH + DB_NAME;
    OutputStream myOutput = new FileOutputStream(outFileName);
    byte[] buffer = new byte[618496];
    int length;
    while ((length = myInput.read(buffer))>0){
        myOutput.write(buffer, 0, length);
    }
    //Close the streams
    myOutput.flush();
    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_READWRITE);
    myDataBase.setVersion(1);
    myDataBase.setLocale(Locale.getDefault());
    myDataBase.setLockingEnabled(true);
    try{
        Cursor cx = myDataBase.rawQuery("SELECT * FROM events"  , null);
        if (cx != null ) {
            if  (cx.moveToFirst()) {
              do {
                Log.e("ID",String.valueOf(cx.getInt(0)));
              }while (cx.moveToNext());
            }
        }
    myDataBase.close(); 
    }
    catch(Exception e){
        Log.e("exception-table", e.getMessage());
    }


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

}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}    
}

in another activity i call the following methods:

             main myDbHelper = new main(this);
    try {
        myDbHelper.createDataBase();
    } catch (IOException ioe) {
        Log.e("ex-2",ioe.getMessage());
     // throw new Error("Unable to create database");
    }


    try {
        myDbHelper.openDataBase();
    }catch(SQLException sqle){
     // throw sqle;
    Log.e("exception",sqle.getMessage());   
    }

my log cat:

07-25 10:33:30.748: ERROR/Database(823): sqlite3_open_v2("/data/data/com.sql.test/databases/festival", &handle, 1, NULL) failed
07-25 10:33:30.748: ERROR/database(823): not exist
07-25 10:33:30.917: ERROR/exception-1(823): festival
07-25 10:33:30.917: ERROR/exception-1(823): Error copying database
07-25 10:33:30.998: ERROR/exception-table(823): no such table: events: , while compiling: SELECT * FROM events


if you have not added android_metadata table then just add this and complie it will work.

for more information this is very good tutorial

http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/


see the following link to copy database from assets. Also check for the table name with case sencitive

http://huuah.com/using-sqlite-from-your-android-app/ or

http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/comment-page-1/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜