python - I have error 'Attempt to use a closed connection.' -


i have problems code. so, want add data accces writing in form. (i not in programming).

from tkinter import * import pypyodbc import ctypes  form=tk () form.title ("add data") form.geometry ('400x200')  #create connection con = pypyodbc.connect('driver={microsoft access driver (*.mdb)};uid=admin;usercommitsync=yes;threads=3;safetransactions=0;pagetimeout=5;maxscanrows=8;maxbuffersize=2048;fil={ms access};driverid=25;defaultdir=c:/users/hp/desktop/pitl;dbq=c:/users/hp/desktop/pitl/pitl.mdb;') cursor = con.cursor ()  = entry (form, width=20, font="arial 16") a.pack () b = entry (form, width=20, font="arial 16") b.pack ()  def add (event):     cursor.execute ("insert crime (`number_of_article`, `id_of_criminal`) values (?, ?)", (a, b))  con.commit () cursor.close () con.close ()  button=button(form, text = 'push me') button.pack () button.bind ('<button-1>', add)  form.mainloop () 

my error is:

exception in tkinter callback traceback (most recent call last):   file "c:\users\hp\appdata\local\programs\python\python36-32\lib\tkinter\__init__.py", line 1699, in __call__     return self.func(*args)   file "c:\users\hp\desktop\pitl\add data.py", line 19, in add     cursor.execute ("insert crime (`number_of_article`, `id_of_criminal`) values (?, ?)", (a, b))   file "c:\users\hp\appdata\local\programs\python\python36-32\lib\site-packages\pypyodbc-1.3.4-py3.6.egg\pypyodbc.py", line 1470, in execute     self._free_stmt(sql_close)   file "c:\users\hp\appdata\local\programs\python\python36-32\lib\site-packages\pypyodbc-1.3.4-py3.6.egg\pypyodbc.py", line 1987, in _free_stmt     raise programmingerror('hy000','attempt use closed connection.') pypyodbc.programmingerror: ('hy000', 'attempt use closed connection.') 

also, if change code (below) a = '*any number*' b = '*any number*', program work

a = entry (form, width=20, font="arial 16") a.pack () b = entry (form, width=20, font="arial 16") b.pack () 

you're closing connections before add called, move close calls bottom of code.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -