复制android studio资产中的sqlite无法正常工作 [英] Copy sqlite in android studio assets not working

查看:84
本文介绍了复制android studio资产中的sqlite无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用免费的DBBrowser(SQLite)软件,我可以在data.sqlite文件中看到数据,但是当我将其复制到Android Studio Assets文件夹时,无论使用什么代码,都会出现以下两个错误之一:没有这样的表Table1_Sheet1或者android.database.sqlite.SQLiteDatabaseCorruptException:文件已加密或不是数据库(代码26):,编译时:从Table1_Sheet1的SELECT Column2,Column3,Column4,Column5,Column6,Column7在哪里Column1 =?

Using free DBBrowser(SQLite) software I can see the data inside the data.sqlite file but when I copy it to Android Studio Assets folder no matter what code I use I get one of two errors: No such table Table1_Sheet1 or android.database.sqlite.SQLiteDatabaseCorruptException: file is encrypted or is not a database (code 26): , while compiling: SELECT Column2, Column3, Column4,Column5,Column6, Column7 FROM Table1_Sheet1 WHERE Column1=?

我尝试的一种代码:

public class MyCustomDBHelper extends SQLiteOpenHelper {
private Context mycontext;
@Override public void onCreate(SQLiteDatabase db){}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
private String DB_PATH=""; 
private static String DB_NAME = "data.sqlite";
public SQLiteDatabase myDataBase;
public MyCustomDBHelper(Context context) throws IOException {
    super(context,DB_NAME,null,1);
    this.mycontext=context;
   DB_PATH=mycontext.getApplicationInfo().dataDir+"/databases/";
    boolean dbexist = checkdatabase();
    if (dbexist) {
        myDataBase=opendatabase();
    } else {
        System.out.println("Database doesn't exist");
        createdatabase();          
    }
}

public void createdatabase(){
        try {
            copydatabase();
        } catch(IOException e) {
            throw new Error("Error copying database");
        }
}

private boolean checkdatabase() {
    boolean checkdb = false;
    try {
        String myPath = DB_PATH+ DB_NAME;
        File dbfile = new File(myPath);
                  checkdb = dbfile.exists();
    } catch(SQLiteException e) {
        System.out.println("Database doesn't exist");
    }
    return checkdb;
}

private void copydatabase() throws IOException {
    InputStream myinput = mycontext.getAssets().open(DB_NAME);
    FileOutputStream(mycontext.getApplicationInfo().dataDir+"/databases/data.sqlite");

    //OutputStream myoutput = new FileOutputStream(mycontext.getDatabasePath("data.sqlite").getAbsolutePath());
    OutputStream myoutput = new FileOutputStream(DB_PATH + DB_NAME);
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myinput.read(buffer))>0) {
        myoutput.write(buffer,0,length);
    }

    //Close the streams
    myoutput.flush();
    myoutput.close();
    myinput.close();
}

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

}

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

}

当我尝试从复制的文件中读取数据时出现错误.我还观察到复制循环仅花费大约7倍的时间就不会花费很长时间.我还在用于测试数据文件夹内的移动设备中观察到,实际上没有为该项目创建任何内容.我观察到的最后一件事是Assets文件夹中的data.sqlite文件的文件图标上带有问号,这是否意味着android studio无法识别该文件?

I get the error when I try to read data from the copied file.I also observed the copy loop doesn't take long time only few times around 7 times. I also observed in the mobile I used for testing inside the data folder nothing is actually created for the project. Last thing I observed the data.sqlite file inside Assets folder has question mark on its file icon does this means it is not identified by android studio ?

推荐答案

我发现以下线程对我的问题更有用:如何在Android应用程序中使用现有数据库我为游标查询提供的参数也是错误的.

I found the following thread more helpful for my issue: How to use an existing database with an Android application also the argument I was feeding for the cursor query was wrong.

这篇关于复制android studio资产中的sqlite无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