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.
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to