PQescapeStringConn - Mailing list pgsql-general

From Scott Frankel
Subject PQescapeStringConn
Date
Msg-id 76D0DBC1-AE15-452A-A614-5E5D2B4546D1@circlesfx.com
Whole thread Raw
Responses Re: PQescapeStringConn  (Richard Huxton <dev@archonet.com>)
List pgsql-general

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/'

);



pgsql-general by date:

Previous
From: "A. Kretschmer"
Date:
Subject: Re: How to improve: performance of query on postgresql 8.3 takes days
Next
From: Richard Huxton
Date:
Subject: Re: PQescapeStringConn