diff --git a/libchat/create_db.py b/libchat/create_db.py new file mode 100755 index 0000000..d1eef34 --- /dev/null +++ b/libchat/create_db.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python2 +# -*- coding: UTF-8 -*- +# File: create_table.py +# Date: Wed Mar 25 16:43:22 2015 +0800 +# Author: Yuxin Wu + +import sys +import os + +from libchat import SqliteLibChat + +if len(sys.argv) != 2: + print "Usage: {} " + sys.exit() + +db_name = sys.argv[1] + +if os.path.exists(db_name): + delete = raw_input("DB exists. Delete ? (y/n)") + if delete == 'y': + os.unlink(db_name) + else: + sys.exit() + +db = SqliteLibChat(db_name) +db.create() diff --git a/libchat/libchat.py b/libchat/libchat.py new file mode 100644 index 0000000..5872ecd --- /dev/null +++ b/libchat/libchat.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python2 +# -*- coding: UTF-8 -*- +# File: libchat.py +# Date: Wed Mar 25 16:43:40 2015 +0800 +# Author: Yuxin Wu +import sqlite3 +import os + +class SqliteLibChat(object): + + def __init__(self, db_file): + self.db_file = db_file + self.conn = sqlite3.connect(db_file) + + def create(self): + c = self.conn.cursor() + c.execute(""" + CREATE TABLE message ( + source SMALLINT, + time INTEGER, + sender TEXT, + chatroom TEXT, + image COLLATE BINARY, + sound COLLATE BINARY, + extra_data COLLATE BINARY + ) + """) + self.conn.commit()