简体   繁体   中英

how I can make in-memory database with sqlite3

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

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

Instead, you can do eg this to keep a single conn ection 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM