472,982 Members | 2,128 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,982 software developers and data experts.

How to quote text before inserting into database?

I am working with Python (psycopg). I have HTML with embedded Python that I'm
inserting into a database and it could contain any character.

Single quotes, at least, must be escaped (to two single quotes, right?) before
inserting it into Postgres.

This poses a problem when I get the data out of the table. It could have
originally contained two single quotes together and I replace them with one
single quote in the unescaping process.

How do you properly escape the special characters (and what all are they)?

TIA,
Scott

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 12 '05 #1
1 4450
Hi Scott,

Scott Chapman wrote:
I am working with Python (psycopg). I have HTML with embedded Python that I'm
inserting into a database and it could contain any character.

Single quotes, at least, must be escaped (to two single quotes, right?) before
inserting it into Postgres.

This poses a problem when I get the data out of the table. It could have
originally contained two single quotes together and I replace them with one
single quote in the unescaping process.

How do you properly escape the special characters (and what all are they)?


This is supported by psycopg. See

http://www.python.org/peps/pep-0249.html

Especially:

.execute(operation[,parameters])

Prepare and execute a database operation (query or
command). Parameters may be provided as sequence or
mapping and will be bound to variables in the operation.
Variables are specified in a database-specific notation
(see the module's paramstyle attribute for details). [5]

A reference to the operation will be retained by the
cursor. If the same operation object is passed in again,
then the cursor can optimize its behavior. This is most
effective for algorithms where the same operation is used,
but different parameters are bound to it (many times).

For maximum efficiency when reusing an operation, it is
best to use the setinputsizes() method to specify the
parameter types and sizes ahead of time. It is legal for
a parameter to not match the predefined information; the
implementation should compensate, possibly with a loss of
efficiency.

The parameters may also be specified as list of tuples to
e.g. insert multiple rows in a single operation, but this
kind of usage is depreciated: executemany() should be used
instead.

Return values are not defined.
This means, if you have to handle strings, you can use

cursor.execute("SELECT value FROM table WHERE key=%s",("your'key",))

for example.

HTH
Tino Wildenhain
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Stijn Goris | last post by:
hi all, My problem: when inserting a large text (from a textarea) into the database it just wont happen. When the text is somewhat smaller no problem occurs. The large text is about 100 lines *...
1
by: Carlos Lozano | last post by:
I need to merge the content of two richtextboxes. It can not be accomplished using the RichtTextBox.Text field because all format will be lost. So it has to be done using the RichtTextBox.Rtf...
4
by: Art | last post by:
I've already posted a number of questions on this topic and everyone has been extremely helpful. I've yet another (related) question. I want to bring a text file into an Access table using...
1
by: Ben Ramsey | last post by:
I'm using PostgreSQL on a Windows 2000 server and cygwin. All is set up just fine. The database works just fine, and ASP is connecting to the database through a DSN just fine. The only problem I...
2
by: DragonShade | last post by:
To be honest, I'm not sure if this is a .NET issue, some configuration on a PC, or a setting in a database but here goes: Environment (have NO control over): XP (London & Tokyo),.NET 2.0 C#, 2...
3
by: Mcoleman | last post by:
Hi guys, First post here, so please be gentle. I'm currently writing an Access 2000 Database, and as part of that I need some reports to make use of the information contained in the db. The...
0
by: mbenedict | last post by:
I am rather new at this code and am attempting to modify existing code to use clob datatypes, which I have never used before. The database tables have been set up for clob data. When trying to use...
2
by: AlexanderDeLarge | last post by:
Hi! I got a problem that's driving me crazy and I'm desperately in need of help. I'll explain my scenario: I'm doing a database driven site for a band, I got these tables for their discography...
5
by: atlanteavila | last post by:
Hello all, I have a problem with inserting text with quotes, or html code in to a MYSql Database. I've been trying to create my own content management system, and unfortunately I've come accross a...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.