10 Replies - 1136 Views - Last Post: 01 December 2011 - 09:36 AM Rate Topic: -----

#1 Ismatus2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 29-November 11

Tkinter PostgreSQL Inserting datas

Posted 30 November 2011 - 09:14 AM

Hello everybody , I could finaly insert datas to a PostgreSQL database from a python , using Tkinter interface , but now I need to insert datas from the Tkinter Entries , i have 3 Entries ( Prénom , Nom , Phone ) , and i have 1 table firstbasetable that contain ( Id , Prenom , Nom , Phone ), my Insert function is :

def insere() :
    firstnamevar=firstname_entry.get()
    lastnamevar=lastname_entry.get()
    Phonevar=Phone_entry.get()
    print '%s  :  %s   :  %s'%(firstnamevar, lastnamevar, Phonevar)
    connect()
    cur = conn.cursor()
    a = 'Prénom3'

    #cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)", (100, "abc'def"))


 
    #cur.execute("INSERT INTO firstbasetable VALUES (5, 'Prénom5', 'Nom5', '0000000005')")
    cur.execute("INSERT INTO firstbasetable ( Prenom, Nom, Phone) VALUES (%s, %s, %s)",(firstnamevar, lastnamevar, Phonevar )) 
    #cur.executemany("""INSERT INTO firstbasetable(Prenom, Nom, Phone) VALUES (%(first_name)s, %(firstnamevar, lastnamevar, Phonevar )s)""", namedict)

   
    cur.close()
    conn.commit()


But when i cklick the Button to insert datas , i have this Error message :

ProgrammingError: ERREUR:  la colonne « prenom » de la relation « firstbasetable » n'existe pas
LINE 1: INSERT INTO firstbasetable ( Prenom, Nom, Phone) VALUES (E't...



Is This A Good Question/Topic? 0
  • +

Replies To: Tkinter PostgreSQL Inserting datas

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Tkinter PostgreSQL Inserting datas

Posted 30 November 2011 - 09:26 AM

I can't be sure, but I think your issue is that you haven't quoted your strings. Try this:
cur.execute("INSERT INTO firstbasetable ( Prenom, Nom, Phone) VALUES ('%s', '%s', '%s')",(firstnamevar, lastnamevar, Phonevar ))



If that doesn't work, print() the query and post it here.
Was This Post Helpful? 0
  • +
  • -

#3 Ismatus2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 29-November 11

Re: Tkinter PostgreSQL Inserting datas

Posted 30 November 2011 - 09:31 AM

Thank u friend , it become better then before :) but still have a problem the new error message is :

Prénom
sdfsdf  :  sdfqsdf   :  sdfqsdf
Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1413, in __call__
    return self.func(*args)
  File "./TkDbPostgresql.py", line 83, in insere
    cur.execute("INSERT INTO firstbasetable ( Prenom, Nom, Phone) VALUES ('%s', '%s', '%s')",(firstnamevar, lastnamevar, Phonevar ))
ProgrammingError: ERREUR:  erreur de syntaxe sur ou près de « sdfsdf »
LINE 1: ...O firstbasetable ( Prenom, Nom, Phone) VALUES ('E'sdfsdf'', ...
                                                             ^


Was This Post Helpful? 0
  • +
  • -

#4 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Tkinter PostgreSQL Inserting datas

Posted 30 November 2011 - 09:49 AM

Ignore my last piece of advice, your code was fine.

Judging by the error message you probably have the column name wrong (is PGSQL case-sensitive?).
Was This Post Helpful? 0
  • +
  • -

#5 Ismatus2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 29-November 11

Re: Tkinter PostgreSQL Inserting datas

Posted 30 November 2011 - 09:50 AM

what should i do now please ? :)
Was This Post Helpful? 0
  • +
  • -

#6 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Tkinter PostgreSQL Inserting datas

Posted 30 November 2011 - 09:51 AM

Undo the change I suggested and put in the correct column name for your INSERT statement.
Was This Post Helpful? 0
  • +
  • -

#7 Ismatus2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 29-November 11

Re: Tkinter PostgreSQL Inserting datas

Posted 30 November 2011 - 09:57 AM

this is what i did ; ok i show u the code ok?

#! /usr/bin/python
#-*- coding:Utf-8-*-

from Tkinter import *
import ttk
import psycopg2

