【发布时间】:2017-01-29 13:32:30
【问题描述】:
使用SQLiteStatement的插入代码通常是这样的,
String sql = "INSERT INTO table_name (column_1, column_2, column_3) VALUES (?, ?, ?)";
SQLiteStatement statement = db.compileStatement(sql);
int intValue = 57;
String stringValue1 = "hello";
String stringValue2 = "world";
// corresponding to each question mark in the query
statement.bindLong(1, intValue);
statement.bindString(2, stringValue1);
statement.bindString(3, stringValue2);
long rowId = statement.executeInsert();
现在这工作得很好,但我在这里发现的问题是我必须非常小心地将正确的数据绑定到相应的索引。一个简单的索引交换会给我一个错误。
另外假设将来我的column_2 从表中删除,那么我将不得不更改column_2 索引之后的所有索引,否则该语句将不起作用。如果我只有 3 列,这似乎微不足道。想象一下,如果一个表有 10-12 个(甚至更多)列并且第 2 列被删除。我将不得不更新所有后续列的索引。整个过程似乎效率低下且容易出错。
有没有一种优雅的方式来处理这一切?
编辑:我为什么要使用 SQLiteStatement ?检查这个:Improve INSERT-per-second performance of SQLite?
【问题讨论】:
-
这个问题看起来很相似:stackoverflow.com/questions/27233530/…
标签: android sqlite sql-insert