Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #56748 > unrolled thread

Error connecting to MySQL from Python

Started bycarlos.ortiz.asm@gmail.com
First post2013-10-12 09:09 -0700
Last post2013-10-12 10:21 -0700
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Error connecting to MySQL from Python carlos.ortiz.asm@gmail.com - 2013-10-12 09:09 -0700
    Re: Error connecting to MySQL from Python Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-10-12 19:46 +0300
      Re: Error connecting to MySQL from Python Carlos Ortiz <carlos.ortiz.asm@gmail.com> - 2013-10-12 10:18 -0700
    Re: Error connecting to MySQL from Python MRAB <python@mrabarnett.plus.com> - 2013-10-12 18:13 +0100
      Re: Error connecting to MySQL from Python Carlos Ortiz <carlos.ortiz.asm@gmail.com> - 2013-10-12 10:21 -0700

#56748 — Error connecting to MySQL from Python

Fromcarlos.ortiz.asm@gmail.com
Date2013-10-12 09:09 -0700
SubjectError connecting to MySQL from Python
Message-ID<037a0c6b-1917-45dd-98cc-f1ad9aa5ef54@googlegroups.com>
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.

Thanks in advance.

[toc] | [next] | [standalone]


#56749

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2013-10-12 19:46 +0300
Message-ID<qoteh7qighy.fsf@ruuvi.it.helsinki.fi>
In reply to#56748
carlos.ortiz.asm@gmail.com writes:

> 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.

[toc] | [prev] | [next] | [standalone]


#56751

FromCarlos Ortiz <carlos.ortiz.asm@gmail.com>
Date2013-10-12 10:18 -0700
Message-ID<987b9b5e-b79a-437c-9c65-e2182a7550ff@googlegroups.com>
In reply to#56749
On Saturday, October 12, 2013 11:46:49 AM UTC-5, Jussi Piitulainen wrote:
> carlos.o...@gmail.com writes:
> 
> 
> 
> > 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.

Thanks a lot man, it worked flawlessly.

[toc] | [prev] | [next] | [standalone]


#56750

FromMRAB <python@mrabarnett.plus.com>
Date2013-10-12 18:13 +0100
Message-ID<mailman.1042.1381597981.18130.python-list@python.org>
In reply to#56748
On 12/10/2013 17:09, carlos.ortiz.asm@gmail.com wrote:
> 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.

[toc] | [prev] | [next] | [standalone]


#56753

FromCarlos Ortiz <carlos.ortiz.asm@gmail.com>
Date2013-10-12 10:21 -0700
Message-ID<0d042cc9-0e9d-4345-bb28-755882b9e580@googlegroups.com>
In reply to#56750
On Saturday, October 12, 2013 12:13:05 PM UTC-5, MRAB wrote:
> On 12/10/2013 17:09, carlos.o...@gmail.com wrote:
> 
> > 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.

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 python programmer (I'm a C, assembly, Java and C# programmer). Anyway the code is now working.

Thanks a lot to both of you.
Regards.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web