Error connecting to MySQL from Python

C

carlos.ortiz.asm

Hello guys I am currently working in a python project at my school. First Iwant to make clear that I'm not a python programmer (I was just called to put out the flames in this project because no one else would and I was brave enough to say yes).

I have the following problem here. I have to write a method that connects to an existing localhost MySQL database (I'm using connector version 1.0.12)and then does pretty basic stuff. The parameters are sent by a GTK-writtenGUI (I didn't write that interface). So I wrote my method like this:

--------------------------PYTHON CODE----------------------------------------
def compMySQL(self, user, database, password, db_level, table_level, column_level):
sql_page_textview = self.mainTree.get_widget('sql_text_view')
sql_page_textview.modify_font(pango.FontDescription("courier 10"))
sql_page_buffer = sql_page_textview.get_buffer()

#Gonna try connecting to DB
try:
print("Calling conn with U:{0} P:{1} DB:{2}".format(user,password,database))
cnxOMC = mysql.connector.connect(user, password,'localhost',database)
except:
print "Error: Database connection failed. User name or Database name may be wrong"
return

#More code ...
------------------------END OF PYTHON CODE-------------------------------------


But when I run my code I get this:

-----------------------------CONSOLE OUTPUT------------------------------------
Calling conn with U:root P:pK17LP12r DB:TESTERS
Error: Database connection failed. User name or Database name may be wrong
---------------------------END OF COLSOLE OUTPUT-------------------------------

And I don't know why, since the arguments sent are the same arguments that get printed (telling me that the GUI the other guy coded works fine) and they are valid login parameters. If I hardcode the login parameters directly insetad of using the GUI everything goes ok and the functions executes properly; the following code executes nice and smooth:

--------------------------PYTHON CODE-----------------------------------------
def compMySQL(self, user, database, password, db_level, table_level, column_level):
sql_page_textview = self.mainTree.get_widget('sql_text_view')
sql_page_textview.modify_font(pango.FontDescription("courier 10"))
sql_page_buffer = sql_page_textview.get_buffer()

#Gonna try hardcoding
try:
#print("Calling conn with U:{0} P:{1} DB:{2}".format(user,password,database))
cnxOMC = mysql.connector.connect(user="root", password='PK17LP12r',host='localhost',database='TESTERS')
print 'No prob with conn'
except:
print "Error: Database connection failed. User name or Database name may be wrong"
return

#more code ...
----------------------------END OF PYTHON CODE----------------------------------

Console output:

--------------------------------CONSOLE OUTPUT----------------------------------
No prob with conn
--------------------------------END OF CONSOLE OUTPUT---------------------------


Any ideas guys? This one is killing me. I'm just learning Python but I imagine the problem to be something very easy for a seasoned python developer so any help would be strongly appreciated.

Thanks in advance.
 
J

Jussi Piitulainen

So I wrote my method like this: ....
cnxOMC = mysql.connector.connect(user,
password,
'localhost',
database) ....
the following code executes nice and smooth: ....
cnxOMC = mysql.connector.connect(user="root",
password='PK17LP12r',
host='localhost',
database='TESTERS')

You pass the hardcoded parameters as keyword arguments, unlike in the
version that doesn't work. Maybe that is the difference. Try this:

cnxOMC = mysql.connector.connect(user=user,
password=password,
host='localhost',
database=database)

It only looks funny. In "user=user" the first "user" is a parameter
name, the other is the variable in your code.

Try help(mysql.connector.connect) at the interactive prompt, or
otherwise check the documentation.
 
M

MRAB

Hello guys I am currently working in a python project at my school. First I want to make clear that I'm not a python programmer (I was just called to put out the flames in this project because no one else would and I was brave enough to say yes).

I have the following problem here. I have to write a method that connects to an existing localhost MySQL database (I'm using connector version 1.0.12) and then does pretty basic stuff. The parameters are sent by a GTK-written GUI (I didn't write that interface). So I wrote my method like this:

