473,225 Members | 1,298 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

error on importing variable value


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
3 1625
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jason Charalambides | last post by:
Is there a way I can import a given value to a variable from the main form to a module? I wanted to have some routines that are repeated set in a module subroutine. However, the value of a...
2
by: steve | last post by:
Hello, I am trying to import an image file into a form. This would be a persons picture saved in the same directory for every unique record. I don't have any problems making an action button to...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
4
by: rshepard | last post by:
I'm stymied by what should be a simple Python task: accessing the value of a variable assigned in one module from within a second module. I wonder if someone here can help clarify my thinking. I've...
3
by: rs387 | last post by:
Hi, I've found the following behaviour on importing a variable from a module somewhat odd. The behaviour is identical in Python 2.5 and 3.0b2. In summary, here's what happens. I have a...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.