gpt4 book ai didi

Android - SQLiteStatement(多次插入)执行失败,API 级别 14

转载 作者:IT王子 更新时间:2023-10-29 06:27:39 27 4
gpt4 key购买 nike

我使用以下代码在数据库中有效地插入新行:

@Override
public void insertImpacts(HolderExpense expense, int[] shares, int[] amounts, HolderUser[] users) {

try {

mDb.beginTransaction(); // the insertion of the impacts of one expense are considered to be ONE block

String sql = "INSERT INTO "+DATABASE_TABLE_IMPACTS+" "+
"("+IMPACT_USERROWID+", "+IMPACT_EXPENSEROWID+", "+IMPACT_TTROWID+", "+IMPACT_NUMBEROFPARTS+", "+IMPACT_FIXEDAMOUNT+") VALUES (?, ?, ?, ?, ?)";

SQLiteStatement stmt = mDb.compileStatement(sql);

stmt.bindLong(2, expense.rowId);
stmt.bindLong(3, expense.tt.rowId);

int i = 0;

while (i < shares.length) {

if (users[i] != null) {

Log.v(TAG, "user " +i+": users[i].rowId:"+users[i].rowId+" expense.rowId:"+expense.rowId+" expense.tt.rowId:"+expense.tt.rowId+" shares[i]:"+shares[i]+" amounts[i]:"+amounts[i]+" ");

stmt.bindLong(1, users[i].rowId);
stmt.bindString(4, shares[i]+"");
stmt.bindString(5, amounts[i]+"");

stmt.execute();

}
i++;
}

stmt.close();

mDb.setTransactionSuccessful();

}
catch (Exception e) { e.printStackTrace(); throw new RuntimeException("insertImpacts() failed"); }
finally { mDb.endTransaction(); }

}

它一直工作到 android 4.x 时出现错误:

02-26 14:27:46.179: W/System.err(937): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed

02-26 14:27:46.179: W/System.err(937): at android.database.sqlite.SQLiteStatement.native_execute(Native Method)

02-26 14:27:46.219: W/System.err(937): at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:92)

02-26 14:27:46.219: W/System.err(937): at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:70)

将第二行插入表中时,它似乎在 stmt.execute() 处崩溃。

有什么线索吗?

-- 版本 --

表的架构如下:

 private static final String DATABASE_CREATE_TABLE_IMPACTS =
"create table " + DATABASE_TABLE_IMPACTS + " ("
+ IMPACT_ROWID + " integer primary key autoincrement, "
+ IMPACT_USERROWID + " integer not null, "
+ IMPACT_EXPENSEROWID + " integer not null, "
+ IMPACT_TTROWID + " integer not null, "
+ IMPACT_NUMBEROFPARTS + " integer not null, "
+ IMPACT_FIXEDAMOUNT + " integer not null, "
+ "constraint i_cstr1 unique ("+IMPACT_USERROWID+", "+IMPACT_EXPENSEROWID+")); ";

此代码在 Android 2.2 上非常有效(但在 Android 4.0 上失败)。

打印我插入的前两行(尝试插入第二行时崩溃):

02-26 14:27:46.069: E/DatabaseAdapter.java(937): user 0: users[i].rowId:7 expense.rowId:2 expense.tt.rowId:2 shares[i]:1 amounts[i]:-1 
02-26 14:27:46.069: E/DatabaseAdapter.java(937): user 1: users[i].rowId:5 expense.rowId:2 expense.tt.rowId:2 shares[i]:1 amounts[i]:-1

最佳答案

找到了。不同版本的 android 不以相同的方式运行。在 Android 4.x 上,所有“bindXXX”行都必须放在“while”循环中。即使它是重复的。

while (i < shares.length) {

if (users[i] != null) {

stmt.bindLong(1, users[i].rowId);
stmt.bindLong(2, expense.rowId);
stmt.bindLong(3, expense.tt.rowId);
stmt.bindString(4, String.valueOf(shares[i]));
stmt.bindString(5, String.valueOf(amounts[i]));

stmt.execute();
}

i++;

}

关于Android - SQLiteStatement(多次插入)执行失败,API 级别 14,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9453957/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com