python错误,写了个小程序,if语句总是不执行,求高手指点 10
importwximportMySQLdbclassadddialog(wx.Frame):def__init__(self):self.frame=wx.Frame._...
import wx
import MySQLdb
class adddialog(wx.Frame):
def __init__(self):
self.frame=wx.Frame.__init__(self,None,-1,'添加',size=(300,300))
panel=wx.Panel(self,-1)
label4=wx.StaticText(panel,-1,'用户名',pos=(10,10))
self.addname=wx.TextCtrl(panel,-1,pos=(100,10),size=(160,-1))
label5=wx.StaticText(panel,-1,'密码',pos=(10,40))
self.addpassword=wx.TextCtrl(panel,-1,'1234',pos=(100,40),size=(160,-1),style=wx.TE_PASSWORD)
self.button3=wx.Button(panel,-1,'添加',pos=(50,200))
self.button4=wx.Button(panel,-1,'取消',pos=(150,200))
self.Bind(wx.EVT_BUTTON, self.OnButton3, self.button3)
self.Bind(wx.EVT_BUTTON, self.OnButton4, self.button4)
def OnButton3(self,event):
n=self.addname.GetValue()
p=self.addpassword.GetValue()
db=MySQLdb.connect(host='localhost',user='root',
passwd='jinlong',db='python')
cur=db.cursor()
cur.execute("insert into user (usernumber,password) values('%s','%s')"%(n,p))
db.commit()
r=cur.execute('select usernumber from user')
r=cur.fetchall()
if n in r:
wx.MessageBox('用户已存在!', '提示信息', wx.OK|wx.ICON_INFORMATION)
else:
dlgOk=wx.MessageBox('添加成功!', '提示信息', wx.OK|wx.ICON_INFORMATION)
cur.close()
db.close()
self.Close()
def OnButton4(self,event):
self.Close()
if __name__ == '__main__':
app = wx.PySimpleApp()
frame=adddialog()
frame.Show(True)
app.MainLoop() 展开
import MySQLdb
class adddialog(wx.Frame):
def __init__(self):
self.frame=wx.Frame.__init__(self,None,-1,'添加',size=(300,300))
panel=wx.Panel(self,-1)
label4=wx.StaticText(panel,-1,'用户名',pos=(10,10))
self.addname=wx.TextCtrl(panel,-1,pos=(100,10),size=(160,-1))
label5=wx.StaticText(panel,-1,'密码',pos=(10,40))
self.addpassword=wx.TextCtrl(panel,-1,'1234',pos=(100,40),size=(160,-1),style=wx.TE_PASSWORD)
self.button3=wx.Button(panel,-1,'添加',pos=(50,200))
self.button4=wx.Button(panel,-1,'取消',pos=(150,200))
self.Bind(wx.EVT_BUTTON, self.OnButton3, self.button3)
self.Bind(wx.EVT_BUTTON, self.OnButton4, self.button4)
def OnButton3(self,event):
n=self.addname.GetValue()
p=self.addpassword.GetValue()
db=MySQLdb.connect(host='localhost',user='root',
passwd='jinlong',db='python')
cur=db.cursor()
cur.execute("insert into user (usernumber,password) values('%s','%s')"%(n,p))
db.commit()
r=cur.execute('select usernumber from user')
r=cur.fetchall()
if n in r:
wx.MessageBox('用户已存在!', '提示信息', wx.OK|wx.ICON_INFORMATION)
else:
dlgOk=wx.MessageBox('添加成功!', '提示信息', wx.OK|wx.ICON_INFORMATION)
cur.close()
db.close()
self.Close()
def OnButton4(self,event):
self.Close()
if __name__ == '__main__':
app = wx.PySimpleApp()
frame=adddialog()
frame.Show(True)
app.MainLoop() 展开
2个回答
展开全部
n=self.addname.GetValue()返回的是unicode
r=cur.fetchall()返回的是包含str(不是unicode)的list
转换下编码哈。
r=cur.fetchall()返回的是包含str(不是unicode)的list
转换下编码哈。
追问
我转换了一下,还是不对,您能指点一下吗?直接给些个转换代码更好,我刚接触,什么都不懂
r=cur.execute('select usernumber from user')
r=cur.fetchall()
ns=[]
for rec in r:
ns.append(rec)
if n in ns:
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询