android - 从文件插入 SQLite 数据库,只插入一行

标签 android sqlite android-database

我正在从文件中向 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/

相关文章:

sql - 从每组sqlite中选择前n条记录

android - 在商店发布新版本的 Android 应用程序时更新 SQLite 数据库

android - 发布带有数据库的应用程序

android - 如何在 Android marshmallow 中禁用自动打开强制门户?

android - 错误构建 APK QT Creator - 构建 android 包失败

javascript - 使用node sqlite3异步获取所有数据

ios - Xcode中如何使用已有的数据库文件,读写?

android - 如何使用android找出用户存在于Sqlite数据库中?

android - 从广播接收器启动异步任务

安卓工作室 : Error:Internal error: (java. lang.ClassNotFoundException) com.google.wireless.android.sdk.stats.IntellijIndexingStats$Index