1

ファイルから 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();
}

データは 1 行だけ挿入され、ファイルの最後の行になります。何がうまくいかないのですか?問題が見つからないようです。

4

2 に答える 2

0

これはトランザクションの標準的なパターンで、setTransactionSuccessful()while ループ内を移動します。

beginTransaction();
try {
//db operations ...

setTransactionSuccessful();
} finally {
endTransaction();
}
于 2015-12-14T14:26:25.377 に答える