【发布时间】:2012-01-19 22:15:34
【问题描述】:
我有一个 SQLite 数据库,我需要定期从中删除记录。在性能方面,这样做的最佳方法是什么。我有一系列独特的 PK id。我想出了两种方法:
使用准备好的语句
int[] ids = {1,2,3,4,5}; //example only, will be built elsewhere
Database db = null;
try {
db = DataConnection.getInstance(); //my class to get the connection instance
db.beginTransaction();
Statement st = db.createStatement("DELETE FROM myTable WHERE id = ?");
st.prepare();
for (int i=0;i<ids.length;i++) {
st.bind(1, ids[i]);
st.execute();
st.reset();
}
db.commitTransaction();
} catch (FileIOException e) {
e.printStackTrace();
} catch (DatabaseException e) {
e.printStackTrace();
}
或者使用'in'关键字
int[] ids = {1,2,3,4,5}; //example only, will be built elsewhere
Database db = null;
try {
db = DataConnection.getInstance();
//Util.JoinArray(int[] ids,char delim, char prepend, char postpend) returns a String of the ids separated by delim with prepend at the front and postpend at the end
Statement st = db.createStatement("DELETE FROM myTable WHERE id IN " + Util.joinArray(ids,',','(',')'));
st.prepare();
} catch (FileIOException e) {
e.printStackTrace();
} catch (DatabaseException e) {
e.printStackTrace();
}
【问题讨论】:
标签: sqlite blackberry