10

postgres を初めて使用します。where 句のMESSAGEパラメータでRTF 構文をエスケープするにはどうすればよいですか?

select count(*) 
from communicationoutgoingmessagescheduledetails 
where email='chris.green@waynesreaves.com' 
and message='{\rtf1\deff0{\fonttbl{\f0 Times New Roman;}}{\colortbl\red0\green0\blue0 ;\red0\green0\blue255 ;}{\*\listoverridetable}{\stylesheet {\ql\cf0 Normal;}{\*\cs1\cf0 Default Paragraph Font;}{\*\cs2\sbasedon1\cf0 Line Number;}{\*\cs3\ul\cf1 Hyperlink;}}\splytwnine\sectd\pard\plain\ql{\cf0 first }{\b\cf0 paymen}{\b\cf0 t bold }{\cf0 rem}{\cf0 inder}\par\pard\plain\ql{\ul\cf0 se}{\ul\cf0 cond PAYMENT }{\b\ul\cf0 reminder}\b\ul\par}' 
and succeddful=-1 
and senttime::timestamp::date='2/7/2013 12:00:00 AM'

アップデート

これは私が得ているエラーです

WARNING:  nonstandard use of escape in a string literal LINE 4: and message='{\rtf1\deff0{\fonttbl{\f0 Times New Roman;}}{\c...

HINT:  Use the escape string syntax for escapes, e.g., E'\r\n'. 
ERROR:  invalid Unicode escape LINE 4: and message='{\rtf1\deff0{\fonttbl{\f0 Times New Roman;}}{\c...

HINT:  Unicode escapes must be \uXXXX or \UXXXXXXXX.
********** Error **********

ERROR: invalid Unicode escape SQL state: 22025 Hint: Unicode escapes must be \uXXXX or \UXXXXXXXX. Character: 124

更新 2

これは、PostgreSQL から結果セットを取得するために実行しているコードです。

string sql = "select count(*) from communicationoutgoingmessagescheduledetails " +
                        "where email='" + email + "' ";

        if (message != string.Empty)
            sql += String.Format("and message='{0}' ", message);

        if (subject != string.Empty)
            sql += String.Format("and subject='{0}' ", subject);

        sql += "and successful=true " +
        "and senttime::timestamp::date='" + date.Date + "'";


        System.Data.IDbConnection connection = ORGen.Access.Config.Instance.Connection;
        IDbCommand command = connection.CreateCommand();

        connection.Open();
        command.CommandText = sql;
        bool retVal = Convert.ToInt16(command.ExecuteScalar()) > 0 ? true : false;
        connection.Close();

        command = null;
        connection = null;
4

1 に答える 1

20

PostgreSQL 9.3 以降で手動でstandard_conforming_strings設定を変更していない場合は、一重引用符以外はエスケープする必要はありません。次のように二重にします。

'this is a single quote: ''. '

古いバージョンを使用している場合は、 inに設定standard_conforming_stringsするか、バックスラッシュを 2 つ使用して、文字列表記を使用します。onpostgresql.confE''

E'{\\rtf1'

これはすべて、ユーザーマニュアルの字句構造セクションで説明されています。

ただし、実際には言語の PostgreSQL クライアント ドライバーを使用して、自動的にエスケープを処理するパラメーター化されたステートメントを送信する必要があります。そうしないと、 SQL インジェクションホールが発生する可能性があります。このサイトを参照してください。

npgsql で C# を使用しているため、おそらくhttp://bobby-tables.com/csharp.htmlが必要です。詳細については、npgsql ユーザー マニュアルを参照してください。

于 2013-02-07T10:13:42.670 に答える