把后天与炼金术联系起来

我知道这可能是一个很简单的问题,但我不知道解决方法。当我尝试连接到后期时,这里发生了什么?我是自学者在这个领域的数据库和编程,所以请对我温柔。 当我尝试下面的代码:

import sqlalchemy
db = sqlalchemy.create_engine('postgresql:///tutorial.db')

我得到了这个错误:

追溯(最近一次通话) : “文件”第一行 Db = sqllegy.create _ engine (‘ postgreql:///Tutorial.db’) 在 create _ engine 中的文件“ C: Python27 lib site-packagsql伟力 -0.7.5 dev-py2.7. egg sql伟力引擎 _ _ init _ _. py”,第327行 Create (* args,* * kwargs) 文件“ C: Python27 lib site-packages sqllegy-0.7.5 dev-py2.7. egg sqllegy engine strategies.py”,第64行,在 create 中 DBapi = 方言 _ cls. dBapi (* * dBapi _ args) 文件“ C: Python27 lib site-packagsqltancy-0.7.5 dev-py2.7. egg sqltancy diects postgreql psycopg2.py”,第289行,dBapi Psycopg = 进口(‘ Psycopg2’) 重要错误: 没有名为 Psycopg2的模块

我需要单独安装 Psycopg2吗? postgreql 的正确连接字符串是什么?

180353 次浏览

Yes, psycopg2 are basically the Python drivers for PostgreSQL that need to be installed separately.

A list of valid connection strings can be found here, yours is a bit off (you need to the username, the password and hostname as specified in the link below):

http://docs.sqlalchemy.org/en/latest/core/engines.html#postgresql

You would need to pip install SQLAlchemy and pip install psycopg2. An example of a SQLAlchemy connection string that uses psycopg2:

from sqlalchemy import create_engine
engine = create_engine('postgresql+psycopg2://user:password@hostname/database_name')

You could also connect to your database using the psycopg2 driver exclusively:

import psycopg2
conn_string = "host='localhost' dbname='my_database' user='postgres' password='secret'"
conn = psycopg2.connect(conn_string)

However, using the psycopg2 driver to connect does not take advantage of SQLAlchemy.

Yes, you need to install psycopg2 separately, if you're using linux you can simply enter the following line to the terminal: $pip install psycopg2 if this doesn't work try using sudo: $sudo pip install psycopg2