Created
June 20, 2023 10:22
-
-
Save lyf-is-coding/5ed58554f1e9ed17fe64b61041c95c05 to your computer and use it in GitHub Desktop.
Replit python interact sqlite database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
connection = sqlite3.connect("user_database") | |
# Create a table for storing data | |
connection.execute( | |
"CREATE TABLE IF NOT EXISTS My_library (id INTEGER PRIMARY KEY, author STRING, book STRING);" | |
) | |
# Perform CRUD operations | |
# Create | |
connection.execute("INSERT INTO My_library (id,author,book) " | |
"VALUES (1, 'Steve Biko','I write what I like.')") | |
# Read | |
cursor_object = connection.execute("SELECT * FROM My_library") | |
print(cursor_object.fetchall()) | |
# Update | |
connection.execute( | |
"UPDATE My_library SET book = 'I WRITE WHAT I LIKE' WHERE id = 1") | |
# Delete | |
connection.execute("DELETE from My_library WHERE id = 1;") | |
# Commit changes | |
connection.commit() | |
# Close the connection | |
connection.close() | |
exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment