How to create and use sqlite in-memory databases with sqlite3

PHOTO EMBED

Tue Jun 18 2024 16:58:47 GMT+0000 (Coordinated Universal Time)

Saved by @pynerds #python

#import the sqlite3 module
import sqlite3

#create a tble
with sqlite3.connect(':memory:') as conn:
  #create a table
  conn.execute('''
    CREATE TABLE point(
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      x INTEGER,
      y INTEGER
      )
    ''')

  #populate table
  conn.executescript('''
    INSERT INTO point (x, y) VALUES (3, 4);
    INSERT INTO point (x, y) VALUES (2, 3);
    INSERT INTO point (x, y) VALUES (-2, 5);
    ''')

  #retrieve data
  cursor = conn.cursor()
  cursor.execute('SELECT x, y FROM point;')

  print(cursor.fetchall())
  cursor.close()
content_copyCOPY

https://www.pynerds.com/sqlite3-in-memory-databases-python/