def insere() :
    firstnamevar=firstname_entry.get()
    lastnamevar=lastname_entry.get()
    Phonevar=Phone_entry.get()
    print '%s  :  %s   :  %s'%(firstnamevar, lastnamevar, Phonevar)
    connect()
    cur = conn.cursor()   
    cur.execute("INSERT INTO firstbasetable ( Prenom, Nom, Phone) VALUES (%s, %s, %s)",(firstnamevar, lastnamevar, Phonevar ))  
    cur.close()
    conn.commit()
 
def connect() :
    global conn
    conn = psycopg2.connect("host='localhost' dbname='firstbase' user='postgres' password='bakpostgresql' ")

fenetre=Tk()
fenetre.geometry("600x400")
fenetre.title('             Test Data')  

firstnamevar = StringVar()
lastnamevar = StringVar()
Phonevar = IntVar()

f1 = Frame(fenetre, bg="#290080",  width=500, height=500)
f1.pack( fill=X, expand=0)
f2 = Frame(fenetre, bg="white",  width=215, height=135)
f2.place(x=130 , y=213)

lab1 = Label(fenetre, text="Voila la table testtable de la base testbase" , bg = "#290080", fg = "white" )
lab1.place ( x=100 , y=15 )

lab2 = Label(fenetre, text="Prénom" , bg = "#290080", fg = "white" )
lab2.place ( x=30 , y=100 )
lab3 = Label(fenetre, text="Nom" , bg = "#290080", fg = "white" )
lab3.place ( x=30 , y=125)
lab4 = Label(fenetre, text="Phone" , bg = "#290080", fg = "white" )
lab4.place ( x=30 , y=150)
lab5 = Label(fenetre, text="Données : " , bg = "#290080", fg = "white" , anchor=W, justify=LEFT)
lab5.place ( x=30 , y=220)

lab6 = Label(fenetre, text="" , bg = "white", fg = "#290080" , anchor=W, justify=LEFT)
lab6.place ( x=130 , y=220)

listb=Listbox(fenetre, fg = "#290080", width = "10")
listb.place(x = 380, y = 215 )
listb.insert(END, "  Prénom  ")
listb.itemconfig(0, fg = "black")

firstname_entry = ttk.Entry(fenetre, width=15, textvariable=firstnamevar)
firstname_entry.place (x = 100 , y = 100 )
lastname_entry = ttk.Entry(fenetre, width=15, textvariable=lastnamevar)
lastname_entry.place (x = 100 , y = 125 )
Phone_entry = ttk.Entry(fenetre, width=15, textvariable=Phonevar)
Phone_entry.place (x = 100 , y = 150 )
Phonevar.set("")

afficher = Button(fenetre, text = "Afficher" , command = afficher)
afficher.place( x=315, y=80 )
inserer = Button(fenetre, text = "Inserer" , command = insere)
inserer.place( x=315, y=110 )
modifier = Button(fenetre, text = "Modifier" ) #, command = modif)
modifier.place( x=315, y=140 )
supprimer = Button(fenetre, text = "Supprimer" , command = Vider)
supprimer.place( x=315, y=170 )
Quitter = Button(fenetre, text = "Quitter" , command = fenetre.quit)
Quitter.place( x=200, y=350 )

fenetre.after(0,center,fenetre)
fenetre.mainloop()


I hope you can change the code and it works , thank u a lot .
Was This Post Helpful? 1
  • +
  • -

#8 Ismatus2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 29-November 11

Re: Tkinter PostgreSQL Inserting datas

Posted 01 December 2011 - 03:07 AM

Hello again , you know ? if i put :

cur.execute("INSERT INTO firstbasetable VALUES (5, 'Prénom5', 'Nom5', '0000000005')")


It works good the function insert , and if i make :

cur.execute("INSERT INTO firstbasetable (Idpersonnes, Prenom, Nom, Phone) VALUES (6, 'Prénom6', 'Nom6', '0000000006')")


it shows the Error message :

ProgrammingError: ERREUR:  la colonne « idpersonnes » de la relation « firstbasetable » n'existe pas
LINE 1: INSERT INTO firstbasetable (Idpersonnes, Prenom, Nom, Phone)...



Can someone tell me from where the problem came from ? thank you .
Was This Post Helpful? 0
  • +
  • -

#9 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Tkinter PostgreSQL Inserting datas

Posted 01 December 2011 - 08:17 AM

Your column name is wrong.
Was This Post Helpful? 0
  • +
  • -

#10 Ismatus2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 29-November 11

