我需要一个SQLite查询来插入或更新SQLite表。
MyTable:
ID(主键,不为null)
名称
电话号码
Expected Query: If the Id is not present then have to insert a new row
and If the existing row value is changed then update the row when inserting multiple row.
编辑1:
我已经发布了INSERT查询,我已经在SQLite错误(错误代码= 1)中插入多行尝试过。 这样,我尝试使用"插入或替换"。 但它是working with Firfox SQLite Manager Not working with Android SQLite。
我已使用sDataBase.execSQL(query)执行此查询。
只是说明可能对某人有帮助。 具有"插入或替换"的批处理命令(例如..值(...),(...),(...)等等)完全可以。
可能对某人有用的另一个说明:)有趣的是,性能测试表明,"插入或替换"比普通的"插入"要慢得多(最坏情况下为5%)。
尝试这个:
String sql ="INSERT OR REPLACE INTO MyTable (Name, PhoneNumber) VALUES (?,?)";
SQLiteStatement st = db.compileStatement(sql);
并编写或更新:
public void writeOrUpdateData(ArrayList data) {
try {
db.beginTransaction();
for(int i = 0; i < data.size(); i++) {
st.clearBindings();
st.bindLong(1, data.get(i).getName());
st.bindString(2, data.get(i).getPhoneNumber);
st.executeInsert();
}
db.setTransactionSuccessful();
}
catch(Exception e) {}
finally {
db.endTransaction();
}
}
这样,您可以获得批量插入/更新,这非常有效!
多行将不适用于您的查询。 我努力了。 stackoverflow.com/questions/1609637/
@Ponmalar Emm,我希望您有一些要在本地sqlite db中编写/更新的自定义类对象。 因此,批量插入/更新就可以了。 在ArrayList中传递多个对象
感谢您的答复。 我正在从webview发送查询。 在WebView内,我将使用sDataBase.execSQL(query);执行查询。 所以我只需要查询
请检查我的EDIT1
我仅在Firefox SQLite Manager中使用主键创建了数据库。 我在Android的SQLite数据库中错过了这一点。 现在我有created database with PRIMARY KEY and NOT NULL。 现在工作正常。