--------------------------PYTHON CODE----------------------------------------
def compMySQL(self, user, database, password, db_level, table_level, column_level):
sql_page_textview = self.mainTree.get_widget('sql_text_view')
sql_page_textview.modify_font(pango.FontDescription("courier 10"))
sql_page_buffer = sql_page_textview.get_buffer()

#Gonna try connecting to DB
try:
print("Calling conn with U:{0} P:{1} DB:{2}".format(user,password,database))
cnxOMC = mysql.connector.connect(user, password,'localhost',database)
except:
print "Error: Database connection failed. User name or Database name may be wrong"
return

#More code ...
------------------------END OF PYTHON CODE-------------------------------------


But when I run my code I get this:

-----------------------------CONSOLE OUTPUT------------------------------------
Calling conn with U:root P:pK17LP12r DB:TESTERS
Error: Database connection failed. User name or Database name may be wrong
---------------------------END OF COLSOLE OUTPUT-------------------------------

And I don't know why, since the arguments sent are the same arguments that get printed (telling me that the GUI the other guy coded works fine) and they are valid login parameters. If I hardcode the login parameters directly insetad of using the GUI everything goes ok and the functions executes properly; the following code executes nice and smooth:

--------------------------PYTHON CODE-----------------------------------------
def compMySQL(self, user, database, password, db_level, table_level, column_level):
sql_page_textview = self.mainTree.get_widget('sql_text_view')
sql_page_textview.modify_font(pango.FontDescription("courier 10"))
sql_page_buffer = sql_page_textview.get_buffer()

#Gonna try hardcoding
try:
#print("Calling conn with U:{0} P:{1} DB:{2}".format(user,password,database))
cnxOMC = mysql.connector.connect(user="root", password='PK17LP12r',host='localhost',database='TESTERS')
print 'No prob with conn'
except:
print "Error: Database connection failed. User name or Database name may be wrong"
return

#more code ...
----------------------------END OF PYTHON CODE----------------------------------

Console output:

--------------------------------CONSOLE OUTPUT----------------------------------
No prob with conn
--------------------------------END OF CONSOLE OUTPUT---------------------------


Any ideas guys? This one is killing me. I'm just learning Python but I imagine the problem to be something very easy for a seasoned python developer so any help would be strongly appreciated.
In the first example you're using positional arguments, whereas in the
second example you're using keyword arguments. Are you sure that the
positional arguments are in the correct order?

Try using keyword arguments in the first argument:

cnxOMC = mysql.connector.connect(user=user, password=password,
host='localhost', database=database)

It might also help if you print the repr of the arguments; that way
you'll be able to see the difference between, say, 0 and "0".

On another point, using a 'bare' except ("except:") is virtually always
a *bad* idea. It'll catch _all_ exceptions, including NameError, which
could indicate a bug in your code. Catch only what you need to, only
what you're expecting and can handle.
 
C

Carlos Ortiz

(e-mail address removed) writes:






You pass the hardcoded parameters as keyword arguments, unlike in the

version that doesn't work. Maybe that is the difference. Try this:



cnxOMC = mysql.connector.connect(user=user,

password=password,

host='localhost',

database=database)



It only looks funny. In "user=user" the first "user" is a parameter

name, the other is the variable in your code.



Try help(mysql.connector.connect) at the interactive prompt, or

otherwise check the documentation.

Thanks a lot man, it worked flawlessly.
 
C

Carlos Ortiz

In the first example you're using positional arguments, whereas in the

second example you're using keyword arguments. Are you sure that the

positional arguments are in the correct order?



Try using keyword arguments in the first argument:



cnxOMC = mysql.connector.connect(user=user, password=password,

host='localhost', database=database)



It might also help if you print the repr of the arguments; that way

you'll be able to see the difference between, say, 0 and "0".



On another point, using a 'bare' except ("except:") is virtually always

a *bad* idea. It'll catch _all_ exceptions, including NameError, which

could indicate a bug in your code. Catch only what you need to, only

what you're expecting and can handle.

Thanks, I really don't know the difference between positional and keyboard arguments so I think I must do some python reading. I'm not really a pythonprogrammer (I'm a C, assembly, Java and C# programmer). Anyway the code isnow working.

Thanks a lot to both of you.
Regards.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top