首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用SQLite在Android上返回“无此类表异常”

使用SQLite在Android上返回“无此类表异常”
EN

Stack Overflow用户
提问于 2011-07-25 05:47:07
回答 2查看 1.7K关注 0票数 0

在我的应用程序中,我使用了一个现有的sqlite数据库,我需要列出数据库中所有表的记录。里面有六张桌子。

我的sqlite数据库文件名是:festival.sqlite,它存储在我的资产文件夹中。

为了复制和列出记录,我使用以下代码。但是在运行这段代码时,我没有得到这样的表异常。我知道在sqlite里有桌子。

如何从桌子上获取记录。请帮帮我。我的代码:

代码语言:javascript
复制
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){
        //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) {
}    
}

在另一个活动中,我调用以下方法:

代码语言:javascript
复制
             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());   
    }

我的原木猫:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-07-25 05:59:00

如果您还没有添加android_metadata表,那么只需添加这个并完成它就可以了。

关于更多信息,这是一个非常好的教程。

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

票数 0
EN

Stack Overflow用户

发布于 2011-07-25 06:05:38

请参阅下面的链接,以从资产复制数据库。也可以用大小写来检查表名。

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6812187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档