2
|
1 import sqlite3
|
|
2
|
|
3 class SQLEx(object):
|
|
4 """
|
|
5 sqlite model
|
|
6 https://docs.python.org/2/library/sqlite3.html
|
|
7 """
|
|
8
|
|
9 def __init__(self, db):
|
|
10 self.db = db
|
|
11 self.conn = sqlite3.connect(self.db)
|
|
12
|
|
13 def __call__(self, sql, *args):
|
|
14 c = conn.cursor()
|
|
15 c.execute(sql, args)
|
|
16 c.commit()
|
|
17
|
|
18 def __del__(self):
|
|
19 self.conn.close()
|
|
20
|
|
21 def tables(self):
|
|
22 """
|
|
23 returns table names in database
|
|
24 """
|
|
25 # http://stackoverflow.com/questions/82875/how-to-list-the-tables-in-an-sqlite-database-file-that-was-opened-with-attach
|
|
26
|
|
27 sql = "SELECT name FROM dbname.sqlite_master WHERE type='table';"
|
|
28 tables = self(sql)
|
|
29 return tables
|
|
30
|