Sqlite database saves chat records

Source: Internet
Author: User

Recently, I am developing an instant chat system-Pian Communicator. This project is based on the XMPP protocol and uses Python + wxPython + xmpp. the development of py is late at night, and the project progress is also slow. The chat function is currently being developed. The chat function also needs to protect the preservation of chat records. Just like QQ, we can view historical chat records. Some Chat records in instant messaging systems also use xml files, but xml files are not as easy to retrieve data as databases. Therefore, I plan to use the sqlite database for storage, in addition, Python itself comes with support for sqlite. First, the record and Query Interfaces are defined. Currently, sqlite is used to record records. However, xml or other methods may also be used to record records in the future. Therefore, an abstraction is defined here, for future extension. The main methods of the interface are read and write, which are used for reading and writing records. The recorded content is only talk (speaker), nickname (speaker's alias), and typ (record type, including msg, file, etc.), msg (content), tm (time ). In addition, to avoid forgetting to call the close method, the _ del _ method is defined and called when the class is garbage collected. The Code is as follows: [python] 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 (se Lf, talk, nickname, typ, msg, tm): raise NotImplementedError () def close (self): raise NotImplementedError () def _ del _ (self): if self. _ opened: self. close () uses a specific class for storing records in the sqlite database, as follows: [python] 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. mkd Ir (_ 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.w.cursor.exe cute (''' create table histories (_ to text, talk text, nick text, typ text, msg text, 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.w.cursor.exe cute ("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.w.cursor.exe cute ('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 should T: pass if not self. _ connection: try: self. _ connection. close () del self. _ connection Failed T: pass self. _ opened = False test code: [python] if _ name _ = '_ main _': SQL = SqliteStorageHistory ('zhang @ Gmail.com ', 'wang2xiao @ gmail.com', 'c: \ his2 ') SQL. open () www.2cto.com SQL. write ('wang2xiao @ gmail.com ', 'wang, 2xiao', 'msg', 'Hi, What are you doing? ', Datetime. datetime. now () SQL. write ('zhang @ gmail.com ', 'zhang, Guangbo', 'msg ', 'Hi, Nothing. ', datetime. datetime. now () for row in SQL. read (): print row SQL. close () output result: [python] (u'wang2xiao @ gmail.com ', u'wang2xiao @ gmail.com', u'wang, 2Xiao ', 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, Guangbo', u'msg ', u'hi, Nothing. ', U' 2013-01-29 16:21:04. 417000 ')

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.