前提
現在教本を元にFlaskでデータベースの中身を表示するシステムを作っています。
表示機能を実装中にエラーメッセージが発生しました。
実現したいこと
Postgresqlに接続してデータベースの中を表示する機能を動作するようにしたいです。
発生している問題・エラーメッセージ
File "c:\users\hoge\appdata\local\programs\python\python36\lib\site-packages\flask\app.py", line 2091, in __call__ return self.wsgi_app(environ, start_response) File "c:\users\hoge\appdata\local\programs\python\python36\lib\site-packages\flask\app.py", line 2076, in wsgi_app response = self.handle_exception(e) File "c:\users\hoge\appdata\local\programs\python\python36\lib\site-packages\flask\app.py", line 2073, in wsgi_app response = self.full_dispatch_request() File "c:\users\hoge\appdata\local\programs\python\python36\lib\site-packages\flask\app.py", line 1518, in full_dispatch_request rv = self.handle_user_exception(e) File "c:\users\hoge\appdata\local\programs\python\python36\lib\site-packages\flask\app.py", line 1516, in full_dispatch_request rv = self.dispatch_request() File "c:\users\hoge\appdata\local\programs\python\python36\lib\site-packages\flask\app.py", line 1502, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args) File "C:\Users\hoge\Desktop\flask\ansaa.xyz\ansaa.py", line 14, in index cur.execute('SELECT * FROM messages') File "c:\users\hoge\appdata\local\programs\python\python36\lib\site-packages\psycopg2\extras.py", line 146, in execute return super().execute(query, vars) psycopg2.errors.UndefinedTable: リレーション"messages"は存在しません LINE 1: SELECT * FROM messages
該当のソースコード
ansaa.py
1from flask import Flask,request,render_template 2import psycopg2 3import psycopg2.extras 4 5app = Flask(__name__) 6if __name__ == '__main__': 7 app.run(debug=True) 8 9conn = psycopg2.connect(host='localhost',port='5432',dbname='testdb',user='postgres',password='hogehoeg') 10cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) 11 12@app.route('/',methods=['GET']) 13def index(): 14 cur.execute('SELECT * FROM messages') 15 messages = cur.fetchall() 16 messages = [dict(message) for message in messages] 17 return render_template('index.html',messages=messages)
index.html
1<!DOCTYPE html> 2<html lang="ja"> 3<head> 4 <meta charset="UTF-8"> 5 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 6 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 7 <title>Document</title> 8</head> 9<body> 10 <div> 11 {% for msg in messages %} 12 <h2>{{msg.username}}</h2> 13 <p>{{msg.message}}</p> 14 {% endfor %} 15 </div> 16</body> 17</html>
postgresql
1postgres=# \l 2 データベース一覧 3 名前 | 所有者 | エンコーディング | 照合順序 | Ctype(変換演算子) | アクセス権限 4-----------+----------+------------------+--------------------+--------------------+----------------------- 5 postgres | postgres | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 | 6 template0 | postgres | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres + 7 | | | | | postgres=CTc/postgres 8 template1 | postgres | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres + 9 | | | | | postgres=CTc/postgres 10 testdb | postgres | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 |
postgresql
1postgres=# \d messages 2 テーブル"public.messages" 3 列 | タイプ | 照合順序 | Null 値を許容 | デフォルト 4----------+------------------------+----------+---------------+------------ 5 username | character varying(64) | | | 6 message | character varying(140) | | |
試したこと
テーブル名や記述ミスが無いか確認したのですが見あたりませんでした。
また、CMDからデータベースに接続出来るか下記コマンドで検証して接続できました。
C:\Users\hoge>psql -U postgres -d testdb -h 127.0.0.1 -p 5432 ユーザー postgres のパスワード: psql (14.6) "help"でヘルプを表示します。 testdb=#
スキーマという概念がある事をしり確認してみましたがpublic以外表示されませんでした。
postgres=# select current_schema; current_schema ---------------- public (1 行) postgres=# \dn スキーマ一覧 名前 | 所有者 --------+---------- public | postgres (1 行)
原因の切り分け方法等ご教示いただけますと幸いです。
よろしくお願いいたします。
補足情報(FW/ツールのバージョンなど)
Windows10 pro
postgresql SQL 14

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