-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlite_demo.py
78 lines (59 loc) · 1.8 KB
/
sqlite_demo.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
68
69
70
71
72
73
74
75
76
77
78
# DELETE data
import sqlite3
from sqlite3 import Error
import pandas as pd
import streamlit as st
def create_connection(db_file):
conn = None
try:
conn = sqlite3.connect(db_file)
except Error as e:
print(e)
return conn
def select_table(conn, tbname):
sql = 'select * from ' + tbname
# print(sql)
cur = conn.cursor()
cur.execute(sql)
rows = cur.fetchall()
names = [description[0] for description in cur.description]
df = pd.DataFrame(rows)
df.columns = names
return df
def searchByTitle(conn, title):
sql = "select id, title, eventtype from papers where title like '%" + title+"%'"
print(sql)
cur = conn.cursor()
cur.execute(sql)
rows = cur.fetchall()
names = [description[0] for description in cur.description]
df = pd.DataFrame(rows)
df.columns = names
return df
def main():
database = r"database.sqlite"
with st.container():
col1, col2, col3 = st.columns(3)
with col1:
table_opts = ['Authors', 'Paperauthors', 'Papers']
# tbname = st.text_input('Search by Table name', 'authors')
tbname = st.selectbox('Select a Table:', table_opts, 0)
# create a database connection
conn = create_connection(database)
with conn:
df = select_table(conn, tbname)
with col2:
st.write("總數:" + str(df.shape[0]))
st.write(df)
with st.container():
st.write("---")
col1, col2, col3 = st.columns(3)
with col1:
title = st.text_input('Search by Title', 'network')
df = searchByTitle(conn, title)
st.write(df)
with col2:
st.write("Total:" + str(df.shape[0]))
conn.close()
if __name__ == '__main__':
main()