In Tkinter - having an input and an entry

  • Thread starter markfernandes02
  • Start date
M

markfernandes02

I need some advice,
I am creating a python program to communicate with an MS Access db in
python 2.4.
I can fetch records but cannot insert records.

def Insert(self, *row):
global cursor, title, author, pubdate
sqlInsert = "INSERT INTO Book_table"
sqlInsert = sqlInsert + "(Bookname, BookAutor, "
sqlInsert = sqlInsert + "Publicationdate) VALUES ('"
sqlInsert = sqlInsert + title + "', '"
sqlInsert = sqlInsert + author + "', "
sqlInsert = sqlInsert + pubdate + ") "
myconn = odbc.odbc('accessDatabase')
cursor = myconn.cursor()
cursor.execute(sqlInsert)
myconn.commit()
cursor.close()
myconn.close()

The above code does not work.
Also with Tkinter, i want to have user input records into the db, i
cannot get what the user inputs into the entry to become a string
variable.

If you can help it would be most appreciated.
Thanks in advanced

Mark
 
S

Steve Holden

I need some advice,
I am creating a python program to communicate with an MS Access db in
python 2.4.
I can fetch records but cannot insert records.

def Insert(self, *row):
global cursor, title, author, pubdate
sqlInsert = "INSERT INTO Book_table"
sqlInsert = sqlInsert + "(Bookname, BookAutor, "
sqlInsert = sqlInsert + "Publicationdate) VALUES ('"
sqlInsert = sqlInsert + title + "', '"
sqlInsert = sqlInsert + author + "', "
sqlInsert = sqlInsert + pubdate + ") "

First of all, read about parameterized queries so that you don't do any
more of this. Abbreviated version: formulate your query so that
cursor.execute takes a query with parameter slots in it as the first
argument and a data set as the second argument.
myconn = odbc.odbc('accessDatabase')
cursor = myconn.cursor()
cursor.execute(sqlInsert)
myconn.commit()
cursor.close()
myconn.close()

The above code does not work.

