473,804 Members | 2,194 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

baffling sql string

Following is a code snippet from a pythoncard app
the problem is with the sql string called iq1
If either mysubject or mytalktext contains an
apostrophe the update fails :
Example: mysubject="Let' s Eat" this fails
mysubject="Lets Eat" this works fine

What options do I have to avoid this issue but still
can use apostrophes in my input data ?

mysubject=self. components.Text Field1.text
mytalktext=self .components.Tex tArea1.text
mymsgno=self.my amsgno
iq1="update MSGTALK set msgdate='NOW',s ubject='%s',tal ktext='%s' where
msgno= %d " % (mysubject,myta lktext,mymsgno)
try:
self.cur.execut e(iq1)
con1.commit()
self.components .TextArea2.appe ndText('Update ok\n')
except:
self.components .TextArea2.appe ndText('Problem during update command\n')
self.components .TextArea2.appe ndText(iq1)
(hope the intendation did not get too messed up )

Thanks
Sep 27 '06 #1
5 1252
DarkBlue <no****@nixmail .comwrote:
iq1="update MSGTALK set msgdate='NOW',s ubject='%s',tal ktext='%s' where
msgno= %d " % (mysubject,myta lktext,mymsgno)
try:
self.cur.execut e(iq1)
Use parameterised queries and get rid of the quotes in the SQL:

iq1="update MSGTALK set msgdate='NOW',s ubject=%s,talkt ext=%s where
msgno= %d "
try:
self.cur.execut e(iq1, (mysubject,myta lktext,mymsgno) )
....

depending on your actual database you might need to use something other
than %s to specify the parameters. Check out 'paramstyle' for your database
connection.
Sep 27 '06 #2

DarkBlueFollowi ng is a code snippet from a pythoncard app
DarkBluethe problem is with the sql string called iq1
DarkBlueIf either mysubject or mytalktext contains an
DarkBlueapostro phe the update fails :
DarkBlueExample : mysubject="Let' s Eat" this fails
DarkBlue mysubject="Lets Eat" this works fine

DarkBlueWhat options do I have to avoid this issue but still can use
DarkBlueapostro phes in my input data ?

You don't mention what database adapter you're communicating with, but they
all have argument quoting facilities. You should be using them instead of
doing the string interpolation yourself.

For example, if I was using MySQL, my code might look something like this:

self.cur.execut e("update MSGTALK"
" set msgdate='NOW', subject=%s, talktext=%s"
" where msgno=%s",
(self.component s.TextField1.te xt,
self.components .TextArea1.text ,
self.myamsgno))

Skip
Sep 27 '06 #3
DarkBlue wrote:
>
Example: mysubject="Let' s Eat" this fails
mysubject="Lets Eat" this works fine

What options do I have to avoid this issue but still
can use apostrophes in my input data ?
Use proper "bind parameters" or "bind variables" when executing the
statement, rather than using string substitution/interpolation.

[...]
iq1="update MSGTALK set msgdate='NOW',s ubject='%s',tal ktext='%s' where
msgno= %d " % (mysubject,myta lktext,mymsgno)
This merely "edits" a statement, stuffing values directly into the
text. As you've noticed, merely substituting values for placeholders
isn't always going to work. Moreover, it's something that could cause
errors (in the nicest circumstances such as in a local application) or
security holes (in nasty circumstances such as in networked or Web
applications).

Instead try this:

iq1="update MSGTALK set msgdate='NOW',s ubject=%s,talkt ext=%s" \
" where msgno=%d" # note that the parameters are not substituted here

Then execute the statement with the parameters as a separate argument:

self.cur.execut e(iq1, [mysubject,mytal ktext,mymsgno])

The database module will then ensure that the values you've supplied
really will get used properly when executing the statement, and things
like apostrophes won't cause any problems at all.

Paul

P.S. The above assumes that the "parameter style" of the database
module is "format", as found by looking at the paramstyle attribute of
the module (eg. MySQLdb.paramst yle). Other styles include "qmark" where
you use "?" instead of "%s" (or other format codes) to indicate where
your values will be used in a statement.

P.P.S. See also the DB-API 2.0 specification:
http://www.python.org/dev/peps/pep-0249/

Sep 27 '06 #4
DarkBlue wrote:
Following is a code snippet from a pythoncard app
the problem is with the sql string called iq1
If either mysubject or mytalktext contains an
apostrophe the update fails :
Example: mysubject="Let' s Eat" this fails
mysubject="Lets Eat" this works fine

What options do I have to avoid this issue but still
can use apostrophes in my input data ?

