· 6 years ago · Dec 17, 2019, 05:15 PM
1import sqlite3
2
3
4class DbProvider:
5 # Create table
6 def create_table(self, name):
7 sql = '''CREATE TABLE IF NOT EXISTS {}
8 (ID INTEGER PRIMARY KEY AUTOINCREMENT,
9 NAME VARCHAR(100)
10 )'''.format(name)
11 self.cursor.execute(sql)
12 self.connection.commit()
13
14 def insert_into_table(self, table_name, name):
15 sql = '''INSERT INTO {0} (NAME) VALUES ('{1}');'''.format(table_name, name)
16 self.cursor.execute(sql)
17 self.connection.commit()
18
19 def get_data_from_table(self, table_name):
20 sql = '''SELECT * FROM {}'''.format(table_name)
21 self.cursor.execute(sql)
22 rows = self.cursor.fetchall()
23 for row in rows:
24 print(row)
25
26 def __init__(self):
27 # Create database if not exist and get a connection to it
28 self.connection = sqlite3.connect('db.sqlite')
29 # Get a cursor to execute sql statements
30 self.cursor = self.connection.cursor()
31
32
33if __name__ == "__main__":
34 db = DbProvider()
35 # db.create_table('Face')
36 db.insert_into_table('Face', 'tempName')
37 db.get_data_from_table('Face')