error on importing variable value

2007-12-29 Thread int32bit

I can't figure out why this doesn't work. Any ideas appreciated.

conn = MySQLdb.connect (db = "vocab")
cursor = conn.cursor ()
cursor.execute ("SELECT VERSION()")
row = cursor.fetchone ()
print "server version:", row[0]
cursor.close ()
conn.close ()

gives:

server version: 5.0.44-log

but

import defs
conn = MySQLdb.connect (defs.connect)
cursor = conn.cursor ()
cursor.execute ("SELECT VERSION()")
row = cursor.fetchone ()
print "server version:", row[0]
cursor.close ()
conn.close ()

where defs.py is

connect = 'db = "vocab"'

gives:

Traceback (most recent call last):
  File "./add_words", line 17, in ?
  conn = MySQLdb.connect (defs.connect)
  File "/usr/lib/python2.4/site-packages/MySQLdb/__init__.py", line
74, in Connect
  return Connection(*args, **kwargs)
  File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py",
line 170, in __init__
  super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2005, 'Unknown MySQL server host
\'db = "vocab"\' (3)')





-- 
http://mail.python.org/mailman/listinfo/python-list


Re: error on importing variable value

2007-12-30 Thread int32bit
On Dec 29, 6:05 pm, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote:
> On Sat, 29 Dec 2007 15:31:30 -0800 (PST), [EMAIL PROTECTED] declaimed
> the following in comp.lang.python:
>
>
>
> > I can't figure out why this doesn't work. Any ideas appreciated.
>
> > conn = MySQLdb.connect (db = "vocab")
>
> This is a keyword parameter association, the parameter named "db" is
> given the string value "vocab".
>
> > import defs
> > conn = MySQLdb.connect (defs.connect)
> > where defs.py is
>
> > connect = 'db = "vocab"'
>
> This is a string.  You'd get the same error using:
>
> conn = MySQLdb.connect('db="vocab"')
>
> as you are giving the entire string to whatever the first defined
> parameter in .connect() is...
>
> Change defs.py to:
>
> -=-=-=-=-
> connect = { "db" : "vocab" }
>
> and change the connection to read:
>
> -=-=-=-=-
> conn = MySQLdb.connect(**defs.connect)
>
> to force keyword unpacking of the dictionary
>
> --
> WulfraedDennis Lee Bieber   KD6MOG
> [EMAIL PROTECTED]  [EMAIL PROTECTED]
> HTTP://wlfraed.home.netcom.com/
> (Bestiaria Support Staff:   [EMAIL PROTECTED])
> HTTP://www.bestiaria.com/


Thanks. This works great. As a side note, it can also be extended so
that if defs.py is

connect = { "host" : "localhost", "user" : "joey", "db" : "vocab" }

the MySQLdb.connect(**defs.connect) still works.





-- 
http://mail.python.org/mailman/listinfo/python-list