需要将批量数据插入SQLite(每次100-500条记录至少...有7列,每行ll有7个值)
使用statement.bind请建议是否有问题,并使用db.execSQL批量插入来实现
SQLiteDatabase db = DBHelper.getWritableDatabase();
String str = "INSERT INTO DBAdapter.KEY_BD1_DB_NAME(a, b, c, d, e, f,g) VALUES(?, ?, ?, ?, ?, ?,?)";
SQLiteStatement statement = db.compileStatement(str);
db.beginTransactionNonExclusive();
try {
for (int i=0;i<arraylist.length;i++)
{
statement.bindLong(1, id);
statement.bindLong(2, alist[i]);
statement.bindLong(3, blist[i]);
statement.bindString(4, strTStamp[0]);
statement.bindString(5, strTStamp[1]);
statement.bindLong(6, j);
statement.bindLong(7, tstamp);
if (alist.size>100)
{
if(count==100)
{
statement.executeInsert();
statement.clearBindings();
count=0;
}
else
{
count++;
}
}
}
statement.executeInsert();
statement.clearBindings();
db.setTransactionSuccessful();
Log.d("INSERT","Done");
return true;
} catch (SQLException ex) {
Log.w("SqlErr", "At kkr : " + ex.toString());
return false;
}finally {
db.endTransaction();
db.close();
}
您一次只能插入一行。因此,从代码中删除奇怪的东西,并将我在评论中提到的位仅包含一次绑定常量值(免责声明:那将在C中起作用;不是100%确定java / android绑定),标准方法插入一堆数据应该类似于:
SQLiteDatabase db = DBHelper.getWritableDatabase();
db.beginTransaction();
try {
String str = "INSERT INTO DBAdapter.KEY_BD1_DB_NAME(a, b, c, d, e, f,g) VALUES(?, ?, ?, ?, ?, ?,?)";
SQLiteStatement statement = db.compileStatement(str);
statement.bindLong(1, id);
statement.bindString(4, strTStamp[0]);
statement.bindString(5, strTStamp[1]);
statement.bindLong(6, j);
statement.bindLong(7, tstamp);
for (int i=0;i<arraylist.length;i++) {
statement.bindLong(2, alist[i]);
statement.bindLong(3, blist[i]);
statement.executeInsert();
}
db.setTransactionSuccessful();
Log.d("INSERT","Done");
return true;
} catch (SQLException ex) {
Log.w("SqlErr", "At kkr : " + ex.toString());
return false;
} finally {
db.endTransaction();
db.close();
}
其他的东西:
看起来您正在尝试在首次使用时填充数据库。但是,在您的应用程序的assets/
中发布您的应用程序并准备好使用数据库文件,然后在第一次使用时只需使用该数据库进行“播种”,而不是这样做,更好,更快,更简单。有一些助手库有助于:https://github.com/jgilfelt/android-sqlite-asset-helper