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应用程序中创建或安装SQLite数据库_Android_Sqlite_Mobile - Fatal编程技术网

在Android应用程序中创建或安装SQLite数据库

在Android应用程序中创建或安装SQLite数据库,android,sqlite,mobile,Android,Sqlite,Mobile,我看到的所有关于在Android中使用SQLite db的教程都是自己创建数据库的。似乎这是一个要求。是否无法使用SQLite工作台创建db,并将其安装在Android应用程序文件夹中的正确位置 是否无法使用SQLite工作台创建db,并将其安装在Android应用程序文件夹中的正确位置 欢迎您这样做,作为打包和部署数据库的方法 但通常情况下,只有当您有重要数据要打包到应用程序中时,才可以这样做 是否无法使用SQLite工作台创建db,并将其安装在Android应用程序文件夹中的正确位置 欢迎您

我看到的所有关于在Android中使用SQLite db的教程都是自己创建数据库的。似乎这是一个要求。是否无法使用SQLite工作台创建db,并将其安装在Android应用程序文件夹中的正确位置

是否无法使用SQLite工作台创建db,并将其安装在Android应用程序文件夹中的正确位置

欢迎您这样做,作为打包和部署数据库的方法

但通常情况下,只有当您有重要数据要打包到应用程序中时,才可以这样做

是否无法使用SQLite工作台创建db,并将其安装在Android应用程序文件夹中的正确位置

欢迎您这样做,作为打包和部署数据库的方法

但通常情况下,只有当您有重要数据要打包到应用程序中时,才可以这样做

是否无法使用SQLite工作台创建db,并将其安装在Android应用程序文件夹中的正确位置

欢迎您这样做,作为打包和部署数据库的方法

但通常情况下,只有当您有重要数据要打包到应用程序中时,才可以这样做

是否无法使用SQLite工作台创建db,并将其安装在Android应用程序文件夹中的正确位置

欢迎您这样做,作为打包和部署数据库的方法

但是,通常只有当您有重要数据要打包到应用程序中时,才可以执行此操作。

这是可能的

只需创建SQLite数据库并将其放在资产文件夹中

当应用程序首次启动时,只需检查数据库是否存在,如果不存在,则创建数据库

    public String DB_PATH = null;
    public final static String DB_NAME = "mye.db"; //take note this is in your asset folder
    private static final int DB_VERSION = 1;
    private SQLiteDatabase myDatabase;
    private Context myContext = null;

        public DBAdapter(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        this.myContext = context;
        DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
    }

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

        this.getReadableDatabase();

        try {
            copyDatabase();
        } catch (IOException e) {

            throw new Error("Error copying database");

        }
    }

    /**
     * Copy DB from ASSETS
     */

    public void copyDatabase() throws IOException {

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

        // Path to 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 inputfile to the outputfile
        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();
    }

    /**
     * Opens Database . Method is synhcronized
     * @throws SQLException
     */
    public synchronized void openDatabase() throws SQLException {
        String dbPath = DB_PATH + DB_NAME;
        myDatabase = SQLiteDatabase.openDatabase(dbPath, null,
                SQLiteDatabase.OPEN_READWRITE);
    }

    /**
     * Closes database. 
     * Method is synchronized for protection
     */
    @Override
    public synchronized void close() {
        if (myDatabase != null) {
            myDatabase.close();
        }
        super.close();
    }


    /**
     * Check if the database exists
     * 
     * @param cntx
     * @return true / false
     */
    public boolean checkDatabase(Context cntx) {
        File dbFile = cntx.getDatabasePath(DB_NAME);
//      Log.e("zeus", "Check db returns : " + dbFile.exists());
        return dbFile.exists();

    }
在扩展应用程序的主活动或类中,只需将一个实例获取到DBAdapter,并检查db是否存在

