Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > [SQL] Right way to set a variable to NULL?

Reply
Thread Tools

[SQL] Right way to set a variable to NULL?

 
 
Gilles Ganault
Guest
Posts: n/a
 
      12-26-2008
Hello

I use regexes to extract information from a text file. Some of the
records don't have e-mails or www addresses, so those must match Null
in SQL, but None doesn't work as expected:

=======
if itemmatch:
web = itemmatch.group(1).strip()
else:
web = None

sql = 'INSERT INTO mytable (name,address,web,mail) VALUES
("%s","%s","%s","%s","%s")' % (name,address,web,mail)
=======

Is there a better way in Python to have a variable match NULL than
building the SQL query step by step?

Thank you.
 
Reply With Quote
 
 
 
 
Martin
Guest
Posts: n/a
 
      12-26-2008
Hi,

I'd create a simple wrapper object

class MailAddress(object):
def __init__(self, address=None):
self.address = address
def __str__(self):
if address:
return self.adress
return "NULL"

you can keep most of your code just replace the original instantiation
of the mail str-instance with the MailAddress

This is just scratched up quickly. Hope it helps

Martin

2008/12/26 Gilles Ganault <>:
> sql = 'INSERT INTO mytable (name,address,web,mail) VALUES
> ("%s","%s","%s","%s","%s")' % (name,address,web,mail)
> =======
>
> Is there a better way in Python to have a variable match NULL than
> building the SQL query step by step?
>
> Thank you.
> --
> http://mail.python.org/mailman/listinfo/python-list
>




--
http://soup.alt.delete.co.at
http://www.xing.com/profile/Martin_Marcher
http://www.linkedin.com/in/martinmarcher

You are not free to read this message,
by doing so, you have violated my licence
and are required to urinate publicly. Thank you.

Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html
 
Reply With Quote
 
 
 
 
John Machin
Guest
Posts: n/a
 
      12-26-2008
On Dec 27, 7:33*am, Gilles Ganault <nos...@nospam.com> wrote:
> Hello
>
> I use regexes to extract information from a text file. Some of the
> records don't have e-mails or www addresses, so those must match Null
> in SQL, but None doesn't work as expected:
>
> =======
> * * * * if itemmatch:
> * * * * * * * * web = itemmatch.group(1).strip()
> * * * * else:
> * * * * * * * * web = None
>
> * * * * sql = 'INSERT INTO mytable *(name,address,web,mail) VALUES
> ("%s","%s","%s","%s","%s")' % (name,address,web,mail)
> =======


You have MULTIPLE problems here.

PROBLEM 1:
That code won't even create the "sql" string; you have %s 5 times, but
only 4 elements in the tuple. When asking for help, always post (1)
the code that you actually ran (2) the result or error message that
you got [merely saying "doesn't work" is not very useful at all].

PROBLEM 2:
Having fixed problem 1, the result is not valid SQL; you get VALUES
("Gilles", ...; it should be VALUES('Gilles', ...

PROBLEM 3:
Having fixed problem 2: If you have a name like "L'Hopital" or
"O'Reilly" the result is VALUES('L'Hopital', ...; it should be VALUES
('L''Hopital', ...

*AND* you don't have to worry about all the rules for SQL constant
values; the worrying and work has been done for you.

> Is there a better way in Python to have a variable match NULL than
> building the SQL query step by step?


Yes. Don't construct the SQL query by Python string formatting. In
fact, don't *ever* construct *any* SQL query that needs args by using
string formatting/interpolation in *any* language, even when you think
it's "working" -- see http://en.wikipedia.org/wiki/SQL_injection ...
search for "SQL injection attack" for more references.

Essential reference: the Python Database API Specification v2.0
(http://www.python.org/dev/peps/pep-0249/)

You need something like:
sql = "INSERT INTO mytable (name,address,web,mail) VALUES(?,?,?,?)"
cursor.execute(sql, (name, address, web, mail))

Some database software uses something other than ? to mark parameter
positions; you may need (for example) ... VALUES(%s,%s,%s,%s) ...
(*NOT* the same meaning as %s in Python!). Read about "paramstyle" in
the DB API spec, and check the documentation for the DB software that
you are using.

HTH,
John
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      12-26-2008
On Dec 27, 8:16*am, Scott David Daniels <Scott.Dani...@Acm.Org> wrote:
> Martin wrote:
> > ...
> > class MailAddress(object):
> > * def __init__(self, address=None):
> > * * self.address = address
> > * def __str__(self):
> > * * if address:
> > * * * return self.adress
> > * * return "NULL"

>
> There is an obvious typo above:
> *> * * if address:
> should be:
> * * * *if self.address:
>
> Or, you could replace the __str__ function with:
> * * *def __str__(self):
> * * * * *return self.address or "NULL"


The above all have the same characteristic: if the input is a zero-
length string, then NULL is inserted into the database instead of a
zero-length string. Some folks (not just pedants!) regard that as an
important difference.

 
Reply With Quote
 
Martin
Guest
Posts: n/a
 
      12-27-2008
2008/12/26 John Machin <>:
> The above all have the same characteristic: if the input is a zero-
> length string, then NULL is inserted into the database instead of a
> zero-length string. Some folks (not just pedants!) regard that as an
> important difference.


agreed but I understood the OP specifically wanted NULL and not ''.

of course for data gathering in web apps I'd personally make the mail
attribute a NOT NULL and reject anybody who wouldn't give me their
mail address. After all mail is the way to get in touch with my
customers/user if they register on my site, and if it's only for a
password reset link.

/martin


--
http://soup.alt.delete.co.at
http://www.xing.com/profile/Martin_Marcher
http://www.linkedin.com/in/martinmarcher

You are not free to read this message,
by doing so, you have violated my licence
and are required to urinate publicly. Thank you.

Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      12-27-2008
On Dec 27, 11:05*am, Martin <mar...@marcher.name> wrote:
> 2008/12/26 John Machin <sjmac...@lexicon.net>:
>
> > The above all have the same characteristic: if the input is a zero-
> > length string, then NULL is inserted into the database instead of a
> > zero-length string. Some folks (not just pedants!) regard that as an
> > important difference.

>
> agreed but I understood the OP specifically wanted NULL and not ''.


He wanted None inserted into the database as NULL if his regex didn't
match. He didn't invite you to change a matching '' to NULL with no
announcement.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
Is TabIndex the right way to set the order of controls? David Thielen ASP .Net Web Controls 1 02-28-2007 04:27 AM
Object variable or With Block variable not set error =?Utf-8?B?TmV3YmllRGV2?= ASP .Net 0 04-26-2006 03:11 AM
Object variable or With block variable not set Neo Geshel ASP .Net 6 03-23-2006 06:50 AM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57