在我的应用程序中,我在sqlite数据库中创建了许多表,但是在一个表中包含超过100k行,我使用http连接从服务器获取数据。得到这么多记录给我带来了更多的时间,以避免这个问题,我想通过PC连接我的平板电脑,我想在平板电脑中放置txt文件(有插入语句)。
现在我如何在我的应用程序中访问该txt文件,如何使用该txt文件在我的表中插入数据,以及我必须复制此txt文件以从我的应用程序访问它的位置。
我的数据库文件大小也是273 MB,所以我认为不可能将它放在资产文件夹中
答案 0 :(得分:5)
您可以将文件复制到assets
文件夹
代码示例:
private void initDatabaseData(SQLiteDatabase db) {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(context.getAssets().open(file_name)), 1024 * 4);
String line = null;
db.beginTransaction();
while ((line = br.readLine()) != null) {
db.execSQL(line);
}
db.setTransactionSuccessful();
} catch (IOException e) {
Log.e(LOG_TAG, "read database init file error");
} finally {
db.endTransaction();
if (br != null) {
try {
br.close();
} catch (IOException e) {
Log.e(LOG_TAG, "buffer reader close error");
}
}
}
}
上面的代码需要 txt文件的每一行都是一个sql语句