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
Hello,
I am a python beginner, covered all the basics. To really learn pythoon i though that it will be a good idea to just dive in and start working on some projects of my own. The first one is suppose to be simple...although i didnt find and tutorial:

I want to build a program for my company that organize all the orders we get from our clients: Model name, Quantity, Client, Date of order, Date of our Derlivery obloighation

All of the above should be entered by our workers. Then i will output a table with all orders.

I tried to do it with mysql, but i didnt fins any tutorial explaining how a user of the program will enter thr pramaters. Im quite sure that it is related to “input” function, but i didnt had any luck with it...If there is a tutorial that explains that i will be more than happy to read it, because im also a fan of RTFM approch.

Thanks
@larz60 thanks for your prompt answer, but i still stuck in the same problem - how a user can enter the data in the table, and not the programmer
I don't use MySQL, but all databases have an interactive query program.
In MySQL, it's simply called mysql
from a command line, type mysql
then you can enter SQL queries for creating tables, inserting data and whatever else you need, See: https://dev.mysql.com/doc/refman/8.0/en/...eries.html
If you don't know SQL, you need to take an SQL tutorial.
@Larz60+, I'm pretty sure OP wants to interact with MySQL using python :-)
@D_frucht: So basically you need a package to work with MySQL from python.
some resources:
https://dev.mysql.com/doc/connector-pyth...ction.html

or using SQLalchemy
https://www.sqlalchemy.org/


https://www.tutorialspoint.com/python/py...access.htm
Dear friends !

I see now a mistake in my question - iment sqlite not mysql
Well, more or less same advice:
https://docs.python.org/3/library/sqlite3.html
It's part of the standard library, so no need to install anything. Yet you still msy use SQLalchemy if you decide so
first:
import sqlite3
Then the remainder depends on exactly what you want to do.
Follow examples here: https://docs.python.org/2/library/sqlite3.html
this is my code, can someone explain what im doing wrong?

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 INTEGER, order_date TEXT, delivery_date TEXT )')

def data_entry():
    c.execute("INSERT INTO orders VALUES('client', 'model', 'quantity', 'order_date', 'delivery_date')")

    conn.commit()
    c.close()
    conn.close()



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()

for i in range(10):
    dynamic_data_entry()

c.close()
conn.close()
i have 2 problems with the code below:

although Client, Models, etc., defined as TEXT, while entering values in text, the program crash. but if i enter numbers, it continue.

the other problem is, after entering the data (using the input), i get this error message:

Output:
client: 56 model: 45 quantity: 345 order date: 345 Delivery date: 45 Traceback (most recent call last): File "/Users/davidfrucht/PycharmProjects/untitled1/DFSL.py", line 22, in <module> dynamic_data_entry() File "/Users/davidfrucht/PycharmProjects/untitled1/DFSL.py", line 18, in dynamic_data_entry (client, model, quantity, order_date, delivery_date)) sqlite3.OperationalError: no such table: orders Process finished with exit code 1
here is the code

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()

dynamic_data_entry()

conn.close()
Pages: 1 2 3