|
| 1 | +import sqlite3 |
| 2 | + |
| 3 | +def create_table(): |
| 4 | + conn =sqlite3.connect('Data.db') |
| 5 | + cursor = conn.cursor() #for interact with database |
| 6 | + |
| 7 | + cursor.execute(''' |
| 8 | + CREATE TABLE IF NOT EXISTS Data ( |
| 9 | + id INTEGER PRIMARY KEY, |
| 10 | + name TEXT, |
| 11 | + desc TEXT, |
| 12 | + status TEXT, |
| 13 | + email TEXT, |
| 14 | + gender TEXT, |
| 15 | + title TEXT, |
| 16 | + number INTEGER)''') #status via named type |
| 17 | + conn.commit() |
| 18 | + conn.close() |
| 19 | + |
| 20 | +def fetch_data(): |
| 21 | + conn = sqlite3.connect('Data.db') |
| 22 | + cursor = conn.cursor() |
| 23 | + cursor.execute('SELECT * FROM Data') |
| 24 | + data = cursor.fetchall() |
| 25 | + conn.close() |
| 26 | + return data |
| 27 | + |
| 28 | +def insert_data(id ,name, desc, status, email, gender, title, number): |
| 29 | + conn = sqlite3.connect('Data.db') |
| 30 | + cursor = conn.cursor() |
| 31 | + cursor.execute('INSERT INTO Data (id ,name, desc, status, email, gender, title, number) VALUES (?, ?, ?, ?, ?, ?, ?, ?)', |
| 32 | + (id ,name, desc, status, email, gender, title, number)) |
| 33 | + conn.commit() |
| 34 | + conn.close() |
| 35 | + |
| 36 | +def delete_data(id): |
| 37 | + conn = sqlite3.connect('Data.db') |
| 38 | + cursor = conn.cursor() |
| 39 | + cursor.execute('DELETE FROM Data WHERE id = ?', (id,)) |
| 40 | + conn.commit() |
| 41 | + conn.close() |
| 42 | + |
| 43 | +def update_data(new_name, new_desc, new_status, new_email, new_gender, new_title, new_number, id): |
| 44 | + conn = sqlite3.connect('Data.db') |
| 45 | + cursor = conn.cursor() |
| 46 | + cursor.execute("UPDATE Data SET name = ?, desc = ?, status = ?, email = ?, gender = ?, title = ?, number = ? WHERE id = ?", |
| 47 | + (new_name, new_desc, new_status, new_email, new_gender, new_title, new_number, id)) |
| 48 | + conn.commit() |
| 49 | + conn.close() |
| 50 | + |
| 51 | +def id_exists(id): |
| 52 | + conn = sqlite3.connect('Data.db') |
| 53 | + cursor = conn.cursor() |
| 54 | + cursor.execute('SELECT COUNT(*) FROM Data WHERE id = ?', (id,)) |
| 55 | + result = cursor.fetchone() |
| 56 | + conn.close() |
| 57 | + return result[0] > 0 #database ids matches or not matches |
| 58 | + |
| 59 | +create_table() |
0 commit comments