Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android在某些设备上的“没有这样的表”问题_Android_Sqlite_Android Intent_Android Emulator_Android Debug - Fatal编程技术网

android在某些设备上的“没有这样的表”问题

android在某些设备上的“没有这样的表”问题,android,sqlite,android-intent,android-emulator,android-debug,Android,Sqlite,Android Intent,Android Emulator,Android Debug,我在android应用程序中使用一个外部数据库,它在所有模拟器和三星real设备上运行良好。但当我检查宏碁智能手机时,我的应用程序会崩溃,但以下情况除外: android.database.sqlite.SQLiteException: no such table: 我觉得很奇怪。我已在文件资源管理器中的“数据”文件夹下进行了检查,其中包含我的数据库和所有表 我不明白为什么会这样 请引导我。提前谢谢 sql helper类的代码如下: public class MyDatabaseHelper

我在android应用程序中使用一个外部数据库,它在所有模拟器和三星real设备上运行良好。但当我检查宏碁智能手机时,我的应用程序会崩溃,但以下情况除外:

android.database.sqlite.SQLiteException: no such table:
我觉得很奇怪。我已在文件资源管理器中的“数据”文件夹下进行了检查,其中包含我的数据库和所有表

我不明白为什么会这样

请引导我。提前谢谢

sql helper类的代码如下:

public class MyDatabaseHelper extends SQLiteOpenHelper {

    // System path of application database.
    private static String DB_PATH = MyApplication.getAppContext()
            .getFilesDir().getParentFile().getPath()
            + "/databases/";
    private static String DB_NAME = "myDB";
    private SQLiteDatabase myDataBase;
    private final Context myContext;

    /**
     * Constructor Takes and keeps a reference of the passed context in order to
     * access to the application assets and resources.
     * 
     * @param context
     */
    public MyDatabaseHelper(Context context) {
        super(context, DB_NAME, null, 1);
        this.myContext = context;
    }

    /**
     * Creates a empty database on the system and rewrites it with own database.
     * 
     */
    public void createDataBase() throws IOException {

        checkDataBase();
        // Creates empty database default system path
        this.getReadableDatabase();
        try {
            copyDataBase();
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }

    /**
     * Checks if the database already exist to avoid re-copying the file each
     * time whenever the application opened.
     * 
     * @return true if it exists, false if it doesn't
     */
    private boolean checkDataBase() {

        SQLiteDatabase checkDB = null;
        try {
            String myPath = DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null,
                    SQLiteDatabase.OPEN_READONLY);
        } catch (SQLiteException e) {
            e.printStackTrace();
        }

        if (checkDB != null) {
            checkDB.close();
        }
        return checkDB != null ? true : false;
    }

    /**
     * Copies database from local assets-folder to the just created empty
     * database in the system folder and from where it can be accessed and
     * handled using byte stream transferring.
     * 
     */
    private void copyDataBase() throws IOException {

        // Open local db as the input stream
        InputStream myInput = myContext.getAssets().open(DB_NAME + ".db");

        // Path of the just created empty db
        String outFileName = DB_PATH + DB_NAME;

        // Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);

        // transfer bytes from the input file to the output file
        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 {

        // Opens the database
        String myPath = DB_PATH + DB_NAME;
        return SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READONLY);
    }

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

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

    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
        // TODO Auto-generated method stub
    }
}

可能您已经硬编码了数据库的路径。对于不同的设备,可能会有所不同。使用envirement.getExternalStorageDirectory创建数据库的路径。

可能您已经对数据库的路径进行了硬编码。对于不同的设备,可能会有所不同。使用envirement.getExternalStorageDirectory创建数据库的路径。

最后我解决了我的问题

