我正在从文件中向 android SQLite 数据库中插入行。在 databaseHelper 的 onUpdate 函数中,我调用了 insertInDb 函数:
InputStream is = null;
AssetManager assetManager = context.getResources().getAssets();
String fileLine = "";
String[] data;
ContentValues row = new ContentValues();
long rowid;
try {
is = assetManager.open(filename);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
db.beginTransaction();
while((fileLine = br.readLine()) != null){
data = fileLine.split("@-@");
row.put(QuoteAllEntry.COLUMN_QUOTE, data[0]);
row.put(QuoteAllEntry.COLUMN_AUTHOR, data[1]);
row.put(QuoteAllEntry.COLUMN_FAVORITE, 0);
row.put(QuoteAllEntry.COLUMN_CAT_KEY, data[2]));
rowid = db.insert(DataContract.QuoteAllEntry.TABLE_NAME, null, row);
}
db.setTransactionSuccessful();
}catch (Exception e) {
e.printStackTrace();
}
finally {
db.endTransaction();
}
只插入了一行数据,而且是文件的最后一行。出了什么问题?我似乎找不到问题。
最佳答案
将 ContentValues row = new ContentValues();
移动到 while 循环中。
此外,您没有关闭导致内存泄漏的输入流。
关于android - 从文件插入 SQLite 数据库,只插入一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34269195/