【发布时间】:2015-09-02 20:49:09
【问题描述】:
当我尝试使用用户 "root" 连接到 mysql 时,我的应用程序返回 "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()
【问题讨论】:
-
您的 mysql 数据库是否在标准端口 - 3306 上运行?仅在将 tkinter 前端与连接脚本一起使用时才会出现此问题吗?
-
是的,3306。当我使用 tkinter 前端时,总是出现这个错误。我无法使用 root 访问我的数据库,但我尝试执行 Connection 的类,它的工作原理。
-
您可以在创建
Conexao对象之前尝试打印-print(entrada_menu_senha.get())并更新我们的结果吗? -
是的,我创建了 def show_entry_field 并“打印”了 entrada_menu_login 和 entrada_menu_senha,它们返回相同的字符串。
-
与正确的字符串相同?当您将代码复制到 SO -
command = conn.getConnection时,这是一个错字吗?
标签: python python-2.7 tkinter mysql-python