0

Here is my label and entry box in Tkinter (only for customer's name, it looks similar for other inputs). My goal is to type some word in this entry box and then insert it into the database, by pressing button "Save".

conn = sqlite3.connect('my_database.db')
cur = conn.cursor()
CustomerName = StringVar()

lblName = Label(bottomLeftTopL, font = ('arial', 16, 'bold'), text = "Name", fg 
= 'black', width = 15, bd = 10, anchor = 'w')
lblName.grid(row = 0, column = 0)

txtName = Entry(bottomLeftTopL, font = ('arial', 16, 'bold'), bd = 2, width = 
24, bg = 'white', justify = 'left', textvariable = CustomerName)
txtName.grid(row = 0, column = 1)

My button which I want to use to save inputs into the database.

btnSave = Button(bottomLeftBottomL, pady = 8, bd = 2, 
fg = 'black', font = ('arial', 10, 'bold'), width = 10, text = "Save",
bg = 'white').grid(row = 7, column = 1)

Here's my class for customer's table in SQLAlchemy.

class Customers(Base):
    __tablename__ = "customers"

    id_customer = Column(Integer, primary_key = True)
    name = Column(String)
    phone_number = Column(String)
    adress = Column(String)

    def __init__(self, name, phone_number, adress):
       self.name = name
       self.phone_number = phone_number
       self.adress = adress

I guess I need to use the cursor and 'Insert into' statement. Could anybody help me to write the function for this operation?

Kamor04
  • 307
  • 1
  • 5
  • 13

1 Answers1

0

This a minimal example of what you are trying to accomplish - Insert the value in entry to db when a button is pressed. The main 2 concepts which are important for this are the

  1. command option for the button - which calls a function when it is clicked and
  2. get method of the entry widget that returns the text in the widget.
from tkinter import *
import sqlite3
root = Tk()
conn = sqlite3.connect('my_database.db')
#create a table for testing
sql_create_projects_table = """ CREATE TABLE IF NOT EXISTS my_table (
                                        name text
                                    ); """
conn.execute(sql_create_projects_table)

#function to be called when button is clicked
def savetodb():
    #txtName.get() will get the value in the entry box
    entry_name=txtName.get()
    conn.execute('insert into my_table(name) values (?)', (str(entry_name),))
    curr=conn.execute("SELECT name from my_table")
    print(curr.fetchone())

txtName = Entry(root)
txtName.pack()

#function savetodb will be called when button is clicked
btnSave = Button(root ,text = "Save",command=savetodb)
btnSave.pack()

root.mainloop()
Bitto
  • 7,937
  • 1
  • 16
  • 38
  • When I want to add every variable into database I receive sqlite3.IntegrityError: datatype mismatch. I think the problem is with `id_customer`, because it is integer's primary key. Can you show me how to fix that? – Kamor04 Feb 09 '19 at 13:21
  • @Kamor04 is it set to auto_increment? – Bitto Feb 09 '19 at 13:24
  • Yes, exactly like in class `Customers` in my main post. – Kamor04 Feb 09 '19 at 13:31
  • @Kamor04 While inserting don't specify the id_customer. Just like in my code explicitly specify the columns which you want to insert data into -- "insert into my_table(name) values (?)" – Bitto Feb 09 '19 at 13:34
  • For `conn.execute('insert into customers (name, adress, postcode, phone_number) values (?,?,?,?)', (str(entry_name),str(entry_adress),str(entry_telephone), str(entry_postcode)))` I receive error **NOT NULL constraint failed: customers.id_customer** – Kamor04 Feb 09 '19 at 13:43
  • @Kamor04 Read https://stackoverflow.com/questions/508627/auto-increment-in-sqlite-problem-with-python – Bitto Feb 09 '19 at 13:47
  • Yes, I use sqlite3. I also use SQLAlchemy for the model of my database. I thought `primary_key = True` set auto_increment automatically. – Kamor04 Feb 09 '19 at 13:50
  • 1
    @Kamor04 I don't think that's the case. Please go through the link in my comment. – Bitto Feb 09 '19 at 13:53