Python Forum

Full Version: Database input by user
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
You forgot to call the create_table function:
import sqlite3
 
conn = sqlite3.connect('dfsl.db')
c = conn.cursor()
 
def create_table():
    c.execute("CREATE TABLE orders (Client TEXT, Model TEXT, Quantity INTEGER, Order_date TEXT, Delivery_date TEXT)")
 
def dynamic_data_entry():
    client = input("client: ")
    model = input("model: ")
    quantity = input("quantity: ")
    order_date = input("order date: ")
    delivery_date = input("Delivery date: ")
 
    c.execute("INSERT INTO orders (Client, Model, Quantity, Order_date, Delivery_date) VALUES (?, ?, ?, ?, ?)",
              (client, model, quantity, order_date, delivery_date))
 
    conn.commit()

# Create the table first
create_table()
# Input the data later
dynamic_data_entry()
 
conn.close()
Also remember that CREATE TABLE will fail if the table exists already in the DB (you can use "CREATE TABLE IF NOT EXISTS")
GREAT thanks !

it created the table, but i still have the problem that when i'm trying to input regullar letters, it comes up with error:
[python]
client: AS
Traceback (most recent call last):
File "/Users/davidfrucht/PycharmProjects/untitled1/DFSL.py", line 24, in <module>
dynamic_data_entry()
File "/Users/davidfrucht/PycharmProjects/untitled1/DFSL.py", line 11, in dynamic_data_entry
client = input("client: ")
File "<string>", line 1, in <module>
NameError: name 'AS' is not defined
[python]
For Python2.X use raw_input() instead of input(). Post the version that you are using when asking a question.
In using python 3
although Client, Models, etc., defined as TEXT, while entering values in text, the program crash (see below the code). but if i enter numbers, it continue.

im using python 3

import sqlite3

conn = sqlite3.connect('dfsl.db')
c = conn.cursor()


def create_table():
    c.execute("CREATE TABLE IF NOT EXISTS orders (Client TEXT, Model TEXT, Quantity TEXT, Order_date TEXT, Delivery_date TEXT)")
    conn.commit()


def dynamic_data_entry():
    client = input("client: ")
    model = input("model: ")
    quantity = input("quantity: ")
    order_date = input("order date: ")
    delivery_date = input("Delivery date: ")

    c.execute("INSERT INTO orders (Client, Model, Quantity, Order_date, Delivery_date) VALUES (?, ?, ?, ?, ?)",
              (client, model, quantity, order_date, delivery_date))

    conn.commit()


create_table()

dynamic_data_entry()

conn.close()
error message:


Error:
Traceback (most recent call last): File "/Users/davidfrucht/PycharmProjects/untitled1/DFSL.py", line 27, in <module> dynamic_data_entry() File "/Users/davidfrucht/PycharmProjects/untitled1/DFSL.py", line 13, in dynamic_data_entry client = input("client: ") File "<string>", line 1, in <module> NameError: name 'h' is not defined
You need to use [/python] as closing tag, put error message in error tags. You have been warned before.

Are you using Python 2 or 3?
p.s.:
Ah I see the edit now.

You'll probably have to put the text you enter in single quotes. Or you can modifiy the code to place the quotes around the text automatically, if you do not want to do it at input.
Im sorry for the /python

Im using python 3
I've edited my previous post, see if my proposed solution works.
Unfortunately im not newr the pc, but im quite sure that one of the things i tried is to make it single quote.
This error shows that you [mistakenly] run the file with python2. You did enter h as input, right?
Pages: 1 2 3