日期:2014-05-16  浏览次数:20548 次

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, jid, to, path):
            super(SqliteStorageHistory, self).__init__(jid, to, 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, talk, nickname, typ, msg, tm):
            if self.__cursor == None or self.__connection == None:
                  raise HistoryError('Please Open connection.')
            
            self.__cursor.execute('insert into histories (_to, talk, nick, typ, msg, datetime) values (?, ?, ?, ?, ?,
?)', \
                                  (self.get_to(), talk, nickname, typ, msg, 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()
      sq