Secondly, observe that in a GUI environment the valuable traceback
information will likely be completely lost, so you should get your code
working first in a command-line based tool that will provide information
about what is going wrong. "Does not work" is way to vague for anyone to
be able to provide helpful responses. We need to see specific tracebacks
(though kudos for actually listing your code: some people effectively
just say the equivalent of "My program to do X does not work, can you
tell me what's wrong with it"!).
Also with Tkinter, i want to have user input records into the db, i
cannot get what the user inputs into the entry to become a string
variable.
That's usually a matter of calling the appropriate widget's correct
method. For example, if you have an Entry widget called e you would
retrieve the input from it by calling e.get - see

http://effbot.org/tkinterbook/entry.htm

for documentation from the effbot, the font of all Tkinter knowledge.
If you can help it would be most appreciated.
Thanks in advanced

Did my best.

regards
Steve
 
F

Fredrik Lundh

I can fetch records but cannot insert records.

def Insert(self, *row):
global cursor, title, author, pubdate

using globals to pass arguments to a function/method is usually not a
good idea. any reason you cannot pass them in as arguments?
sqlInsert = "INSERT INTO Book_table"
sqlInsert = sqlInsert + "(Bookname, BookAutor, "
sqlInsert = sqlInsert + "Publicationdate) VALUES ('"
sqlInsert = sqlInsert + title + "', '"
sqlInsert = sqlInsert + author + "', "
sqlInsert = sqlInsert + pubdate + ") "
myconn = odbc.odbc('accessDatabase')

if you're doing multiple insertions, it's usually better to connect once
and reuse the connection object.
cursor = myconn.cursor()
cursor.execute(sqlInsert)
myconn.commit()
cursor.close()
myconn.close()

The above code does not work.

define "does not work". do you get a traceback, a database error, some
other problem? can you perhaps post the traceback?
Also with Tkinter, i want to have user input records into the db, i
cannot get what the user inputs into the entry to become a string
variable.

what did you try? you can get input data from the Entry widget in
several ways, including calling the "get" method or using StringVar
objects; see

http://effbot.org/tkinterbook/entry.htm#patterns

for some code snippets.

</F>
 
M

markfernandes02

Traceback (most recent call last):
File "F:\Programming\python and database\access_db8.2.py", line 129,
in ?
Tkwindow()
File "F:\Programming\python and database\access_db8.2.py", line 88,
in Tkwindow
title = stringVar()
NameError: global name 'stringVar' is not defined

Here is the TKwindow code.

def Tkwindow():
global entry, entry2, entry3, entry4, entry5, entry6
global title, author, pubdate
root = Tk()
b1 = Button(root, text='Exit', command=root.quit)
b1.grid(column = 1, row = 4)
b2 = Button(root, text= 'Fetch', command= Fetch)
b2.grid(column = 0, row = 3)
b3 = Button(root, text= 'Clear', command= Clear)
b3.grid(column = 0, row = 4)
b4 = Button(root, text= 'Insert', command= Insert)
b4.grid(column = 1, row = 3)

#title = stringVar()
#author = stringVar()
#pubdate = stringVar()

entry = Entry(root)
entry.grid(column = 0, row = 0)
entry2 = Entry(root)
entry2.grid(column = 0, row = 1)
entry3 = Entry(root)
entry3.grid(column = 0, row = 2)

title = stringVar()
entry4 = Entry(root, textvariable = title)
entry4.grid(column = 1, row = 0)

author = stringVar()
entry5 = Entry(root, textvariable = author)
entry5.grid(column = 1, row = 1)

pubdate = stringVar()
entry6 = Entry(root, textvariable = pubdate)
entry6.grid(column = 1, row = 2)

print author
print title
print pubdate

root.mainloop()
 
F

Fredrik Lundh

Traceback (most recent call last):
File "F:\Programming\python and database\access_db8.2.py", line 129,
in ?
Tkwindow()
File "F:\Programming\python and database\access_db8.2.py", line 88,
in Tkwindow
title = stringVar()
NameError: global name 'stringVar' is not defined
False

</F>
 
M

markfernandes02

False

</F>

Thanks it sorted out my 'StringVar' problem.
I now have another problem...

Exception in Tkinter callback
Traceback (most recent call last):
File "D:\Python24\lib\lib-tk\Tkinter.py", line 1345, in __call__
return self.func(*args)
TypeError: Insert() takes at least 1 argument (0 given)

Code below

def Insert(self, *row):
global cursor, title, author, pubdate, accessDatabase
sqlInsert = "INSERT INTO Book_table (Bookname, BookAutor,
Publicationdate) VALUES('title + ',' author + ',' pubdate)"

myconn = odbc.odbc('accessDatabase') #accessDatabase is the main
connection fn. to db stored on HardDrive
cursor = myconn.cursor()
cursor.execute(sqlInsert)
myconn.commit()
cursor.close()
myconn.close()


Tkanks for your tech support.
Mark
 
F

Fredrik Lundh

Thanks it sorted out my 'StringVar' problem.
I now have another problem...

Exception in Tkinter callback
Traceback (most recent call last):
File "D:\Python24\lib\lib-tk\Tkinter.py", line 1345, in __call__
return self.func(*args)
TypeError: Insert() takes at least 1 argument (0 given)

Code below

def Insert(self, *row):
global cursor, title, author, pubdate, accessDatabase
sqlInsert = "INSERT INTO Book_table (Bookname, BookAutor,
Publicationdate) VALUES('title + ',' author + ',' pubdate)"
...

is Insert a function or a method (that is, a function defined inside a
class)? who's calling Insert? what is "self" supposed to be?

to sort out Python's function call mechanisms, try reading the following
chapters:

http://www.ibiblio.org/swaroopch/byteofpython/read/functions.html
http://docs.python.org/tut/node6.html#SECTION006600000000000000000

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,294
Latest member
LandonPigo

Latest Threads

Top