I have created an SQLite db for iPhone to store my data (strings). I have some data that contains ' single quotes (i.e. don't be tense). This was inserted into the DB using the normal '' escape for SQLite.
INSERT INTO todo(test) VALUES('don''t be tense');
When I do a select on the data in Terminal I can see the single quote in the record.
don't be tense
My problem is when I read the record in, the single quote is not there in the NSLog:
dont be tense
This is the call to read in the field:
self.text = [NSString stringWIthUTF8String:(char *)sqlite3_column_text(init_statement, 1)];
NSLog(@"%@",self.translation);
I would greatly appreciate help on how to ensure the quote is read in.
Below is the full code, if it helps:
static sqlite3_stmt *init_statement = nil;
@implementation Todo
@synthesize primaryKey,text;
- (id)initWithPrimaryKey:(NSInteger)pk database:(sqlite3 *)db {
if (self = [super init]) {
primaryKey = pk;
database = db;
// Compile the query for retrieving book data. See insertNewBookIntoDatabase: for more detail.
if (init_statement == nil) {
// Note the '?' at the end of the query. This is a parameter which can be replaced by a bound variable.
// This is a great way to optimize because frequently used queries can be compiled once, then with each
// use new variable values can be bound to placeholders.
const char *sql = "SELECT text FROM todo WHERE pk=?";
if (sqlite3_prepare_v2(database, sql, -1, &init_statement, NULL) != SQLITE_OK) {
NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
}
// For this query, we bind the primary key to the first (and only) placeholder in the statement.
// Note that the parameters are numbered from 1, not from 0.
sqlite3_bind_int(init_statement, 1, primaryKey);
if (sqlite3_step(init_statement) == SQLITE_ROW) {
self.text = [NSString stringWithUTF8String:(char *)sqlite3_column_text(init_statement, 0)];
NSLog(@"%@",self.text);
} else {开发者_Python百科
self.text = @"Nothing";
}
// Reset the statement for future reuse.
sqlite3_reset(init_statement);
}
return self;
}
I fixed it. It seems that when I was updating the database I didn't delete the old one off the simulator so it was testing with old data the entire time. Stupid mistake! So there is no issue reading in single quotes from SQLite (probably why I could never find information about it). Thanks to those who read and tried to help!
精彩评论