Re: Tkinter PostgreSQL Inserting datas

Posted 01 December 2011 - 09:22 AM

Hello Motoma , thank you for answering , I finaly could insert datas from a Tkinter window .

Now , in a treeview in my window i couldn't insert to it 'é' nor 'à' , the accented characters in general , so how to deal with that ???
Was This Post Helpful? 0
  • +
  • -

#11 Ismatus2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 29-November 11

Re: Tkinter PostgreSQL Inserting datas

Posted 01 December 2011 - 09:36 AM

this is all my code python :

#! /usr/bin/python
#-*- coding:Utf-8-*-

from Tkinter import *
import ttk


def center(window):

    sw = window.winfo_screenwidth()
    sh = window.winfo_screenheight()
    rw = window.winfo_reqwidth()
    rh = window.winfo_reqheight()
    xc = (sw - rw) / 2
    yc = (sh -rh) / 2
    window.geometry("+%d+%d" % (xc, yc))
    window.deiconify() 

# La fonction inserer ou afficher

def afficher() : 

    tv.insert("","end", values=('é', 'é', 'é'))




# Linterface :


fenetre=Tk()
fenetre.geometry("700x400")
fenetre.title('             Test Data')  

firstnamevar = StringVar()
lastnamevar = StringVar()
Phonevar = IntVar()

f1 = Frame(fenetre, bg="#290080",  width=500, height=500)
f1.pack( fill=X, expand=0)
f2 = Frame(fenetre, bg="white",  width=215, height=135)
f2.place(x=130 , y=213)

lab1 = Label(fenetre, text="Voila la table testtable de la base testbase" , bg = "#290080", fg = "white" )
lab1.place ( x=100 , y=15 )

lab2 = Label(fenetre, text="Prénom" , bg = "#290080", fg = "white" )
lab2.place ( x=30 , y=100 )
lab3 = Label(fenetre, text="Nom" , bg = "#290080", fg = "white" )
lab3.place ( x=30 , y=125)
lab4 = Label(fenetre, text="Phone" , bg = "#290080", fg = "white" )
lab4.place ( x=30 , y=150)
lab5 = Label(fenetre, text="Données : " , bg = "#290080", fg = "white" , anchor=W, justify=LEFT)
lab5.place ( x=30 , y=220)

lab6 = Label(fenetre, text="" , bg = "white", fg = "#290080" , anchor=W, justify=LEFT)
lab6.place ( x=130 , y=220)

listb=Listbox(fenetre, fg = "#290080", width = "10")
listb.place(x = 380, y = 215 )
listb.insert(END, "  Prénom  ")
listb.itemconfig(0, fg = "black")


# Les entrées


firstname_entry = ttk.Entry(fenetre, width=15, textvariable=firstnamevar)
firstname_entry.place (x = 100 , y = 100 )
lastname_entry = ttk.Entry(fenetre, width=15, textvariable=lastnamevar)
lastname_entry.place (x = 100 , y = 125 )
Phone_entry = ttk.Entry(fenetre, width=15, textvariable=Phonevar)
Phone_entry.place (x = 100 , y = 150 )
Phonevar.set("")

scrollbar = Scrollbar(fenetre)
scrollbar.place (x = 670 , y = 170)

# la creation de la TreeView 

tv = ttk.Treeview(fenetre, show='headings',  height =3, yscrollcommand=scrollbar.set)
tv["columns"]=("col1","col2","col3")
tv.column("col1",width=80,anchor="center", stretch = True, minwidth = 50)
tv.column("col2",width=80,anchor="center")
tv.column("col3",width=110,anchor="center")
tv.heading("col1",text="Prénom")
tv.heading("col2",text="Nom")
tv.heading("col3",text="Phone number")
tv.place( x = 400 , y = 120)
scrollbar.config(command=tv.yview)

# Les bouttons 

afficher = Button(fenetre, text = "Afficher" , command = afficher)
afficher.place( x=290, y=80 )
inserer = Button(fenetre, text = "Inserer" )
inserer.place( x=290, y=110 )
modifier = Button(fenetre, text = "Modifier" ) 
modifier.place( x=290, y=140 )
supprimer = Button(fenetre, text = "Supprimer" )
supprimer.place( x=290, y=170 )
Quitter = Button(fenetre, text = "Quitter" , command = fenetre.quit)
Quitter.place( x=200, y=350 )

fenetre.after(0,center,fenetre)
fenetre.mainloop()


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1