не могу вставить запись из окна tkinter в таблицу postgresql
#40013194
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
Ссылка на профиль пользователя:
|
Участник
Откуда: из России
Сообщения: 267
|
|
Добрый день!
Учусь по видеокурсу.
Конкретно ругается на знак "?" вот в этом месте:
1. 2. 3. 4.
def insert_data(self, id, marka, tip_mash):
self.c.execute('''INSERT INTO marki (id, marka, tip_mash) VALUES (?, ?, ?)''',
(id, marka, tip_mash))
self.conn.commit()
psycopg2.errors.SyntaxError: ОШИБКА: ошибка синтаксиса (примерное положение: ",")
LINE 1: INSERT INTO marki (id, marka, tip_mash) VALUES (?, ?, ?)
^
маркер стоит под запятой полсе первого знака ?
А вот весь код:
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113.
import tkinter as tk
import tkinter.ttk as ttk
import psycopg2
class Main(tk.Frame):
def __init__(self, root):
super().__init__(root)
self.init_main()
self.db = db
self.view_records()
def init_main(self):
toolbar = tk.Frame(bg='#d7d8e0')
toolbar.pack(side=tk.TOP, fill=tk.X)
self.add_img = tk.PhotoImage(file='add.gif')
btn_open_dialog = tk.Button(toolbar, text='Добавить позицию', command=self.open_dialog, bg='#d7d8e0', bd=0,
compound=tk.TOP, image=self.add_img)
btn_open_dialog.pack(side=tk.LEFT)
self.tree = ttk.Treeview(self, columns=('id', 'marka', 'tip_mash'), height=15, show='headings')
self.tree.column('id', width=40, anchor=tk.CENTER)
self.tree.column('marka', width=150, anchor=tk.CENTER)
self.tree.column('tip_mash', width=200, anchor=tk.CENTER)
self.tree.heading('id', text='id')
self.tree.heading('marka', text='Марка')
self.tree.heading('tip_mash', text='Тип машины')
self.tree.pack()
def records(self, id, marka, tip_mash):
self.db.insert_data(id, marka, tip_mash)
self.view_records()
def view_records(self):
self.db.c.execute('''SELECT * FROM marki ORDER BY id''')
[self.tree.delete(i) for i in self.tree.get_children()]
[self.tree.insert('', 'end', values=row) for row in self.db.c.fetchall()]
def open_dialog(self):
Child()
class Child(tk.Toplevel):
def __init__(self):
super().__init__(root)
self.init_child()
self.view = app
def init_child(self):
self.title('Добавить данные в Марки')
self.geometry('400x220+400+300')
self.resizable(False, False)
label_id = tk.Label(self, text='id')
label_id.place(x=50, y=50)
label_marka = tk.Label(self, text='Марка')
label_marka.place(x=50, y=80)
label_tip = tk.Label(self, text='Тип машины')
label_tip.place(x=50, y=110)
###################################
self.entry_id = ttk.Entry(self)
self.entry_id.place(x=200, y=50)
self.entry_marka = ttk.Entry(self)
self.entry_marka.place(x=200, y=80)
self.combobox = ttk.Combobox(self, values=[u'Трактор', u'Комбайн', u'Сеялка', u'Жатка', u'Подборщик'])
self.combobox.current(0)
self.combobox.place(x=200, y=110)
btn_cancel = ttk.Button(self, text='Закрыть', command=self.destroy)
btn_cancel.place(x=300, y=170)
btn_ok = ttk.Button(self, text='Добавить')
btn_ok.place(x=220, y=170)
btn_ok.bind('<Button-1>', lambda event: self.view.records(self.entry_id.get(),
self.entry_marka.get(),
self.combobox.get()))
self.grab_set()
self.focus_set()
class DB:
def __init__(self):
self.conn = psycopg2.connect(database ="agro1", user = "postgres",
password = "postgres", host = "localhost",
port = "5432")
self.c = self.conn.cursor()
def insert_data(self, id, marka, tip_mash):
self.c.execute('''INSERT INTO marki (id, marka, tip_mash) VALUES (?, ?, ?)''',
(id, marka, tip_mash))
self.conn.commit()
if __name__ == "__main__":
root = tk.Tk()
db = DB()
app = Main(root)
app.pack()
root.title("Склад запасных частей")
root.geometry("1400x750+100+50")
root.resizable(False, False)
root.mainloop()
|
|