我为iPhone创建了一个SQLite数据库来存储我的数据(字符串)。
我有一些包含'单引号的数据(即不要紧张)。
使用SQLite的正常''转义将其插入到数据库中。
INSERT INTO todo(test) VALUES('don''t be tense');
当我在终端I中选择数据时,可以在记录中看到单引号。
don't be tense
我的问题是当我读取记录时,单引号不在NSLog中:
dont be tense
这是在现场阅读的电话:
self.text = [NSString stringWIthUTF8String:(char *)sqlite3_column_text(init_statement, 1)];
NSLog(@"%@",self.translation);
我非常感谢如何确保报价被读入的帮助。
以下是完整代码,如果它有帮助:
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 {
self.text = @"Nothing";
}
// Reset the statement for future reuse.
sqlite3_reset(init_statement);
}
return self;
}