Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sqlite3
|
2 |
+
|
3 |
+
# Connect to the database
|
4 |
+
conn = sqlite3.connect('example.db')
|
5 |
+
|
6 |
+
# Create a table
|
7 |
+
conn.execute('''CREATE TABLE users
|
8 |
+
(id INT PRIMARY KEY NOT NULL,
|
9 |
+
name TEXT NOT NULL,
|
10 |
+
email TEXT NOT NULL);''')
|
11 |
+
|
12 |
+
# Insert data into the table
|
13 |
+
conn.execute("INSERT INTO users (id, name, email) VALUES (1, 'John Doe', 'john.doe@example.com')")
|
14 |
+
conn.execute("INSERT INTO users (id, name, email) VALUES (2, 'Jane Doe', 'jane.doe@example.com')")
|
15 |
+
|
16 |
+
# Read data from the table
|
17 |
+
cursor = conn.execute("SELECT id, name, email FROM users")
|
18 |
+
for row in cursor:
|
19 |
+
print(f"ID = {row[0]}, NAME = {row[1]}, EMAIL = {row[2]}")
|
20 |
+
|
21 |
+
# Update data in the table
|
22 |
+
conn.execute("UPDATE users SET email = 'jane.doe@newexample.com' WHERE id = 2")
|
23 |
+
|
24 |
+
# Delete data from the table
|
25 |
+
conn.execute("DELETE FROM users WHERE id = 1")
|
26 |
+
|
27 |
+
# Commit the changes and close the connection
|
28 |
+
conn.commit()
|
29 |
+
conn.close()
|