将Gtk.Textbuffer存储在SQL数据库中.编码麻烦 [英] Store Gtk.Textbuffer in SQL database. Encoding troubles

查看:62
本文介绍了将Gtk.Textbuffer存储在SQL数据库中.编码麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 python2/Gtk3/Glade 开发一个笔记应用程序.

I'm working on a note taking app using python2/Gtk3/Glade.

这些注释存储在MySQL数据库中,并显示在 TextView小部件中.我可以加载/存储/显示纯文本格式.但是我希望能够将图像添加到笔记页面,并将其存储在Database.so中,因此必须对数据进行序列化,并且在弄清楚如何对序列化的序列进行编码/解码时遇到了一些麻烦数据进出数据库.我收到unicode起始字节错误.如果正在处理文件,我可以以二进制模式打开文件,但是我将其作为字符串存储在数据库中.我已经尝试使用 bytes() string.encode() encoding/decoding 作为UTF-8和ASCII [请参见下面的示例代码]和其他几种方法,但都无效.

The notes are stored in a MySQL Database and displayed in a TextView widget. I can load/store/display plain text fine. However I want the ability to add images to the note page, and store them in the Database.so the data has to be serialised and I'm having some trouble figuring out how to encode/decode the serialised data going in and out of the Database. I'm getting unicode start byte errors. If was working with files I could just open the file in binary mode, but I'm storing as a string in a Database. I've tried encoding/decoding as UTF-8 and ASCII using bytes() and string.encode()[see the sample code below] and a few other ways but none work.

我正在使用此功能将图像添加到textview缓冲区中:

I am using this function to add the image to the textview buffer:

def _AddImagetoNode(self,oWidget):
    filenm = None
    seliter = self.GetTreeSelection(self.treeview)
    filenm = self.FileOpenDiag("Select an Image To Insert.","Image","*.png,*.jpg,*.bmp")
    if filenm == None:
        return()

    #filenm =  "/home/drift/Pictures/a.png"
    buf = self.dataview.get_buffer()
    pixbuf = GdkPixbuf.Pixbuf.new_from_file(filenm)
    #pixbuf.scale_simple(dest_width, dest_height, gtk.gdk.INTERP_BILINEAR)
    buf.insert_pixbuf(buf.get_end_iter(), pixbuf) 
    self.dataview.set_buffer(buf)
    self.dataview.show()

这是存储textview缓冲区的函数:

This is the function that stores the textview buffer:

def SaveDataView(self):
    global DataViewNode
    global DataViewIsImage

    if len(self.GetProjectName()) == 0:
        return()
    buf = self.dataview.get_buffer()

    format = buf.register_serialize_tagset()
    data2 = buf.serialize(buf, format, buf.get_start_iter(), buf.get_end_iter())

    #convert bytes(data) to string
    data = data2.decode(encoding='UTF-8') #<< i think my problem is here
    print("save b4 decode >>>>>>:%s"%data2)

    sql = "UPDATE " + self.GetProjectName() + " SET tDataPath=%s  WHERE tNodeID=%s"
    val = (data, DataViewNode)

    self.cursor.execute(sql,val)
    self.mariadb_connection.commit()

这是加载缓冲区的函数:

This is the function that loads the Buffer:

def UpdateDataView(self, nodeid):
    global DataViewNode
    #global DataViewIsFile
    DataViewNode=nodeid


    if self.GetProjectName() != None and DataViewNode != None:
        self.dataview.set_sensitive(True)
    else:
        self.dataview.set_sensitive(False)
        self.dataview.show()
        return()

    buf = self.dataview.get_buffer()
    buf.set_text('')
    enc = self.DbGetNodeData(nodeid)



    #convert string(enc) to bytes
    data = enc.encode(encoding='UTF-8')#<<< i think my problem is here
    print("update after decode >>>>>>>>>: %s"%data)
    ########### load
    format = buf.register_deserialize_tagset()
    buf.deserialize(buf, format, buf.get_end_iter(),data) 


    #buf.set_text(enc)
    self.dataview.set_buffer(buf)
    self.dataview.show()

我正在使用mysql.connector连接到mariadb.这是sql连接字符串:

