-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlite3_example.py
102 lines (87 loc) · 2.46 KB
/
sqlite3_example.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import sqlite3
database = "django-db.sqlite"
def createTable():
conn = sqlite3.connect(database)
# create table operation
try:
with conn:
drop_old_table_query = "DROP TABLE IF EXISTS users;"
conn.execute(drop_old_table_query)
create_query = """
CREATE TABLE IF NOT EXISTS users(
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT,
password TEXT,
is_admin INTEGER
);
"""
conn.execute(create_query)
conn.commit()
print("Table created successfully!")
except Exception as e:
print(str(e))
finally:
conn.close()
def insertData():
conn = sqlite3.connect(database)
# insert operation
try:
with conn:
insert_query = "INSERT INTO users (username, password, is_admin) VALUES (?, ?, ?)"
values = ("admin", "admin123", 1)
conn.execute(insert_query, values)
conn.commit()
print("Data inserted successfully!")
except Exception as e:
print(str(e))
finally:
conn.close()
def readData():
conn = sqlite3.connect(database)
# select operation
try:
with conn:
select_query = "SELECT * FROM users"
curs = conn.execute(select_query)
result = curs.fetchall()
for row in result:
print(row[1]) # username
print(row[2]) # password
except Exception as e:
print(str(e))
finally:
conn.close()
def updateData():
conn = sqlite3.connect(database)
# update operation
try:
with conn:
update_query = "UPDATE users SET password = ? WHERE username = ?"
values = ("newpassword", "admin")
conn.execute(update_query, values)
conn.commit()
print("Data updated successfully!")
except Exception as e:
print(str(e))
finally:
conn.close()
def deleteData():
conn = sqlite3.connect(database)
# delete operation
try:
with conn:
delete_query = "DELETE FROM users WHERE username = ?"
values = ("admin",)
conn.execute(delete_query, values)
conn.commit()
print("Data deleted successfully!")
except Exception as e:
print(str(e))
finally:
conn.close()
if __name__ == "__main__":
createTable()
insertData()
readData()
updateData()
deleteData()