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 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()

Use
GetValue().decode('utf-8'))
or what ever character set you wish. Generally, when using a non-English language, you have to use .decode(). AFAIK-which isn't much when it comes to this-
# -*- coding: utf-8 -*-
means the program code can be written in utf-8 characters, so you can use Greek letters in variable names, etc. within the program itself.

If you get a

UnicodeDecodeError: 'utf8' codec can't decode byte X in position Y: invalid continuation byte

you are using the wrong encoding and Greek may be "iso-8859-7" but you will have to research that yourself.

Edited 4 Years Ago by woooee: n/a

First i check if i have wxpython unicode version
Second the code is here

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)

       
        self.word=[]
    def OnSaveAsFile(self, event):
        
        idis="000"
        newname=self.name.GetValue()

        p=newname.encode("utf-8")
        
        
        

        

            
            
            
       
        
            
            
        
                    
                    
            

            
                

            
                
        
        
            

        db=MySQLdb.connect(host="localhost",use_unicode="True",charset="utf8",user="root",passwd="root",db="test")
        cursor=db.cursor()

        sql="""INSERT INTO example(id,name) VALUES("%s","%s")"""%(idis,p)

        
        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()
This article has been dead for over six months. Start a new discussion instead.