我已完全放弃了这一点,所以如果主持人碰巧来了,删除就会很棒 保留这个问题并没有什么坏处,但是CoreData非常好,你知道吗?
我有一个用于处理表内容的sqlite数据库。这很好,一切(比我看到的其他选项容易得多),但我遇到了整数问题。我第一次在启动应用程序后编辑项目时,int的字段为空。重新输入工作正常,它保存并显示在表格中,但下一次编辑(不重新打开应用程序)将第二项的int设置为第一项。
即,A(1)重置为A(0)。我修复它(A(1)),但是一加载编辑视图,B(2)就变成B(1)。修复它(B(2))或不修复(B(1)),C(3)将具有与B相同的(#)。
我仍然无法弄清楚是什么导致了它。将int更改为字符串(编辑数据库列和应用程序中的每个相关文件)肯定会起作用,但这只是为了让它更慢更容易破解而进行的大量不必要的工作。
编辑:
CREATE TABLE "items" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" VARCHAR, "need" INTEGER DEFAULT 0, "notes" TEXT)
- (void)updateItemAtIndexPath:(NSIndexPath *)path {
Item *i = (Item *)[items objectAtIndex:path.row];
int ret;
const char *sql = "update items set name = ?, need = ?, notes = ? where id = ?;";
if (!updStmt) { // build update statement
if ((ret = sqlite3_prepare_v2(database, sql, -1, &updStmt, NULL)) != SQLITE_OK) {
NSAssert1(0, @"Error building statement to update items [%s]", sqlite3_errmsg(database));
}
}
// bind values to statement
NSString *s = i.name;
if (s == NULL) s = @"";
sqlite3_bind_text(updStmt, 1, [s UTF8String], -1, SQLITE_TRANSIENT);
NSInteger n = i.need;
sqlite3_bind_int(updStmt, 2, n);
s = i.notes;
if (s == NULL) s = @"";
sqlite3_bind_text(updStmt, 3, [s UTF8String], -1, SQLITE_TRANSIENT);
n = i.itemid;
sqlite3_bind_int(updStmt, 4, n);
// now execute sql statement
if (sqlite3_step(updStmt) != SQLITE_DONE) {
NSAssert1(0, @"Error updating values [%s]", sqlite3_errmsg(database));
}
// now reset bound statement to original state
sqlite3_reset(updStmt);
}
- (void)insertItem:(Item *)item {
int ret;
const char *sql = "insert into items (name, need, notes) values (?, ?, ?);";
if (!insStmt) { // first insert - build statement
if ((ret = sqlite3_prepare_v2(database, sql, -1, &insStmt, NULL)) != SQLITE_OK) {
NSAssert1(0, @"Error building statement to insert item [%s]", sqlite3_errmsg(database));
}
}
// bind values
NSString *s = item.name;
if (s == NULL) s = @"";
sqlite3_bind_text(insStmt, 1, [s UTF8String], -1, SQLITE_TRANSIENT);
NSInteger n = item.need;
sqlite3_bind_int(insStmt, 2, n);
s = item.notes;
if (s == NULL) s = @"";
sqlite3_bind_text(insStmt, 3, [s UTF8String], -1, SQLITE_TRANSIENT);
// execute sql statement
if (sqlite3_step(insStmt) != SQLITE_DONE) {
NSAssert1(0, @"Error inserting item [%s]", sqlite3_errmsg(database));
}
// reset bound statement to original state
sqlite3_reset(insStmt);
[self readItems]; // refresh array
}
答案 0 :(得分:1)
我会尝试从各种值构造查询字符串,而不是使用sqlite3_bind_text
和sqlite3_bind_int
,而是使用sqlite3_exec
来运行它。让我们称之为解决方案的尝试。
示例(警告,未经测试!!):
- (void)updateItemAtIndexPath:(NSIndexPath *)path {
Item *i = (Item *)[items objectAtIndex:path.row];
// validate values
NSString *name = i.name;
if (name == NULL) name = @"";
[name stringByReplacingOccurrencesOfString:@"'"
withString:@"''"];
NSInteger need = i.need;
NSString *notes = i.notes;
if (notes == NULL) notes = @"";
[notes stringByReplacingOccurrencesOfString:@"'"
withString:@"''"];
NSInteger itemid = i.itemid;
NSString *sql = [NSString stringWithFormat:
@"update items set name = '%@', need = %@, notes = '%@' where id = %@;",
name, need, notes, itemid];
// now execute sql statement
if (sqlite3_exec(database, [sql UTF8String], NULL, NULL, NULL) != SQLITE_DONE) {
NSAssert1(0, @"Error updating values [%s]", sqlite3_errmsg(database));
}
}