mysubject=self. components.Text Field1.text
mytalktext=self .components.Tex tArea1.text
mymsgno=self.my amsgno
iq1="update MSGTALK set msgdate='NOW',s ubject='%s',tal ktext='%s' where
msgno= %d " % (mysubject,myta lktext,mymsgno)
Your SQL after the % formatting will read something like this:
update ... set ... subject='Let's Eat',talktext=. ...
which is illegal SQL syntax -- if a string constant contains an
apostrophe, it must be doubled:
... subject='Let''s Eat', ...
which would require you to do data.replace("' ", "''") on each text
column. In general, % formatting is *not* a good idea, for this reason
and also because it leaves you wide open to an SQL injection attack. It
is much better to use the placeholder system, and let the called
software worry about inserting apostrophes, converting date formats,
etc etc. So:

iq1="""\
update MSGTALK
set msgdate='NOW', subject=?, talktext=?
where msgno= ?
"""

self.cur.execut e(iq1, (mysubject,myta lktext,mymsgno) )

Your [unspecified] DBMS adaptor may use some other placeholdet than
"?"; this will be documented in its manual .....

HTH,
John

Sep 27 '06 #5
Duncan Booth wrote:
...

depending on your actual database you might need to use something other
than %s to specify the parameters. Check out 'paramstyle' for your
database connection.

Thank you all for prompt suggestions

I am using firebird 1.5.3 with kinterbasdb

Db
Sep 27 '06 #6

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

Similar topics

5
4596
by: TimS | last post by:
I am getting a baffling File not found error (0x800A0035). I am writing asp on a windows 2000 server. I build a path and filename from several fields in a jet database using SQL commands, like this: Sql = "SELECT dirs.rootname,dirs.dirname FROM dirs" RS.open Sql RS.movefirst do while not RS.EOF temp1=trim(RS("rootname"))
8
1980
by: Michelle | last post by:
I have tried every variation of the "onchange" statement below without any actual reloading of the page: The FireFox javaScript Console reports the following message: Error: missing : in conditional expression Source Code: location.href=themes.php?scheme=(form.color_schemes.options.value) My Code for the dropdown box is: <form name='color_schemes' method='post' action='themes.php' >
2
1819
by: bfrasca | last post by:
I have a well-formed XML file that I am trying to load into a dataset using the ReadXML method. It keeps telling me that "there are multiple root elements". I can view the data using XML Designer in Visual Studio 2003 without any problems and the schema and file have been validated using several different tools. I even successfully generated a typed dataset with Visual Studio 2003 by importing the XML into my project. I'm baffled, does...
1
1367
by: WillWeGetOurFreedomBack | last post by:
When I try to run the query below, I get a warning that says Access could not insert ANY of the records due to Validation rule violations. Access gives me NO hint WHICH fields are violating the rules or WHAT rules are being violated! Furthermore, I re-checked EVERY field in the target table and there ARE NO VALIDATION RULES SET on ANY field! There is no conflict between source and target data types on any field and NON of the target...
12
1519
by: Mike | last post by:
I have posted this before, but no one responds, so here it goes again. I think my question is intimidating people and I have no idea why?!? This is not a new concept. You can do this with other programming languages, so why is C# so different? Again, here is my baffling C# question. I have C# code that opens an application, but right after it opens I am confronted by prompts for information (i.e. "Please enter your id"). I would like to...
4
1532
by: Steve | last post by:
I'm currently running into a problem, and I have no idea what to make of it. I have a class with nested clases and static properties that I'm using to store configuration information using a custom configuration section. When my code accesses one of the static properties the static constructors are fired, and the code goes in and pulls the data from the configuration section. In the current instance, I'm getting a connection string....
2
1368
by: Double Echo | last post by:
Hi, Forgive me if I ask a dumb question, I'm new to classes and object programming with PHP, and cannot figure out what the problem is with this class. Maybe I have overlooked something but just can't see it. The error message usually means a typo or something that I missed. I get this error: Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in crypt_class.php on line 6
2
1258
by: CharlesA | last post by:
hi folks, I'm using the .Net framework 1.1 with ASP.net and C#: this is what I'm trying to do... I've got a repeater template working that creates a hyperlink with a javascript function call embedded inside it when the user clicks on the link, it populates a textbox with a value specific to that link (i.e a person's ID)...I can make this work in a standalone browser window, but I'm trying to stuff this functionality at run
1
1293
by: Tim Redfern | last post by:
Hi I'm having an issue that is probably obvious to somebody here but its baffling me.. I'm using python (2.5.1) on an embedded linux sytem system (kernel 2.6.19 I think) with only 32MB memory (for an art project). My python code runs nicely when launched manually from a login shell, ps shows its using 11MB or so of memory. However, when I try to launch the same code from a startup script, it
0
10603
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10353
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10356
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10099
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9176
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7643
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.