-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary_app.py
More file actions
41 lines (33 loc) · 965 Bytes
/
Copy pathLibrary_app.py
File metadata and controls
41 lines (33 loc) · 965 Bytes
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
import sqlite3
# Connect to the database (or create it if it doesn't exist)
conn = sqlite3.connect("Library.db")
cursor = conn.cursor()
# Create the table for storing book information
cursor.execute("""
CREATE TABLE IF NOT EXISTS books (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT,
author TEXT,
price REAL,
stock INTEGER
)
""")
# Insert your books into the table
cursor.execute("DELETE FROM books")
cursor.executemany("""
INSERT INTO books (title, author, price, stock)
VALUES (?, ?, ?, ?)
""", [
("Negative Self-Talk", "Shad Helmstetter", 15.5, 10),
("How The Secret Changed My Life", "Rhonda Byrne", 12.0, 6),
("The Little Prince", "Antoine de Saint-Exupéry", 9.8, 23)
])
# Commit the changes
conn.commit()
# Code for reading and displaying data
cursor.execute("SELECT * FROM books")
rows = cursor.fetchall()
for row in rows:
print(row)
# close the connection
conn.close()