I'm using mysql.connector to connect to a mariadb. This is the sql connection string:

self.mariadb_connection = mariadb.connect(user='box', password='box', host='localhost', database='Boxer',charset='utf8')

这是我遇到的错误.

回溯(最近一次通话最后一次):文件"Boxer.py",第402行,在_TreeSelectionChangedself.SaveDataView()文件SaveBoxView中的"Boxer.py"文件,第334行data = data2.decode(encoding ='UTF-8')UnicodeDecodeError:'utf-8'编解码器无法解码位置174的字节0xb4:无效的起始字节回溯(最近一次通话):文件"Boxer.py",行398,在_DataViewLostFocusself.SaveDataView()文件SaveBoxView中的"Boxer.py"文件,第334行data = data2.decode(encoding ='UTF-8')UnicodeDecodeError:'utf-8'编解码器无法解码位置174的字节0xb4:无效的起始字节

Traceback (most recent call last): File "Boxer.py", line 402, in _TreeSelectionChanged self.SaveDataView() File "Boxer.py", line 334, in SaveDataView data = data2.decode(encoding='UTF-8') UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb4 in position 174: invalid start byte Traceback (most recent call last): File "Boxer.py", line 398, in _DataViewLostFocus self.SaveDataView() File "Boxer.py", line 334, in SaveDataView data = data2.decode(encoding='UTF-8') UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb4 in position 174: invalid start byte

使用此代码,我可以在文本视图中添加/编辑纯文本并成功保存/加载,但是一旦添加图像,我就可以编码错误.任何帮助将不胜感激.

With this code I can add/edit plain text in the text view and successfully save/load it but as soon as I add the image, I'm get the encoding errors. Any help would be appreciated.

推荐答案

了解其工作原理.感谢theGtknerd,您的答案才是关键.对于任何对此有麻烦的人,我最终将BLOB类型用于与im一起工作的列的MySQL字段类型.我尝试了BINARY [返回格式错误的序列化数据]和VARBINARY [甚至都不允许我创建表],所以我最终使用了LONGBLOB类型.这是任何需要它的人的工作代码.

Got it workings. thanks to theGtknerd your answer was the key. for anyone else having trouble with this i ended up using the BLOB type for the MySQL field type for the column im working with. I tried BINARY[it returnd malformed serialize data] AND VARBINARY [wouldnt even allow me to create the table] so i ended up using the LONGBLOB type. here is the working code for anyone that needs it.

def UpdateDataView(self, nodeid):
    global DataViewNode
    #global DataViewIsFile
    DataViewNode=nodeid


    if self.GetProjectName() != None and DataViewNode != None:
        self.dataview.set_sensitive(True)
    else:
        self.dataview.set_sensitive(False)
        self.dataview.show()
        return()

    buf = self.dataview.get_buffer()
    buf.set_text('')
    data = self.DbGetNodeData(nodeid)
    if data =='':
        return()


    format = buf.register_deserialize_tagset()
    buf.deserialize(buf, format, buf.get_end_iter(),data)       

    self.dataview.set_buffer(buf)
    self.dataview.show() 


def SaveDataView(self):
    global DataViewNode
    global DataViewIsImage

    if len(self.GetProjectName()) == 0:
        return()
    buf = self.dataview.get_buffer()
    enc = buf.get_text(buf.get_start_iter(),buf.get_end_iter(),False)
    self.AddData2Db(DataViewNode,enc)

    format = buf.register_serialize_tagset()
    data = buf.serialize(buf, format, buf.get_start_iter(), buf.get_end_iter())

    sql = "UPDATE " + self.GetProjectName() + " SET tDataPath=%s  WHERE tNodeID=%s"
    val = (data, DataViewNode)

    self.cursor.execute(sql,val)
    self.mariadb_connection.commit()

和即时通讯使用它来创建表

and im using this to create the table

sql = "CREATE TABLE %s (tParentNodeID TEXT,tNodeTxt TEXT,tNodeID TEXT,tDataPath LONGBLOB)" %pName
    self.cursor.execute(sql)
    self.mariadb_connection.commit()

这篇关于将Gtk.Textbuffer存储在SQL数据库中.编码麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