Problems with MySQLdb python

76 views Asked by At

When I try connect to mysql with user "root", my application returns "Access denied for user *"ODBC"@"localhost" (using password:NO)"*. I dnt know why this appear.

Im using Tkinter like GUI, I tested only Connection's script, its Okay.

Code Connection

import MySQLdb

class Conexao():
    def __init__(self, host,user,passwd,db):
        self.host = host
        self.user = user
        self.passwd = passwd
        self.db = db

    def getconnection(self):
        try:
            self.conn = MySQLdb.connect(host=self.host,
                                        user = self.user,
                                        passwd = self.passwd,
                                        db = self.db)
            print "Connected"
            return self.conn.cursor()
        except MySQLdb.Error,e:
            print " error "+ str(e[1])
            return e

Code App

from Tkinter import *

import sys
sys.path.insert(0,'C:\\Users\\felipe.cunha\\Documents\\project')

from conexao.conexao import Conexao


"""
b.execute("select * from setor")
>>> b.fetchall()
"""
class View(Frame):
        def __init__(self,master = None):
                Frame.__init__(self)
                self.master.title("teste")
                self.create_menu()
        def create_menu(self):#,width = self.width,height = self.height):
                host = "localhost"
                label_menu = Label(self.master,text = 'Login')
                label_menu.pack()

                entrada_menu_login = Entry(self.master)# user
                entrada_menu_senha = Entry(self.master, show = "*")# passwd                

                conn = Conexao(host,entrada_menu_login.get(),entrada_menu_senha.get(),"dsti")
                #conn = Conexao(host,"root","d04m10","dsti")


                btn_login = Button(self.master,text="Logar",command = conn.getConnection)#self.show_entry_fields)

                entrada_menu_login.pack()
                entrada_menu_senha.pack()
                btn_login.pack()


        def show_entry_fields(self):
                print(self.entrada_menu_login.get(), self.entrada_menu_senha.get())





app = View()
app.mainloop()
2

There are 2 answers

0
Anand S Kumar On BEST ANSWER

Like Bryan said, that may be the issue with your code, instead of making the button call conn.getConnection() directly, try an approach, where you save the entry variables into 'self' and then when the button is invoked, you create the connection at that time.

The code would look something like -

class View(Frame):
        def __init__(self,master = None):
                Frame.__init__(self)
                self.master.title("teste")
                self.create_menu()
        def create_menu(self):#,width = self.width,height = self.height):
                host = "localhost"
                label_menu = Label(self.master,text = 'Login')
                label_menu.pack()

                self.entrada_menu_login = Entry(self.master)# user
                self.entrada_menu_senha = Entry(self.master, show = "*")# passwd


                btn_login = Button(self.master,text="Logar",command = self.buttonListener) #self.show_entry_fields

                entrada_menu_login.pack()
                entrada_menu_senha.pack()
                btn_login.pack()

        def buttonListener(self):
            conn = Conexao(host,self.entrada_menu_login.get(),self.entrada_menu_senha.get(),"dsti")                                
            #conn = Conexao(host,"root","d04m10","dsti")
            dbcursor = conn.getConnection()
            #Do the rest of the logic you want to do.
1
Bryan Oakley On

The problem is that you are creating the connection when your GUI starts up, before the user has the ability to type in a username or password. Both entrada_menu_login and entrada_menu_senha are blank at the time you create the connection.

The solution is to not create the connection until after the user has clicked the button.