如何解决Android SQLiteStatement插入,没有任何反应
我有一些来自SoapObject的数据,我想插入到sqlite中,为了获得更好的性能,我使用以下代码:
public void testInsert(String sql,SoapObject rs,int index) {
try {
sql = "INSERT INTO NSPMasterHarga (KdBarang,Wilayah,HargaJual1,HargaJual2) VALUES (?,?,?)";
theDatabase = getWritableDatabase();
theDatabase.beginTransaction();
String drop = "DROP TABLE IF EXISTS NSPMasterHarga";
SQLiteStatement stmtDrop = theDatabase.compileStatement(drop);
stmtDrop.execute();
String create = "CREATE TABLE NSPMasterHarga (KdBarang TEXT PRIMARY KEY,Wilayah TEXT,HargaJual1 TEXT,HargaJual2 TEXT)";
SQLiteStatement stmtCreate = theDatabase.compileStatement(create);
stmtCreate.execute();
SQLiteStatement stmt = theDatabase.compileStatement(sql);
int count = rs.getPropertyCount();
for (int i = 0; i < count; i++) {
SoapObject row = (SoapObject) rs.getProperty(i);
for (int j = 1; j <= index; j++) {
stmt.bindString(j,row.getProperty(j - 1).toString().replace("anyType{}",""));
}
long entryID = stmt.executeInsert();
stmt.clearBindings();
}
/*for (int i = 0; i < NUMBER_OF_ROWS; i++) {
//generate some values
stmt.bindString(1,randomName);
stmt.bindString(2,randomDescription);
stmt.bindDouble(3,randomPrice);
stmt.bindLong(4,randomNumber);
long entryID = stmt.executeInsert();
stmt.clearBindings();
}*/
theDatabase.setTransactionSuccessful();
theDatabase.endTransaction();
theDatabase.close();
}
catch (Exception ex)
{
String err = ex.getMessage();
}
}
调试时,我没有任何错误,但是数据没有插入到我的sqlite中。 有任何想法或线索吗?
谢谢
解决方法
以获得更好的性能
我不确定您指的是代码的哪一部分。每次交互后打开和关闭数据库对于性能而言都糟糕。 SQLiteOpenHelper负责所有这些工作,因此您无需手动执行任何操作。
尝试以下替代方法以插入条目:
public boolean addEntry(){
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("column1","value1"); // make sure the type corresponds to your sql column type
values.put("column2","value2");
values.put("column3","value3");
values.put("column4Int",1);
long newRowId = db.insert(TABLE_NAME,null,values);
Log.d("DBHelper","Added row " + newRowId + " to DB.");
return newRowId != -1; // -1 means it failed
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。