PQescapeStringConn

From: Scott Frankel <frankel(at)circlesfx(dot)com>
To: PostgreSQL List <pgsql-general(at)postgresql(dot)org>
Subject: PQescapeStringConn
Date: 2010-07-30 06:52:15
Message-ID: 76D0DBC1-AE15-452A-A614-5E5D2B4546D1@circlesfx.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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/'

);

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Richard Huxton 2010-07-30 08:13:17 Re: PQescapeStringConn
Previous Message A. Kretschmer 2010-07-30 06:11:29 Re: How to improve: performance of query on postgresql 8.3 takes days