mDBAdapter = new DBAdapter(getApplicationContext());
if (!mDBAdapter.checkDatabase(getApplicationContext())) {
    try {
        mDBAdapter.createDataBase();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
这是可能的

只需创建SQLite数据库并将其放在资产文件夹中

当应用程序首次启动时,只需检查数据库是否存在,如果不存在,则创建数据库

    public String DB_PATH = null;
    public final static String DB_NAME = "mye.db"; //take note this is in your asset folder
    private static final int DB_VERSION = 1;
    private SQLiteDatabase myDatabase;
    private Context myContext = null;

        public DBAdapter(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        this.myContext = context;
        DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
    }

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

        this.getReadableDatabase();

        try {
            copyDatabase();
        } catch (IOException e) {

            throw new Error("Error copying database");

        }
    }

    /**
     * Copy DB from ASSETS
     */

    public void copyDatabase() throws IOException {

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

        // Path to 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 inputfile to the outputfile
        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();
    }

    /**
     * Opens Database . Method is synhcronized
     * @throws SQLException
     */
    public synchronized void openDatabase() throws SQLException {
        String dbPath = DB_PATH + DB_NAME;
        myDatabase = SQLiteDatabase.openDatabase(dbPath, null,
                SQLiteDatabase.OPEN_READWRITE);
    }

    /**
     * Closes database. 
     * Method is synchronized for protection
     */
    @Override
    public synchronized void close() {
        if (myDatabase != null) {
            myDatabase.close();
        }
        super.close();
    }


    /**
     * Check if the database exists
     * 
     * @param cntx
     * @return true / false
     */
    public boolean checkDatabase(Context cntx) {
        File dbFile = cntx.getDatabasePath(DB_NAME);
//      Log.e("zeus", "Check db returns : " + dbFile.exists());
        return dbFile.exists();

    }
在扩展应用程序的主活动或类中,只需将一个实例获取到DBAdapter,并检查db是否存在

mDBAdapter = new DBAdapter(getApplicationContext());
if (!mDBAdapter.checkDatabase(getApplicationContext())) {
    try {
        mDBAdapter.createDataBase();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
这是可能的

只需创建SQLite数据库并将其放在资产文件夹中

当应用程序首次启动时,只需检查数据库是否存在,如果不存在,则创建数据库

    public String DB_PATH = null;
    public final static String DB_NAME = "mye.db"; //take note this is in your asset folder
    private static final int DB_VERSION = 1;
    private SQLiteDatabase myDatabase;
    private Context myContext = null;

        public DBAdapter(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        this.myContext = context;
        DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
    }

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

        this.getReadableDatabase();

        try {
            copyDatabase();
        } catch (IOException e) {

            throw new Error("Error copying database");

        }
    }

    /**
     * Copy DB from ASSETS
     */

    public void copyDatabase() throws IOException {

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

        // Path to 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 inputfile to the outputfile
        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();
    }

    /**
     * Opens Database . Method is synhcronized
     * @throws SQLException
     */
    public synchronized void openDatabase() throws SQLException {
        String dbPath = DB_PATH + DB_NAME;
        myDatabase = SQLiteDatabase.openDatabase(dbPath, null,
                SQLiteDatabase.OPEN_READWRITE);
    }

    /**
     * Closes database. 
     * Method is synchronized for protection
     */
    @Override
    public synchronized void close() {
        if (myDatabase != null) {
            myDatabase.close();
        }
        super.close();
    }


    /**
     * Check if the database exists
     * 
     * @param cntx
     * @return true / false
     */
    public boolean checkDatabase(Context cntx) {
        File dbFile = cntx.getDatabasePath(DB_NAME);
//      Log.e("zeus", "Check db returns : " + dbFile.exists());
        return dbFile.exists();

    }
在扩展应用程序的主活动或类中,只需将一个实例获取到DBAdapter,并检查db是否存在

mDBAdapter = new DBAdapter(getApplicationContext());
if (!mDBAdapter.checkDatabase(getApplicationContext())) {
    try {
        mDBAdapter.createDataBase();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
这是可能的

只需创建SQLite数据库并将其放在资产文件夹中

当应用程序首次启动时,只需检查数据库是否存在,如果不存在,则创建数据库

    public String DB_PATH = null;
    public final static String DB_NAME = "mye.db"; //take note this is in your asset folder
    private static final int DB_VERSION = 1;
    private SQLiteDatabase myDatabase;
    private Context myContext = null;

        public DBAdapter(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        this.myContext = context;
        DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
    }

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

        this.getReadableDatabase();

        try {
            copyDatabase();
        } catch (IOException e) {

            throw new Error("Error copying database");

        }
    }

    /**
     * Copy DB from ASSETS
     */

    public void copyDatabase() throws IOException {

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

        // Path to 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 inputfile to the outputfile
        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();
    }

    /**
     * Opens Database . Method is synhcronized
     * @throws SQLException
     */
    public synchronized void openDatabase() throws SQLException {
        String dbPath = DB_PATH + DB_NAME;
        myDatabase = SQLiteDatabase.openDatabase(dbPath, null,
                SQLiteDatabase.OPEN_READWRITE);
    }

    /**
     * Closes database. 
     * Method is synchronized for protection
     */
    @Override
    public synchronized void close() {
        if (myDatabase != null) {
            myDatabase.close();
        }
        super.close();
    }


    /**
     * Check if the database exists
     * 
     * @param cntx
     * @return true / false
     */
    public boolean checkDatabase(Context cntx) {
        File dbFile = cntx.getDatabasePath(DB_NAME);
//      Log.e("zeus", "Check db returns : " + dbFile.exists());
        return dbFile.exists();

    }
在扩展应用程序的主活动或类中,只需将一个实例获取到DBAdapter,并检查db是否存在

mDBAdapter = new DBAdapter(getApplicationContext());
if (!mDBAdapter.checkDatabase(getApplicationContext())) {
    try {
        mDBAdapter.createDataBase();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

如何将其放置在我的资产文件夹中?当我将手机连接到电脑时,我找不到这样的文件夹?我不确定你是否理解我写的内容。在你的应用程序文件夹结构中,你有src/libs/res等。还应该有一个名为“资产”的文件夹,如果没有,创建它并将你的数据库放在那里。我如何将它放在我的资产文件夹中?当我将手机连接到电脑时,我找不到这样的文件夹?我不确定你是否理解我写的内容。在你的应用程序文件夹结构中,你有src/libs/res等。还应该有一个名为“资产”的文件夹,如果没有,创建它并将你的数据库放在那里。我如何将它放在我的资产文件夹中?当我将手机连接到电脑时,我找不到这样的文件夹?我不确定你是否理解我写的内容。在你的应用程序文件夹结构中,你有src/libs/res等。还应该有一个名为“资产”的文件夹,如果没有,创建它并将你的数据库放在那里。我如何将它放在我的资产文件夹中?当我将手机连接到电脑时,我找不到这样的文件夹?我不确定你是否理解我写的内容。在应用程序文件夹结构中,您有src/libs/res等。还应该有一个名为“资产”的文件夹,如果没有,请创建它并将数据库放在那里。