sqlite数据库保存聊天记录

前端之家收集整理的这篇文章主要介绍了sqlite数据库保存聊天记录前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

版权所有,转载请注明出处: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 you
doing?',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')
原文链接:https://www.f2er.com/sqlite/201506.html

猜你在找的Sqlite相关文章