-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tidying up the demo projects and adding clarity
- Loading branch information
Showing
5 changed files
with
37 additions
and
63 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,26 @@ | ||
''' | ||
This code is a demo of how to create a database with a schema structure in Python using SQL Lite. | ||
For more information, see: https://datatofish.com/create-database-python-using-sqlite3/ | ||
''' | ||
import sqlite3 | ||
|
||
#we can name the file antyhing we want, but it is common to use .db | ||
db_file = "new.db" | ||
|
||
connection = sqlite3.connect(db_file) | ||
#the connection is the database file itself represented in python | ||
connection = sqlite3.connect(db_file) | ||
|
||
#the cursor is the object that will allow us to execute SQL commands | ||
cursor = connection.cursor() | ||
|
||
#execute runs the SQL command but does not save it, the results are just in memory for now | ||
cursor.execute(''' | ||
CREATE TABLE IF NOT EXISTS products | ||
([product_id] INTEGER PRIMARY KEY, [product_name] TEXT) | ||
''') | ||
|
||
cursor.execute(''' | ||
CREATE TABLE IF NOT EXISTS prices | ||
([product_id] INTEGER PRIMARY KEY, [price] INTEGER) | ||
''') | ||
#commit is when we actually save the changes we made to the database | ||
connection.commit() |
Empty file.
This file contains 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