-
Notifications
You must be signed in to change notification settings - Fork 0
/
35db_read.py
38 lines (34 loc) · 1.06 KB
/
35db_read.py
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
32
33
34
35
36
37
38
#!/usr/bin/env python
import sqlite3
dbPath = "DBtesty/db01.sqlite"
tableName = "sqlite_sequence"
try:
con = sqlite3.connect(dbPath)
print("OK: connected to:", dbPath)
except sqlite3.Error as error:
print("Problem to connect to:", dbPath)
else:
try:
with con:
cur = con.cursor()
if (tableName == "sqlite_sequence"):
query = "SELECT * FROM {} ".format(tableName)
else:
query = "SELECT * FROM {} ORDER BY id DESC LIMIT 20".format(
tableName)
result = cur.execute(query).fetchall()
print("Content of table {}:".format(tableName))
for row in result:
print(row)
except sqlite3.Error as error:
print("Problem with simple query:", error)
else:
cur.close()
con.close()
print("DB closed")
try:
print(con.execute("PRAGMA database_list").fetchall())
except sqlite3.ProgrammingError as error:
print(error)
except NameError as error:
print("dbPath probably doesn't exists:", error)