ポスグレのDBからデータを取得して一覧に表示させたいです。
ポスグレのDBには接続されます。
実行すると
for r in conn.execute(sql):
AttributeError: 'psycopg2.extensions.connection' object has no attribute 'execute'
というエラーが表示されます。
改善方法を調べていると
cur = conn.cursor()
cur.execute()
が必要と書いている記事がありましたが、
executeが参照?できていないのでエラーが出ているのかなと思っていますが
改善方法が分かりません。
どなたかご教授ください。
追記)
cursor= conn.cursor()
cursor.execute(aql)追加
'NoneType' object is not iterableというエラーがでる
# -*- coding: utf-8 -*- import sys import tkinter from tkinter import ttk from tkinter import font import psycopg2 # connect postgreSQL conn = psycopg2.connect(host="ホスト", database="データベース名", user="postgres", port=5432, password="パスワード") # 追記) cursor= conn.cursor() # 最初の画面に戻るのだ def return_view(): root_new_csv.destroy() root_new_csv = tkinter.Tk() # 画面サイズ root_new_csv.geometry('800x480') #画面タイトル root_new_csv.title(u"一覧") # ツリービューの作成 tree = ttk.Treeview(root_new_csv, height=10) tree["columns"] = (1, 2, 3, 4, 5) tree["show"] = "headings" tree.column(1, width=120) tree.column(2, width=120) tree.column(3, width=120) tree.column(4, width=120) tree.column(5, width=70) # tree.column(6, width=170) tree.heading(1, text="ID") tree.heading(2, text="名") tree.heading(3, text="よみ") tree.heading(4, text="生年月日") tree.heading(5, text="年齢") # tree.heading(6, text="メモ") # ツリービューのスタイル変更 style = ttk.Style() # TreeViewの全部に対して、フォントサイズの変更 style.configure("Treeview", font=("", 10)) # TreeViewのHeading部分に対して、フォントサイズの変更と太字の設定 style.configure("Treeview.Heading", font=("", 10, "bold")) # SELECT文の作成 sql = """ SELECT id,name,kana,birth,memo FROM gs_table """ # ツリービューに要素の追加 # cursor.execute(sql) 追記) i = 0 for r in cursor.execute(sql): # 金額(r[2])を通貨形式に変換 r = (r[0], r[1], r[2],r[3],r[4]) tree.insert("", "end", tags=i, values=r) if i & 1: tree.tag_configure(i, background="#CCFFFF") i += 1 # ツリービューの配置 tree.place(x=40, y=200) root_new_csv.mainloop()

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/10/02 10:55