-
Notifications
You must be signed in to change notification settings - Fork 0
/
crud_python_sqlite.py
93 lines (72 loc) · 2.31 KB
/
crud_python_sqlite.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
import os, sqlite3
#Bianca Rodrigues
#GitHub: https://github.com/bia-rodrig/
#Check if DB exists and create if doesn't
'''
check = os.path.exists('db_test.db')
if (not check):
file = open('db_test.db', 'w')
file.close()
print('created')
# connection to DB
connection = sqlite3.connect('db_test.db')
# SQLite commands cursor
cursor = connection.cursor()
#Create query to table
sql = 'CREATE TABLE IF NOT EXISTS Contacts (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME VARCHAR(100), AGE INT, PHONE VARCHAR(20))'
# Table Name: Contacts
# Colunms:
# ID: Integer
# Name: varchar(100)
# PHONE: VARCHAR(20)
'''
def create_db(db_file):
file = open(db_file, 'w')
file.close()
connection = sqlite3.connect(db_file)
cursor = connection.cursor()
sql = 'CREATE TABLE IF NOT EXISTS Contacts (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME VARCHAR(100), PHONE VARCHAR(20))'
cursor.execute(sql)
connection.close()
def check_if_exists(db_file, name):
connection = sqlite3.connect(db_file)
cursor = connection.cursor()
cursor.execute('SELECT * FROM Contacts WHERE NAME=?', (name,))
result = cursor.fetchall()
if (len(result) > 0):
return True
else: return False
def insert_contact(db_file, name, phone):
connection = sqlite3.connect(db_file)
cursor = connection.cursor()
cursor.execute('INSERT INTO Contacts (NAME, PHONE) VALUES (?, ?)', (name, phone))
connection.commit()
connection.close()
def update_contact(db_file, id_var, name, phone):
connection = sqlite3.connect(db_file)
cursor = connection.cursor()
cursor.execute('UPDATE Contacts set NAME = ?, PHONE = ? where ID=?', (name, phone, id_var))
connection.commit()
connection.close()
print('Contact updated\n')
def search_contact(db_file, name):
connection = sqlite3.connect(db_file)
cursor = connection.cursor()
sql = 'SELECT * FROM Contacts Where NAME LIKE \'%' + name +'%\''
cursor.execute(sql)
result = cursor.fetchall()
connection.close()
return result
def delete(db_file, contact_id):
connection = sqlite3.connect(db_file)
cursor = connection.cursor()
cursor.execute('DELETE FROM Contacts where ID=?', (contact_id,))
connection.commit()
connection.close()
def list_contacts(db_name):
connection = sqlite3.connect(db_name)
cursor = connection.cursor()
cursor.execute('SELECT * from Contacts')
contacts = cursor.fetchall()
connection.close()
return contacts