1 | # sqlite db functionality |
||
2 | import sqlite3 as lite |
||
3 | |||
4 | class SqliteDB: |
||
5 | def __init__(self, bot=None): |
||
6 | self.bot = bot |
||
7 | |||
8 | self._prepare_database() |
||
9 | |||
10 | self.con.close() |
||
11 | |||
12 | |||
13 | def _prepare_database(self, create_string=None): |
||
14 | self._open() |
||
15 | if create_string: |
||
16 | self.cur.execute(create_string) |
||
17 | self.cur.execute('CREATE TABLE IF NOT EXISTS admins (id INTEGER PRIMARY KEY AUTOINCREMENT, username CHAR(32) DEFAULT NULL UNIQUE)') |
||
18 | self.cur.execute('CREATE TABLE IF NOT EXISTS img (id INTEGER PRIMARY KEY, url CHAR(128) DEFAULT NULL, user CHAR(32) DEFAULT NULL, time DATETIME DEFAULT CURRENT_TIMESTAMP, channel CHAR(32) DEFAULT NULL)') |
||
19 | self.cur.execute('CREATE TABLE IF NOT EXISTS lastfm (lastfm_username CHAR(64) NOT NULL, nick CHAR(32) NOT NULL)') |
||
20 | self.cur.execute('CREATE TABLE IF NOT EXISTS qdb (id INTEGER PRIMARY KEY, quote TEXT NOT NULL, date DATETIME DEFAULT CURRENT_TIMESTAMP)') |
||
21 | self.cur.execute('CREATE TABLE IF NOT EXISTS qdb_votes (vote_id PRIMARY KEY, quote_id INTEGER NOT NULL, votes INTEGER DEFAULT 0)') |
||
22 | |||
23 | |||
24 | def _open(self): |
||
25 | # perhaps TODO |
||
26 | self.con = lite.connect(self.bot.conf.getNick(self.bot.network) + ".db", check_same_thread=False) |
||
27 | self.cur = self.con.cursor() |
||
28 | |||
29 | View Code Duplication | def e(self, sql): |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
30 | try: |
||
31 | self._open() |
||
32 | self.cur.execute(sql) |
||
33 | if "INSERT" in sql or "REPLACE" in sql: |
||
34 | self.con.commit() |
||
35 | self.con.close() |
||
36 | elif "SELECT" in sql: |
||
37 | e = self.cur.fetchall() |
||
38 | self.con.close() |
||
39 | return e |
||
40 | except Exception, e: |
||
41 | print e |
||
42 | self.con.rollback() |
||
43 | self.con.close() |
||
44 | return None |
||
45 | |||
46 | def insertImg(self, user, url, channel): |
||
47 | self._open() |
||
48 | if user == "" or user == None: |
||
49 | user = "nobody" |
||
50 | |||
51 | self.cur.execute("INSERT INTO img (user, url, channel) VALUES (?, ?, ?)", (user, url, channel)) |
||
52 | self.con.commit() |
||
53 | |||
54 | self.con.close() |
||
55 | |||
56 | def getImgs(self): |
||
57 | self._open() |
||
58 | try: |
||
59 | self.cur.execute("SELECT * FROM img ORDER BY time DESC") |
||
60 | data = self.cur.fetchall() |
||
61 | self.con.close() |
||
62 | except: |
||
63 | self.con.close() |
||
64 | return None |
||
65 | |||
66 | return data |
||
67 | |||
68 | def isAdmin(self, username): |
||
69 | self._open() |
||
70 | try: |
||
71 | self.cur.execute("SELECT * FROM admins WHERE username = ?",(username,)) |
||
72 | data = self.cur.fetchall() |
||
73 | self.con.close() |
||
74 | except Exception,e: |
||
75 | self.con.close() |
||
76 | return None |
||
77 | |||
78 | return data |
||
79 |