0

这是我正在尝试使用的方法:

NSString *orario = [NSString new];
int idmed = 0;
fileMgr = [NSFileManager defaultManager];
sqlite3_stmt *stmt=nil;
sqlite3_stmt *stmt1=nil;
sqlite3_stmt *stmt2=nil;
sqlite3 *dbase;
NSString *database = [self.GetDocumentDirectory stringByAppendingPathComponent:@"med.sqlite"];
sqlite3_open([database UTF8String], &dbase);
const char *sql = "delete from orari where date(orario) <= date('now','-2 day','localtime')";
sqlite3_prepare_v2(dbase, sql, -1, &stmt, NULL);
sqlite3_step(stmt);
const char *sql1 = "select orario,idmedicina from orari where date(orario)=date('now','localtime') and forever = 1";
const char *sql2 = "insert into orari (orario,idmedicina,presa,forever) values (datetime('?','+1 day'),?,0,1)";
sqlite3_prepare_v2(dbase, sql1, -1, &stmt1, NULL);

while(sqlite3_step(stmt1) == SQLITE_ROW) {
    orario = [NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt1, 0)];
    idmed = [[NSNumber numberWithInt:(int)sqlite3_column_int(stmt1, 1)] intValue];
    sqlite3_prepare_v2(dbase, sql2, -1, &stmt2, NULL);
    sqlite3_bind_text(stmt2, 1, [orario UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_int(stmt2, 2, idmed);
    sqlite3_step(stmt2);
}
sqlite3_close(dbase);

3 个查询在我看来是正确的,但最后一个没有插入任何行。第二个,得到一排,所以最后一个应该可以工作(如果我直接使用它就可以)。你觉得哪里不对?

4

2 回答 2

1

您的?. 这将尝试将文字问号字符解析为日期时间。如果要?表示参数,则不能将其括在引号中。

const char *sql2 = "... VALUES (datetime(?, '+1 day'), ?, 0, 1)";
                                         ^
于 2012-07-14T09:24:47.740 回答
1

在您的代码中:

const char *sql2 = "insert into orari (orario,idmedicina,presa,forever) values (datetime('?','+1 day'),?,0,1)";

你在你的周围加上引号?(在哪里说values (datetime('?'把它改成values (datetime(?)。生成的代码应如下所示:

const char *sql2 = "insert into orari (orario,idmedicina,presa,forever) values (datetime(?,'+1 day'),?,0,1)";
于 2012-07-14T09:27:13.043 回答