简体   繁体   中英

Basic Database made with SQLITE3

I created a basic database in python with sqlite3 which takes in 3 values and stores them. Now where the problem lies is that when I created a function that is supposed to output the values, no syntax errors were displayed on terminal and none of my values were printed. Im guessing this is a minor error but I am not able to spot it.

The code is given below:

import sqlite3

def create_table():
    conn = sqlite3.connect("lite.db")    
    cur = conn.cursor()
    cur.execute("CREATE TABLE IF NOT EXISTS store (item TEXT, quantity 
    INTEGER, price FLOAT)")
    conn.commit()
    conn.close()

def insert(item, quantity, price):
    conn = sqlite3.connect("lite.db")
    cur = conn.cursor()
    cur.execute("INSERT INTO store VALUES ('?, ?, ?')", (item, quantity, 
    price))
    conn.commit()
    conn.close()

insert("Mug", 8, 6)

def view():
    conn = sqlite3.connect("lite.db")
    cur = conn.cursor()
    cur.execute("SELECT * FROM store")
    rows = cur.fetchall()
    conn.close()
    return rows

print(view())

Again no error messages were displayed but my values are not displayed.

I have tried it. it seems the single quotes are not needed in the insert . See modified below:

import sqlite3

def create_table():
    conn = sqlite3.connect("lite.db")    
    cur = conn.cursor()
    cur.execute("CREATE TABLE IF NOT EXISTS store (item TEXT, quantity INTEGER, price FLOAT)")
    conn.commit()
    conn.close()

def insert(item, quantity, price):
    conn = sqlite3.connect("lite.db")
    cur = conn.cursor()
    cur.execute("INSERT INTO store VALUES (?,?,?)", (item, quantity, price))
    conn.commit()
    conn.close()


def view():
    conn = sqlite3.connect("lite.db")
    cur = conn.cursor()
    cur.execute("SELECT * FROM store")
    rows = cur.fetchall()
    conn.close()
    return rows
create_table()
insert("Mug", 1, 5)
print(view())

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