By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,911 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,911 IT Pros & Developers. It's quick & easy.

error on importing variable value

P: n/a

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)')

Dec 29 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
CS
I'm new to programming and I'm trying to find some answers. I wrote a few
python cgi scripts for my website all of which access a mysql db on
'localhost'. My question is, Is it a bad idea to have my username and
password for my db coded in my script? Is there a better way to make sure
that information can't be acessed? Obviously I wan't to make sure that my
*.py can't be downloaded from /cgi-bin and give anyone access to my db's.

Cory

Dec 29 '07 #2

P: n/a
On 29 dic, 20:31, int32...@yahoo.com wrote:
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)
[...]
where defs.py is

connect = 'db = "vocab"'

gives:

Traceback (most recent call last):
* * * * _mysql_exceptions.OperationalError: (2005, 'Unknown MySQL server host
\'db = "vocab"\' (3)')
Try this:

defs.py:
dbname = "vocab"

import defs
conn = MySQLdb.connect(db=defs.dbname)

BTW, please read the Style Guide at http://www.python.org/dev/peps/pep-0008
- in particular, I feel space before an opening parens rather
annoying. But it's just a matter of style.

--
Gabriel Genellina
Dec 30 '07 #3

P: n/a
On Dec 29, 6:05 pm, Dennis Lee Bieber <wlfr...@ix.netcom.comwrote:
On Sat, 29 Dec 2007 15:31:30 -0800 (PST), int32...@yahoo.com 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

--
Wulfraed Dennis Lee Bieber KD6MOG
wlfr...@ix.netcom.com wulfr...@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-a...@bestiaria.com)
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.

Dec 30 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.