Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Database input by user
#11
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")
Reply
#12
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]
Reply
#13
For Python2.X use raw_input() instead of input(). Post the version that you are using when asking a question.
Reply
#14
In using python 3
Reply
#15
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
Reply
#16
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.
Reply
#17
Im sorry for the /python

Im using python 3
Reply
#18
I've edited my previous post, see if my proposed solution works.
Reply
#19
Unfortunately im not newr the pc, but im quite sure that one of the things i tried is to make it single quote.
Reply
#20
This error shows that you [mistakenly] run the file with python2. You did enter h as input, right?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 1,137 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  restrict user input to numerical values MCL169 2 983 Apr-08-2023, 05:40 PM
Last Post: MCL169
  user input values into list of lists tauros73 3 1,114 Dec-29-2022, 05:54 PM
Last Post: deanhystad
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,162 Dec-25-2022, 03:00 PM
Last Post: askfriends
Question Take user input and split files using 7z in python askfriends 2 1,162 Dec-11-2022, 07:39 PM
Last Post: snippsat
Sad how to validate user input from database johnconar 3 2,019 Sep-11-2022, 12:36 PM
Last Post: ndc85430
  How to split the input taken from user into a single character? mHosseinDS86 3 1,238 Aug-17-2022, 12:43 PM
Last Post: Pedroski55
  Use pexpect to send user input alisha17 0 1,953 May-10-2022, 02:44 AM
Last Post: alisha17
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,538 Apr-05-2022, 06:18 AM
Last Post: C0D3R
  Matplotlib - close multple plots with user input Positron79 0 1,791 Dec-01-2021, 05:26 PM
Last Post: Positron79

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020