2010-12-02 10 views
2

データ(文字列)を格納するための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; 
} 
+0

良い編集は、質問が今よりはるかに意味があります。私は前者が本当に混乱していた "私はレコード内の一重引用符を見ることができます。緊張しないでください"。 :) –

+0

データベースとテストエントリを作成するためのsqlite3スクリプトを提供できますか?あなたのコードは、私のように作成した私のテストデータベースでうまく働いていました。 'sqlite3 so_squot.db create table TODO(pk integer主キーasc、text text not null); TODO値に挿入する(1、 '緊張しないでください'); .quit' –

+0

こんにちはジェレミー、お試しいただきありがとうございます。 – Brian

答えて

0

を、私はそれを修正:読み込まれ、それが助け場合は、以下の

は、完全なコードです。 私はデータベースを更新していたときに古いものをシミュレータから削除しなかったので、古いデータを使ってテストしていたようです。愚かな間違い! したがって、SQLiteからシングルクォートを読むのに問題はありません(おそらく私はそれについての情報を見つけることができませんでした)。 読んで助けようとした人に感謝します!

関連する問題