[英]DATABASE NOT EXIST (psycopg2.OperationalError)
my problem is the DATABASE not exist i dont know th reason我的问题是数据库不存在我不知道原因
i did install FLASKALCHEMY and run these codes in CMD:我确实安装了 FLASKALCHEMY 并在 CMD 中运行这些代码:
-pip install flask-sqlAlqhemy
-python
- from app import db
- db.create_all()
- pip install psycopg2
this error comes to me in CMD:这个错误在 CMD 中出现在我身上:
File "C:\Users\hajar\OneDrive\Desktop\flaskpro\env\lib\site-packages\sqlalchemy\engine\default.py", line 493, in connect
return self.dbapi.connect(*cargs, **cparams)
File "C:\Users\hajar\OneDrive\Desktop\flaskpro\env\lib\site-packages\psycopg2\__init__.py", line 127, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL: database "hajar" does not exist
(Background on this error at: http://sqlalche.me/e/13/e3q8)
app.py:应用程序.py:
from flask import Flask, render_template, request, redirect,url_for
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:jojo1437@localhost/hajar'
db = SQLAlchemy(app)
class user(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_name = db.Column(db.String(80))
email = db.Column(db.String(100), unique=True)
def __init__(self, user_name, email):
self.user_name = user_name
self.email = email
def __repr__(self):
return '<User %r>' % self.user_name
@app.route('/')
def index():
return render_template('add_user.html')
@app.route('/post_user', methods=['post'])
def post_user():
user = user(request.form['username'], request.form['email'])
db.session.add(user)
db.session.commit()
return redirect(url_for('index'))
if __name__ == "__main__":
app.run()
please i realy stuck here ؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟请我真的被困在这里؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟ :( :(
You've created your database as "hajar "
with a trailing space while you are trying to connect to "hajar"
, which won't work since that database does in fact not exists.你已经创建了数据库"hajar "
后面有个空格,而你正在试图连接到"hajar"
,这是行不通的,因为该数据库实际上并不存在。
Simply remove the unnecessary space(s) in your create database query, re run your query and you should be good to go.只需删除创建数据库查询中不必要的空间,重新运行查询,就可以了。
Very easy mistake to mis, I've made these sort of "obvious" mistakes hundreds of times.很容易出错,我已经犯了数百次这种“明显”的错误。
Your error means that you did not create a data base.您的错误意味着您没有创建数据库。
See this tutorial - Define and Access the Database - for more information on this topic.有关此主题的更多信息,请参阅 本教程 - 定义和访问数据库。
You can try to use flask init-db
- see here .您可以尝试使用flask init-db
- 请参见此处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.