SQLite binding function in prepared statement

2019-07-19 16:52发布

问题:

I've problem with binding current date. I want to use datetime('now') function as one of inserted value.

I've used something like this:

sqlite3_bind_text(stmt, i + 1, values[i], -1, SQLITE_STATIC);

where values[i] is char * text = datetime('now'). But obviously it inserts that text. Is there possibility to bind function like that: datetime('now')?

回答1:

Binding, by definition, nicely escapes everything and makes sure everything is a string that the SQL interpreter doesn't actually misread as an SQL component. It's a data safety issue.

Instead, make your stmt variable put the datetime('now') directly where it should be in the original SQL expression. IE, remove the related ? and put datetime('now') in its place.