Inserting nil Values into Sqlite Database?

2019-08-23 10:03发布

I am working on an iPhone App where I am pulling data from an XML file and inserting it into a sqlite database located in the App. I am able to successfully do this process, but it appears that if I try to sqlite3_bind_text with a NSString that has a value of "nil", the App quickly dies.

This is an example of code that fails: (modified for this example)

// idvar = 1
// valuevar = nil

const char *sqlStatement = "insert into mytable (id, value) VALUES(?, ?)";
sqlite3_stmt *compiledStatement = nil;  
sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL);

sqlite3_bind_int(compiledStatement, 1, [idvar intValue]);;
sqlite3_bind_text(compiledStatement, 2, [valuevar UTF8String], -1, SQLITE_TRANSIENT);

1条回答
闹够了就滚
2楼-- · 2019-08-23 10:23

Have you tried something like:

sqlite3_bind_text(compiledStatement, 2, valuevar==nil?"":[valuevar UTF8String], -1, SQLITE_TRANSIENT);
查看更多
登录 后发表回答