我正在尝试使用 SQLAlchemy 连接到 Postgres 数据库。我已经安装了 psycopg2。但是,我收到错误sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:postgres。如何配置 SQLAlchemy 以连接到 PostgreSQL?
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "postgres://username@localhost:5432/template1"
db = SQLAlchemy(app)
Run Code Online (Sandbox Code Playgroud) 我试图为教程安装postgres,但是pip给了我错误:
pip install psycopg
Run Code Online (Sandbox Code Playgroud)
我得到一个错误:
Error: pg_config executable not found.
Please add the directory containing pg_config to the PATH
or specify the full executable path with the option:
python setup.py build_ext --pg-config /path/to/pg_config build ...
or with the pg_config option in 'setup.cfg'.
Run Code Online (Sandbox Code Playgroud)
pg_config我的virtualenv 在哪里?如何配置?我正在使用virtualenv,因为我不想在系统范围内安装postgres.
在我的代码我用的是DictCursor从psycopg2.extras这样的
dict_cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
Run Code Online (Sandbox Code Playgroud)
但是,当我加载游标时,突然间我收到以下错误:
AttributeError: 'module' object has no attribute 'extras'
Run Code Online (Sandbox Code Playgroud)
也许在我的装置中有些东西,但我不知道从哪里开始寻找.我用pip做了一些更新,但据我所知,没有依赖关系psycopg2.
我正在尝试为我的django项目建立一个PostgreSQL数据库,我相信我现在已经完成了,这要归功于我为django项目设置postgreSQL数据库时遇到的问题.我现在正试图在终端中运行命令'python manage.py runserver'来获取我的本地主机但是当我运行命令时,我看到这个响应...
Error: No module named psycopg2.extensions
Run Code Online (Sandbox Code Playgroud)
我不确定这意味着什么 - 我试图下载psycopg2但似乎找不到使用自制软件下载psycopg2的方法.我已经尝试过easy_install,pip install和sudo但是所有的返回错误都是这样的......
Downloading http://www.psycopg.org/psycopg/tarballs/PSYCOPG-2-4/psycopg2-2.4.5.tar.gz
Processing psycopg2-2.4.5.tar.gz
Writing /tmp/easy_install-l7Qi62/psycopg2-2.4.5/setup.cfg
Running psycopg2-2.4.5/setup.py -q bdist_egg --dist-dir /tmp/easy_install-l7Qi62/psycopg2-2.4.5/egg-dist-tmp-PBP5Ds
no previously-included directories found matching 'doc/src/_build'
unable to execute gcc-4.0: No such file or directory
error: Setup script exited with error: command 'gcc-4.0' failed with exit status 1
Run Code Online (Sandbox Code Playgroud)
如何解决这个问题?
我尝试将psycopg2安装到我的环境中,但是我收到以下错误:
(venv)avlahop@apostolos-laptop:~/development/django/rhombus-dental$ sudo pip install psycopg2
Downloading/unpacking psycopg2,
Downloading psycopg2-2.5.2.tar.gz (685kB): 685kB downloaded
Running setup.py egg_info for package psycopg2
Installing collected packages: psycopg2
Running setup.py install for psycopg2
building 'psycopg2._psycopg' extension
x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall Wstrict-prototypes -fPIC -DPSYCOPG_DEFAULT_PYDATETIME=1 -DPSYCOPG_VERSION="2.5.2 (dt dec pq3 ext)" -DPG_VERSION_HEX=0x09010D -DPSYCOPG_EXTENSIONS=1 -DPSYCOPG_NEW_BOOLEAN=1 -DHAVE_PQFREEMEM=1 -I/usr/include/python2.7 -I. -I/usr/include/postgresql -I/usr/include/postgresql/9.1/server -c psycopg/psycopgmodule.c -o build/temp.linux-x86_64-2.7/psycopg/psycopgmodule.o -Wdeclaration-after-statement
In file included from psycopg/psycopgmodule.c:27:0:
./psycopg/psycopg.h:30:20: fatal error: Python.h: ??? ??????? ?????? ?????? ? ?????????
#include <Python.h>
^
compilation terminated. …Run Code Online (Sandbox Code Playgroud) 是否有可能安装psycopg2到virtualenv当我的开发系统的MacBook Pro与OS X 10.6没有安装PostgreSQL的?
当我pip install psycopg2在我的内部运行时virtualenv,我收到了下面显示的错误.
我正在尝试使用Django连接到服务器上的遗留数据库,如果可能的话,我宁愿不在我的开发系统上安装PostgreSQL.
使用自制软件安装PostgreSQL时收到错误.我在我的MacBook Pro上安装了Xcode4并且只安装了Xcode4,我认为这与缺少gcc 4.0有关.但是,这是另一个StackOverflow问题的问题.
更新于2011年4月12日上午8:37:我仍然想知道如果不在我的MacBook Pro上安装PostgreSQL就可以实现这一点.然而,我跑了brew update并强迫重新安装ossp-uuid,brew install --force ossp-uuid现在可行brew install postgresql.随着PostgreSQL的成功安装,我能够pip install psycopg2从我的virtualenv中获得.
pip install psycopg2$ pip install psycopg2
Downloading/unpacking psycopg2
Running setup.py egg_info for package psycopg2
Error: pg_config executable not found.
Please add the directory containing pg_config to the PATH
or specify the full executable path with the option:
python setup.py …Run Code Online (Sandbox Code Playgroud) 在SQL语句中通过%s使列表(countryList)可用的正确方法是什么?
# using psycopg2
countryList=['UK','France']
sql='SELECT * from countries WHERE country IN (%s)'
data=[countryList]
cur.execute(sql,data)
Run Code Online (Sandbox Code Playgroud)
就像现在一样,它在尝试运行"(ARRAY [...])中的WHERE国家"后出错.除了通过字符串操作之外,有没有办法做到这一点?
谢谢
尝试使用postgres mac应用程序设置postgres并点击此错误,这是我无法解决的.有什么想法吗?
ImportError: dlopen(/Users/Craig/pyenv/mysite/lib/python2.7/site-packages/psycopg2/_psycopg.so, 2): Library not loaded: @executable_path/../lib/libssl.1.0.0.dylib
Referenced from: /Applications/Postgres.app/Contents/MacOS/lib/libpq.dylib
Reason: image not found
Run Code Online (Sandbox Code Playgroud) 我之前没有使用psycopg2,但我正在尝试将光标工厂更改为DictCursor,以便fetchall或fetchone将返回字典而不是列表.
我创建了一个测试脚本,使事情变得简单,只测试这个功能.这是我觉得应该工作的一点代码
import psycopg2
import psycopg2.extras
conn = psycopg2.connect("dbname=%s user=%s password=%s" % (DATABASE, USERNAME, PASSWORD))
cur = conn.cursor(cursor_factory = psycopg2.extras.DictCursor)
cur.execute("SELECT * from review")
res = cur.fetchall()
print type(res)
print res
Run Code Online (Sandbox Code Playgroud)
res变量总是一个列表,而不是我期望的字典.
我实现的当前解决方法是使用此函数来构建字典并运行fetchall通过它返回的每一行.
def build_dict(cursor, row):
x = {}
for key,col in enumerate(cursor.description):
x[col[0]] = row[key]
return d
Run Code Online (Sandbox Code Playgroud)
Python是版本2.6.7,psycopg2是版本2.4.2.
我需要将龙卷风中的JSON数据插入到postgres中,所以这里的测试如下:
from psycopg2 import connect
conn = connect("user='pguser' host='localhost' dbname='pgdb' password='pgpass'")
cursor = conn.cursor()
data = '[{"id":"sdf","name":"wqe","author":"vb"}]'
for row in eval(data):
print row
cursor.execute("""INSERT INTO books(id,name,author) VALUES('%s','%s','%s')""" % \
(row['id'], row['name'], row['author'])
)
>>> cursor.execute("SELECT * FROM books")
>>> cursor.fetchall()
[('sdf', 'wqe', 'vb')]
>>>
$> psql -d pgdb -U pguser -W
Password for user pguser:
psql (9.1.6)
Type "help" for help.
pgdb=> select * from books;
id | name | author
----+------+--------
(0 rows)
Run Code Online (Sandbox Code Playgroud)
正如你select在python shell中看到的那样,有一些数据,但在psql中有0行!我可能做错了什么?
Python 2.7.2+
psycopg2 ×10
python ×9
postgresql ×6
django ×2
macos ×2
dictionary ×1
heroku ×1
importerror ×1
pip ×1
sqlalchemy ×1
virtualenv ×1
where-in ×1