MySQLdb 的问题 python
Problems with MySQLdb python
当我尝试使用用户 "root"
连接到 mysql
时,我的应用程序 returns "Access denied for user *"ODBC"@"localhost" (using password:NO)"*
。我不知道为什么会出现这个。
我像 GUI 一样使用 Tkinter,我只测试了 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
代码应用程序
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()
问题是您在 GUI 启动时创建了连接,用户还没有能力输入用户名或密码。 entrada_menu_login
和 entrada_menu_senha
在您创建连接时都是空白的。
解决方案是在用户单击按钮之前不创建连接。
就像 Bryan 所说,这可能是您的代码的问题,而不是直接调用按钮 conn.getConnection(),尝试一种方法,将入口变量保存到 'self' 中,然后然后当按钮被调用时,你就在那个时候创建了连接。
代码看起来像 -
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.
当我尝试使用用户 "root"
连接到 mysql
时,我的应用程序 returns "Access denied for user *"ODBC"@"localhost" (using password:NO)"*
。我不知道为什么会出现这个。
我像 GUI 一样使用 Tkinter,我只测试了 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
代码应用程序
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()
问题是您在 GUI 启动时创建了连接,用户还没有能力输入用户名或密码。 entrada_menu_login
和 entrada_menu_senha
在您创建连接时都是空白的。
解决方案是在用户单击按钮之前不创建连接。
就像 Bryan 所说,这可能是您的代码的问题,而不是直接调用按钮 conn.getConnection(),尝试一种方法,将入口变量保存到 'self' 中,然后然后当按钮被调用时,你就在那个时候创建了连接。
代码看起来像 -
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.