我只是将可读数据库放入数据库中,并在再次打开它之后关闭它。我的完整代码如下:

 public class MyDatabaseHelper extends SQLiteOpenHelper {

    // System path of application database.
    private static String DB_PATH = MyApplication.getAppContext()
            .getFilesDir().getParentFile().getPath()
            + "/databases/";
    private static String DB_NAME = "myDB";
    private SQLiteDatabase myDataBase;
    private final Context myContext;

    /**
     * Constructor Takes and keeps a reference of the passed context in order to
     * access to the application assets and resources.
     * 
     * @param context
     */
    public MyDatabaseHelper(Context context) {
        super(context, DB_NAME, null, 1);
        this.myContext = context;
    }

    /**
     * Creates a empty database on the system and rewrites it with own database.
     * 
     */
    public void createDataBase() throws IOException {

        checkDataBase();
        SQLiteDatabase db_Read = null;

        // Creates empty database default system path
        db_Read = this.getReadableDatabase();
        db_Read.close();
        try {
            copyDataBase();
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }

    /**
     * Checks if the database already exist to avoid re-copying the file each
     * time whenever the application opened.
     * 
     * @return true if it exists, false if it doesn't
     */
    private boolean checkDataBase() {

        SQLiteDatabase checkDB = null;
        try {
            String myPath = DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null,
                    SQLiteDatabase.OPEN_READONLY);
        } catch (SQLiteException e) {
            e.printStackTrace();
        }

        if (checkDB != null) {
            checkDB.close();
        }
        return checkDB != null ? true : false;
    }

    /**
     * Copies database from local assets-folder to the just created empty
     * database in the system folder and from where it can be accessed and
     * handled using byte stream transferring.
     * 
     */
    private void copyDataBase() throws IOException {

        // Open local db as the input stream
        InputStream myInput = myContext.getAssets().open(DB_NAME + ".db");

        // Path of the just created empty db
        String outFileName = DB_PATH + DB_NAME;

        // Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);

        // transfer bytes from the input file to the output file
        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 {

        // Opens the database
        String myPath = DB_PATH + DB_NAME;
        return SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READONLY);
    }

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

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

    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
        // TODO Auto-generated method stub
    }
}

我找到了答案:

最后我解决了我的问题

我只是将可读数据库放入数据库中,并在再次打开它之后关闭它。我的完整代码如下:

 public class MyDatabaseHelper extends SQLiteOpenHelper {

    // System path of application database.
    private static String DB_PATH = MyApplication.getAppContext()
            .getFilesDir().getParentFile().getPath()
            + "/databases/";
    private static String DB_NAME = "myDB";
    private SQLiteDatabase myDataBase;
    private final Context myContext;

    /**
     * Constructor Takes and keeps a reference of the passed context in order to
     * access to the application assets and resources.
     * 
     * @param context
     */
    public MyDatabaseHelper(Context context) {
        super(context, DB_NAME, null, 1);
        this.myContext = context;
    }

    /**
     * Creates a empty database on the system and rewrites it with own database.
     * 
     */
    public void createDataBase() throws IOException {

        checkDataBase();
        SQLiteDatabase db_Read = null;

        // Creates empty database default system path
        db_Read = this.getReadableDatabase();
        db_Read.close();
        try {
            copyDataBase();
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }

    /**
     * Checks if the database already exist to avoid re-copying the file each
     * time whenever the application opened.
     * 
     * @return true if it exists, false if it doesn't
     */
    private boolean checkDataBase() {

        SQLiteDatabase checkDB = null;
        try {
            String myPath = DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null,
                    SQLiteDatabase.OPEN_READONLY);
        } catch (SQLiteException e) {
            e.printStackTrace();
        }

        if (checkDB != null) {
            checkDB.close();
        }
        return checkDB != null ? true : false;
    }

    /**
     * Copies database from local assets-folder to the just created empty
     * database in the system folder and from where it can be accessed and
     * handled using byte stream transferring.
     * 
     */
    private void copyDataBase() throws IOException {

        // Open local db as the input stream
        InputStream myInput = myContext.getAssets().open(DB_NAME + ".db");

        // Path of the just created empty db
        String outFileName = DB_PATH + DB_NAME;

        // Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);

        // transfer bytes from the input file to the output file
        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 {

        // Opens the database
        String myPath = DB_PATH + DB_NAME;
        return SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READONLY);
    }

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

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

    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
        // TODO Auto-generated method stub
    }
}

我找到了答案:

尝试使用这个私有静态字符串DB_PATH=/data/data/your package name/databases/

尝试使用这个私有静态字符串DB_PATH=/data/data/your package name/databases/

在此处添加java代码帮助器类。是否使用默认的db路径?在此处添加java代码帮助器类。是否使用默认的db路径?我通常会在代码中的何处检查数据库是否为硬编码?我认为我正在使用的代码有三个本地数据库。在代码中,我通常在哪里检查数据库是否是硬编码的?我认为我正在使用的代码有三个本地数据库。