From 1d1b9c49ffc7778516998742d2500f03fcd6c95c Mon Sep 17 00:00:00 2001 From: Gabor Szabo Date: Thu, 15 Aug 2024 13:15:55 +0300 Subject: [PATCH] add --- python/examples/sqlite/in_memory.py | 8 +++++ python/examples/sqlite/sql_create.py | 45 +++++++++++++++++----------- python/sqlite.md | 16 +++++++++- 3 files changed, 50 insertions(+), 19 deletions(-) create mode 100644 python/examples/sqlite/in_memory.py diff --git a/python/examples/sqlite/in_memory.py b/python/examples/sqlite/in_memory.py new file mode 100644 index 000000000..95da4e393 --- /dev/null +++ b/python/examples/sqlite/in_memory.py @@ -0,0 +1,8 @@ +import sqlite3 + +conn = sqlite3.connect(":memory:") +crs = conn.cursor() + +# use the database here + +conn.close() diff --git a/python/examples/sqlite/sql_create.py b/python/examples/sqlite/sql_create.py index 5138fef4f..ce2ca2374 100644 --- a/python/examples/sqlite/sql_create.py +++ b/python/examples/sqlite/sql_create.py @@ -1,25 +1,34 @@ import sqlite3 -conn = sqlite3.connect("companies.db") -crs = conn.cursor() -sql = ''' - CREATE TABLE companies ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - name VARCRCHAR(100) UNIQUE NOT NULL, - established INTEGER NOT NULL, - employees INTEGER DEFAULT 0 -) -''' +def create_table(conn): + crs = conn.cursor() -try: - crs.execute(sql) -except sqlite3.OperationalError as err: - print(f'sqlite error: {err.args[0]}') # table companies already exists - print(f'remove companies.db to show how it works') + sql = ''' + CREATE TABLE companies ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name VARCRCHAR(100) UNIQUE NOT NULL, + established INTEGER NOT NULL, + employees INTEGER DEFAULT 0 + ) + ''' -conn.commit() + try: + crs.execute(sql) + except sqlite3.OperationalError as err: + print(f'sqlite error: {err.args[0]}') # table companies already exists + print(f'remove companies.db to show how it works') -conn.close() + conn.commit() -print('done') +def main(): + conn = sqlite3.connect("companies.db") + + create_table(conn) + + conn.close() + + print('done') + +if __name__ == "__main__": + main() diff --git a/python/sqlite.md b/python/sqlite.md index 29282df28..c1a840268 100644 --- a/python/sqlite.md +++ b/python/sqlite.md @@ -5,7 +5,8 @@ {id: sqlite3} {i: sqlite} -* [sqlite3](http://docs.python.org/library/sqlite3.html) +* [SQLite](https://sqlite.org/) the most popular embedded relational database. +* [sqlite3](http://docs.python.org/library/sqlite3.html) - Python library to use SQLite. ## Connecting to SQLite database @@ -13,8 +14,21 @@ {i: connect|sqlite} {i: cursor|sqlite} +This connects to the database in the given file. If the file does not exist yet this will create the file and prepare it to hold +an SQLite database. No tables are created at this point and no data is inserted. + ![](examples/sqlite/sql_connect.py) +## Connecting to in-memory SQLite database +{id: sqlite-in-memory-connect} + +We can also create in-memory database. + +This is not persistent, but it can be useful to load some data into memory and then do fast SQL queries on +that database. + +![](examples/sqlite/in_memory.py) + ## Create TABLE in SQLite {id: sqlite-create}