android, how to exec a sql file in sqlitedatabase

前端 未结 4 977
孤街浪徒
孤街浪徒 2020-12-01 02:18

i have \"food_db.sql\" file stored in /res/raw folder, it has tons of \'insert\' in it.

my question is how to i exec the file and get the data into sqlite databse in

4条回答
  •  隐瞒了意图╮
    2020-12-01 02:54

    @Override
    public void onCreate(SQLiteDatabase db) {
        InputStream is = null;
        try {
            is = context.getResources().openRawResource(R.raw.database_init);
            String initSql = IOUtils.toString(is);
            db.execSQL(initSql);
        } catch (IOException e) {
            Log.e(TAG, "Error loading init SQL from raw", e);
        } catch (SQLException e) {
            Log.e(TAG, "Error executing init SQL", e);
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
    

    An important callout is, SQLiteDatabase.execSQL(String sql) only execute a single SQL statement, multiple statements separated by semicolons are not supported.

    I've tried a hard time to find out why my database_init.sql doesn't work as expected.

提交回复
热议问题