-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path21db_create_simple_table2.py
More file actions
31 lines (28 loc) · 1.06 KB
/
21db_create_simple_table2.py
File metadata and controls
31 lines (28 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python
import sqlite3
dbPath = "DBtesty/db01.sqlite"
tableNameList = ["t11"] # , "t12", "t13"
try:
con = sqlite3.connect(dbPath)
print("OK: connected to", dbPath)
except sqlite3.Error as error:
print("Problem to connect to {}: {}".format(dbPath, error))
else:
try:
with con:
cur = con.cursor()
print("OK: cursor created")
query = "SELECT name FROM sqlite_master WHERE type='table';"
allTablesList = [item[0] for item in cur.execute(query).fetchall()]
for tableName in tableNameList:
if tableName not in allTablesList:
query = "CREATE TABLE {}(id, col01, col02, col03, col05)".format(tableName)
cur.execute(query)
print("Table created:", tableName)
else:
print("Table {} already exists".format(tableName))
except sqlite3.Error as error:
print("Problem to create table:", error)
cur.close()
con.close()
print("OK: cursor and connection closed")