希腊字符通过 TextCtrl 插入 mysqldb 错误
我在 MySQdb 中创建了一个数据库,其中包含一个名为 example 的表,在这个表中我想保存一个名称, 这个名字是希腊语。我的问题是,当我尝试立即保存名称而不使用 textctrl 时,它可以,但是当我使用 textctrl 时,我会出错。查看代码: 谁能帮帮我,我尝试过用 utf-8 编码,用 utf-8 解码,到 unicode 但什么也没有。
import os
import math
import random
import wx
import MySQLdb
import sys
APP_SIZE_X = 500
APP_SIZE_Y = 300
# -*- coding: utf-8 -*-
class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, size=(APP_SIZE_X, APP_SIZE_Y))
panel = wx.Panel(self, -1,style=wx.SUNKEN_BORDER)
wx.StaticText(panel, -1,'Name', (10, 55),style=wx.LEFT)
self.name = wx.TextCtrl(panel, -1, '', (110, 55), (120, -1))
save = wx.Button(panel, 1, 'Save', (70, 215),(130,-1))
save.Bind(wx.EVT_BUTTON,self.OnSaveAsFile)
quitbtn = wx.Button(panel, 1, 'Quit', (250, 215),(130,-1))
quitbtn.Bind(wx.EVT_BUTTON,self.OnQuit)
def OnSaveAsFile(self, event):
idis="000"
newname=self.name.GetValue()
db=MySQLdb.connect(host="localhost",use_unicode="True",charset="utf8",user="root",passwd="root",db="test")
cursor=db.cursor()
sql="""INSERT INTO TB_EXAMPLE(name) VALUES("%s","%s")"""%(idis,newname)
try:
cursor.execute(sql)
db.commit()
print 'save ok'
except:
print 'no save'
db.rollback()
def OnQuit(self,e):
self.Close()
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, -1, 'form1.py')
frame.Show(True)
self.SetTopWindow(frame)
return True
app = MyApp(0)
app.MainLoop()
I have create in MySQdb a DB with a table named example,in this table i want to save a name,
this name is in Greek language.My problem is thw follown when i try to save the name instantly without use textctrl, its ok but when i use textctrl i take error.Look the code:
Can anyone hel me please i have try encoding in utf-8,decoding in utf-8, to unicode but nothing.
import os
import math
import random
import wx
import MySQLdb
import sys
APP_SIZE_X = 500
APP_SIZE_Y = 300
# -*- coding: utf-8 -*-
class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, size=(APP_SIZE_X, APP_SIZE_Y))
panel = wx.Panel(self, -1,style=wx.SUNKEN_BORDER)
wx.StaticText(panel, -1,'Name', (10, 55),style=wx.LEFT)
self.name = wx.TextCtrl(panel, -1, '', (110, 55), (120, -1))
save = wx.Button(panel, 1, 'Save', (70, 215),(130,-1))
save.Bind(wx.EVT_BUTTON,self.OnSaveAsFile)
quitbtn = wx.Button(panel, 1, 'Quit', (250, 215),(130,-1))
quitbtn.Bind(wx.EVT_BUTTON,self.OnQuit)
def OnSaveAsFile(self, event):
idis="000"
newname=self.name.GetValue()
db=MySQLdb.connect(host="localhost",use_unicode="True",charset="utf8",user="root",passwd="root",db="test")
cursor=db.cursor()
sql="""INSERT INTO TB_EXAMPLE(name) VALUES("%s","%s")"""%(idis,newname)
try:
cursor.execute(sql)
db.commit()
print 'save ok'
except:
print 'no save'
db.rollback()
def OnQuit(self,e):
self.Close()
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, -1, 'form1.py')
frame.Show(True)
self.SetTopWindow(frame)
return True
app = MyApp(0)
app.MainLoop()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你使用的是 unicode 版本的 wxPython 吗?这可能会解决这个问题。或者您可以查看以下两个链接:
你也许可以通过做类似的事情来伪造它这:
Are you using the unicode version of wxPython? That might just fix this problem. Or you could take a look at the following two links:
You might be able to fake it just by doing something like this:
如果让 db 模块进行参数替换而不是让 Python 进行,会有什么区别吗? (无论如何,如果您插入用户输入的值,这会更安全。)例如:
从阅读 MySQLdb 文档来看,听起来当设置 charset 连接参数时,它也会自动转换为 unicode 或从 unicode 转换。
Does it make any difference if you let the db module do the parameter substitution instead of having Python do it? (This is much safer anyway if you are inserting user entered values.) For example:
From reading the MySQLdb docs it sounds like that will also auto-convert to/from unicode when the charset connect parameter is set.