Hi all,

What's the best way to insert long strings that contain numerous special characters into a PG database?

I'm assuming that importing an SQL script with prepared statements is the way to go. If so, how to escape all the special characters?

I've found documentation on PQescapeStringConn but haven't found any examples of it in use.

I have a number of very long strings that each contain many instances of semi-colons, single quotes, forward and back slashes, etc. I'm looking for an efficient and safe way to write them to my db using a prepared statement.

An example follows.

Thanks in advance!
Scott


CREATE TABLE foo (
        foo_id                          SERIAL          PRIMARY KEY,
        name                            VARCHAR(32)     UNIQUE NOT NULL,
        description                     TEXT,
        body                            TEXT            DEFAULT NULL,
        created                         timestamp       DEFAULT 
CURRENT_TIMESTAMP,
        UNIQUE                          (name));


PREPARE fooprep (VARCHAR(32), text, text) AS
    INSERT INTO foo (name, description, body) VALUES ($1, $2, $3);
EXECUTE fooprep('foo1', 'this is foo1',

'#!()[]{};
qwe'poi'asdlkj"zxcmnb";
/\1\2\3\4\5\6\7\8\9/'

);



Reply via email to