sqlite数据库保存聊天记录

sqlite数据库保存聊天记录,第1张

概述版权所有,转载请注明出处:http://guangboo.org/2013/01/29/save-chat-history-with-sqlite 最近正在开发一个即时聊天系统----Pian Communicator,该项目是基于XMPP协议,使用Python+wxPython+xmpp.py开发,由于都是深夜奋战,项目进展也比较慢,目前正在开发聊天功能。聊天功能中还需要保护聊天记录保存,就像

版权所有,转载请注明出处:http://guangboo.org/2013/01/29/save-chat-history-with-sqlite

最近正在开发一个即时聊天系统----Pian Communicator,该项目是基于XMPP协议,使用Python+wxPython+xmpp.py开发,由于都是深夜奋战,项目进展也比较慢,目前正在开发聊天功能。聊天功能中还需要保护聊天记录的保存,就像QQ一样,我们可以浏览历史聊天记录。有些即时聊天系统的聊天记录也有采用xml文件记录的,但是xml文件毕竟没有数据库那么容易检索数据,因此我这里打算采用sqlite数据库来存储,并且Python本身就自带了对sqlite的支持。

首先先定义了记录和查询接口,比较目前确定是使用sqlite来记录,但以后也有可能使用xml或其它方式来记录的可能,因此这里定义一个抽象,以便以后进行扩展。接口主要的方法为read和write方法,分别用于读,写记录,记录的内容暂时只有talk(发言者),nickname(发言者的别名),typ(记录类别,包括msg,file等),msg(内容),tm(时间)。另外为了避免忘记调用close方法,特地定义了__del__方法,并且该方法会在类被垃圾回收时调用。代码如下:

class History(object):      def __init__(self,jID,to,path):            self.__jID = jID            self.__to = to            self.__path = path            self.__opened = False            if not os.path.exists(path):                  os.mkdir(path)      def get_jID(self): return self.__jID      def get_to(self): return self.__to      def get_path(self): return self.__path      def open(self): raise NotImplementedError()      def read(self): raise NotImplementedError()      def write(self,talk,nickname,typ,msg,tm): raise NotImplementedError()      def close(self): raise NotImplementedError()      def __del__(self):            if self.__opened:                  self.close()

采用sqlite数据库存储记录的具体类,如下实现:

class sqliteStorageHistory(History):      def __init__(self,path):            super(sqliteStorageHistory,self).__init__(jID,path)            _dir = os.path.join(path,self.get_jID())            if not os.path.exists(_dir):                  os.mkdir(_dir)                              self.dbname = os.path.join(path,self.get_jID(),'history.db')            self.__connection = None            self.__cursor = None      def open(self):            has = True            if not os.path.exists(self.dbname):                  has = False            self.__connection = sqlite3.connect(self.dbname)            self.__cursor = self.__connection.cursor()            if not has:                  self.__cursor.execute('''create table historIEs (_to text,talk text,nick text,typ text,msg text,datetime datetime)''')                  self.__connection.commit()            self.__opened = True                  def read(self):            if self.__cursor == None or self.__connection == None:                  raise HistoryError('Please Open connection.')                        for row in self.__cursor.execute("select * from historIEs where _to=?",(self.get_to(),)):                  yIEld row      def write(self,tm):            if self.__cursor == None or self.__connection == None:                  raise HistoryError('Please Open connection.')                        self.__cursor.execute('insert into historIEs (_to,nick,datetime) values (?,?,?)',\                                  (self.get_to(),str(tm),))            self.__connection.commit()      def close(self):            if not self.__opened:                  return                        if not self.__cursor:                  try:                        self.__cursor.close()                        del self.__cursor                  except: pass                              if not self.__connection:                  try:                        self.__connection.close()                        del self.__connection                  except: pass            self.__opened = False

测试代码为:

if __name__ == '__main__':      sql = sqliteStorageHistory('zhang@gmail.com','wang2xiao@gmail.com','c:\his2')      sql.open()      sql.write('wang2xiao@gmail.com','Wang,2Xiao','msg','Hi,What are you doing?',datetime.datetime.Now())      sql.write('zhang@gmail.com','Zhang,Guangbo',nothing.',datetime.datetime.Now())      for row in sql.read():            print row      sql.close()

输出结果:

(u'wang2xiao@gmail.com',u'wang2xiao@gmail.com',u'Wang,u'msg',u'Hi,What are youdoing?',u'2013-01-29 16:21:04.324000')(u'wang2xiao@gmail.com',u'zhang@gmail.com',u'Zhang,u'2013-01-29 16:21:04.417000')
总结

以上是内存溢出为你收集整理的sqlite数据库保存聊天记录全部内容,希望文章能够帮你解决sqlite数据库保存聊天记录所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/1174565.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-02
下一篇2022-06-02

发表评论

登录后才能评论

评论列表(0条)

    保存