データ(文字列)を格納するためのiPhone用SQLite dbを作成しました。 私は、一重引用符(すなわち、緊張しないでください)を含むいくつかのデータを持っています。 これは、通常の '' SQLiteのエスケープを使用してDBに挿入されました。sqlite for iPhoneからシングルクォートエンコーディングの問題を読み取る
INSERT INTO todo(test) VALUES('don''t be tense');
私は、ターミナル内のデータに選択を行うと、私はレコード内の単一引用符を見ることができます。
dont be tense
これはフィールドに読み込むための呼び出しは次のとおりです:
私はレコードを読んだとき don't be tense
私の問題は、単一引用符は、のNSLogではありません、です
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;
}
良い編集は、質問が今よりはるかに意味があります。私は前者が本当に混乱していた "私はレコード内の一重引用符を見ることができます。緊張しないでください"。 :) –
データベースとテストエントリを作成するためのsqlite3スクリプトを提供できますか?あなたのコードは、私のように作成した私のテストデータベースでうまく働いていました。 'sqlite3 so_squot.db create table TODO(pk integer主キーasc、text text not null); TODO値に挿入する(1、 '緊張しないでください'); .quit' –
こんにちはジェレミー、お試しいただきありがとうございます。 – Brian