-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite_example.py
More file actions
35 lines (28 loc) · 770 Bytes
/
sqlite_example.py
File metadata and controls
35 lines (28 loc) · 770 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
import sqlite3
# connecting to SQLite database
conn = sqlite3.connect('library.db')
# creating a cursor object to execute SQL commands
cursor = conn.cursor()
# creating a table for storing books
cursor.execute('''
CREATE TABLE IF NOT EXISTS books (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
author TEXT NOT NULL,
publication_year INTEGER
)
''')
# inserting a new book into the table
cursor.execute('''
INSERT INTO books (title, author, publication_year)
VALUES (?, ?, ?)
''', ('To Kill a Mockingbird', 'Harper Lee', 1960))
# committing the transaction
conn.commit()
# quer and display all books
cursor.execute('SELECT * FROM books')
books = cursor.fetchall()
for book in books:
print(book)
# close the connection
conn.close()