-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.py
67 lines (57 loc) · 1.89 KB
/
logic.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from pathlib import Path
from tkinter import filedialog
import sqlite3
import ui
def selectFile(root):
prevFilePath = getPrevFilePath()
filePath = filedialog.askopenfilename(initialdir=prevFilePath, title="Select a Database",filetypes=(("Database Files","*.sqlite3"),("All Files","*.*")))
if (filePath == () or filePath == ""):
return
saveFilePath(filePath)
openDB(root, filePath)
def saveFilePath(filePath):
with open("filePath.txt", "w") as f:
filePath = str(Path(filePath).parent)
f.write(filePath)
def getPrevFilePath():
try:
with open("filePath.txt", "r") as f:
filePath = f.read()
return filePath
except:
return ""
def openDB(root, filePath):
# connect to database
conn = sqlite3.connect(filePath)
if (conn == None):
print("Error: Could not connect to database")
return
cursor = conn.cursor()
# get table names
tables = getTableNames(cursor)
#get num of rows in each table
numOfRows = getNumOfRowsInAllTables(cursor, tables)
# show tables
ui.showTablesFrame(root, tables, numOfRows, cursor)
def getTableNames(cursor):
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = cursor.fetchall()
return tables
def getNumOfRowsInAllTables(cursor, tables):
numOfRows = []
for table in tables:
tableName = table[0]
cursor.execute("SELECT COUNT(*) FROM {0};".format(tableName))
numOfRows.append(cursor.fetchall()[0][0])
return numOfRows
def getColumnNames(tableName, cursor):
cursor.execute("PRAGMA table_info(" + tableName + ")")
columns = cursor.fetchall()
columnNames = []
for column in columns:
columnNames.append(column[1])
return columnNames
def getTableData(tableName, cursor):
cursor.execute("SELECT * FROM " + tableName)
data = cursor.fetchall()
return data