我面对的是"没有这样的桌子"已导入数据库的异常

时间:2018-01-11 23:43:27

标签: android database sqlite ormlite

我在我的应用中预先制作了.db文件。准备好的数据库在资产文件夹中不到1mb。

我完成了所有权限(sdk> 23),应用程序在几乎所有设备上都运行良好。但在一台设备上测试时我有问题。 (引起:android.database.sqlite.SQLiteException:没有这样的表......)。

可能有什么问题(可能只是一部手机),以及我需要做些什么来改善它?

我使用的是ormlite,这是DatabaseHelper类......

public class DatabaseHelper extends OrmLiteSqliteOpenHelper{


    private static final String DATABASE_NAME    = "myDataBase";
    private static String DB_PATH = "/data/data/com.example.mypcname.myapp/databases/";

    Context mContext;

    private static final int DATABASE_VERSION = 1;

    private Dao<Case, Integer> mCaseDao = null;

    ...multiple Dao objects



    public DatabaseHelper(Context context)
    {

        super(context, DB_PATH+DATABASE_NAME, null, DATABASE_VERSION);
        this.mContext = context;
        copyDatabase();

    }


    private void copyDatabase(){
        boolean dbExist = checkdatabase();
        if(!dbExist){

            try {

                File dir = new File(DB_PATH);
                dir.mkdirs();
                InputStream myInput = mContext.getAssets().open(DATABASE_NAME);
                String outfilename = DB_PATH + DATABASE_NAME;
                Log.i(DatabaseHelper.class.getName(), "DB Path : " + outfilename);
                dir.mkdirs();
                OutputStream myoutput = new FileOutputStream(outfilename);
                byte[] buffer = new byte[1024];
                int lenght;
                while ((lenght = myInput.read(buffer)) > 0){
                    myoutput.write(buffer, 0, lenght);
                }

                myoutput.flush();
                myoutput.close();
                myInput.close();



            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    private boolean checkdatabase() {
        boolean checkdb = false;

        String myPath = DB_PATH + DATABASE_NAME;
        //String myPath = mContext.getPackageCodePath() + DATABASE_NAME;
        File dbfile = new File(myPath);
        checkdb = dbfile.exists();

        Log.i(DatabaseHelper.class.getName(), "DB Exist : " + checkdb);

        return checkdb;
    }


    public Dao<Case, Integer> getmCaseDao() throws SQLException{

    if (mCaseDao == null){
         mCaseDao = getDao(Case.class);
    }
    return mCaseDao;
    }


    ...


    @Override
    public void close() {

    mCaseDao = null;

    ...

        super.close();
    }
}

0 个答案:

没有答案