Home > Enterprise >  how I can make in-memory database with sqlite3
how I can make in-memory database with sqlite3

Time:10-04

I wanna make an in-memory database but and I know the difference between the two databases is the in-memory database we shouldn't in the last line close that. I tried and I can't do that and I can't with searching in web find the answer.

this is the full code for database:

    def connect():
    conn = sqlite3.connect(":memory:")
    cur = conn.cursor()
    cur.execute(
       """CREATE TABLE itm (id INTEGER PRIMARY KEY , namee VARCHAR , numbers INTEGER , price INTEGER )""")
    conn.commit()


def insert(name, number, price):
    conn = sqlite3.connect(":memory:")
    cur = conn.cursor()
    cur.execute(
        """INSERT INTO itm VALUES(NULL , ?, ?,?)""",(name, number, price)
    )
    conn.commit()


def view():
    conn = sqlite3.connect(":memory:")
    cur = conn.cursor()
    cur.execute(
        "SELECT * FROM itm"
    )
    rows = cur.fetchall()
    return rows

def delete(id):
    conn = sqlite3.connect(":memory:")
    cur = conn.cursor()
    cur.execute("DELETE FROM itm WHERE id=?", (id,))
    conn.commit()




connect()

and in the last when I running the project python raise this error:

    Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Green\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "D:\python\WindowsProject\app\manager\manager_sign_in.py", line 44, in back_to_main_manager
    main_screen()
NameError: name 'main_screen' is not defined
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Green\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "D:\python\WindowsProject\app\manager\sign.py", line 33, in back_to_main_mngr
    main_screen()
NameError: name 'main_screen' is not defined
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Green\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "D:\python\WindowsProject\app\main.py", line 33, in user_sign
    user_screen()
NameError: name 'user_screen' is not defined
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Green\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "D:\python\WindowsProject\app\user\user_sign_in.py", line 188, in choose
    item_memory = app.user.memory.insert(name_var.get(), numbers.get(), price,)
  File "D:\python\WindowsProject\app\user\memory.py", line 15, in insert
    cur.execute(
sqlite3.OperationalError: no such table: itm
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Green\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "D:\python\WindowsProject\app\user\user_sign_in.py", line 188, in choose
    item_memory = app.user.memory.insert(name_var.get(), numbers.get(), price,)
  File "D:\python\WindowsProject\app\user\memory.py", line 15, in insert
    cur.execute(
sqlite3.OperationalError: no such table: itm

CodePudding user response:

You can't do sqlite3.connect(":memory:") in each function; they'll each get a new empty database.

Instead, you can do e.g. this to keep a single connection around in your process as a global variable.

You don't need to explicitly close it either; it'll be closed cleanly as your program is exiting.

conn = None


def connect():
    global conn
    if conn:
        raise RuntimeError("You can't call connect() twice")
    conn = sqlite3.connect(":memory:")
    cur = conn.cursor()
    cur.execute("""CREATE TABLE itm (id INTEGER PRIMARY KEY , namee VARCHAR , numbers INTEGER , price INTEGER )""")
    conn.commit()


def insert(name, number, price):
    cur = conn.cursor()
    cur.execute("""INSERT INTO itm VALUES(NULL , ?, ?,?)""", (name, number, price))
    conn.commit()


def view():
    cur = conn.cursor()
    cur.execute("SELECT * FROM itm")
    rows = cur.fetchall()
    return rows


def delete(id):
    cur = conn.cursor()
    cur.execute("DELETE FROM itm WHERE id=?", (id,))
    conn.commit()


connect()
  • Related