Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Insert 0 values in database
#1
Photo 
I have this python code. I am using tk and pyodbc. when I have run this code, it inserts 0 value in user_id column in the database even I haven't enter anything in name_entry entry. how to handle same this problems.
in database: I have created tables and this is the code:
CREATE TABLE test3
( entry_id int IDENTITY(1,1) PRIMARY KEY,
 user_id int  not null,
    user_name VARCHAR  (25) not NULL,
);
and here are python code

   id_id=IntVar()
   id_entry=tk.Entry(root, textvariable=id_id)

   name_name=StringVar()
   name_entry=tk.Entry(root,textvariable=name_name)

   get_id=id_entry.get()
   get_name=name_entry.get()
   cursor.execute("""INSERT INTO test3 (user_id, user_name) VALUES (?,?)""", get_id, get_name)
any help would be greatly appreciated
[Image: Xax1I.png]
Reply
#2
missing commit
saljuaid likes this post
Reply
#3
(Oct-26-2020, 10:19 AM)Larz60+ Wrote: missing commit

no I do not just I want to make the code short for question
here are the all codes

import tkinter as tk
from tkinter import *
import pyodbc
root = tk.Tk()

id_id=IntVar()
id_entry=tk.Entry(root, textvariable=id_id)
id_entry.grid()

name_name=StringVar()
name_entry=tk.Entry(root,textvariable=name_name)
name_entry.grid()

get_id=id_entry.get()
get_name=name_entry.get()
conn = pyodbc.connect('Driver={ODBC Driver 17 for SQL Server};'
                      'Server=XXXX\SQLEXPRESS;'
                      'Database=test1;'
                      'Trusted_Connection=yes;')
cursor = conn.cursor()
cursor.execute("""INSERT INTO test3 (user_id, user_name) VALUES (?,?)""", get_id, get_name)
conn.commit()
conn.close()
root.mainloop()
Reply
#4
the code to execute the insert will execute upon instantiation.
This cannot work as nothing has been entered into the entry widget at this point.

you need to bind the entry to a callback routine, and execute the database insert within the callback.
Here's a 'how to' for this: https://www.delftstack.com/howto/python-...n-tkinter/
saljuaid and ndc85430 like this post
Reply
#5
I want to say thank you to all who had replied, and I want to give you some update, it may helps someone who has same my issue. I have added try: and except and this was solving my problem:

try:
            cnn.autocommit = False
            cursor.execute("""INSERT INTO  (A) values (?)""",AA)
        except pyodbc.DatabaseError as err:
            print(err)
            cnn.rollback()
        else:
            cnn.commit()
        finally:
            cnn.autocommit = True
            cnn.commit()
            cnn.close()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  store all variable values into list and insert to sql_summary table mg24 3 1,095 Sep-28-2022, 09:13 AM
Last Post: Larz60+
  Datas are not insert into database aravinth 1 2,152 Dec-21-2019, 04:17 PM
Last Post: buran
  Custom Function to Return Database Values rm78 0 1,764 Sep-05-2019, 01:01 PM
Last Post: rm78
  Create application that will take information from database and insert into PDF sorrelsj 1 2,042 Aug-19-2019, 10:08 PM
Last Post: Gribouillis
  Creation of Dynamic HTML by substituting Database values Sandy777 1 2,101 Apr-18-2019, 07:17 AM
Last Post: buran
  how to insert list into database zubair 2 9,865 Feb-05-2019, 12:16 PM
Last Post: rajesh1997
  Saving Values Changed in a database themick789 1 2,248 Nov-28-2018, 08:16 AM
Last Post: Larz60+
  Newbie question for bulk insert into SQL Server database zydjohn 6 12,311 Dec-14-2017, 11:04 PM
Last Post: Larz60+
  Python 3 MySQL database insert error georgian2all 7 8,235 Aug-01-2017, 06:37 PM
Last Post: Larz60+
  Insert using psycopg giving syntax error near "INSERT INTO" olgethorpe 4 15,491 Jul-21-2017, 07:39 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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