import psycopg2
import numpy
conn = psycopg2.connect(host='localhost', user='bob', passwd='mypasswd', db='bigdb')
curs = conn.cursor()
numrows = curs.execute("SELECT id, rating FROM video")
#dtype='i4,i4' means two columns, both 4 byte (32 bit) integers
# you will have to adapt to your data type and data structure
A = numpy.fromiter(curs.fetchall(), count=numrows, dtype=('i4,i4'))
如果您使用的是PostgreSQL,那么COPY或\ COPY是您唯一的选择。你还没说为什么那是不可行的
我假设数据库正在更改,您希望将最新的数据拉入Python程序。如果数据库是静态的,那么您可以
解决此问题的另一种方法是使用数据库连接器从Python中提取数据This solution是特定于MySQL的,但应该使用PostgreSQL数据连接器。以下是适用于PostgreSQL的代码:
相关问题 更多 >
编程相关推荐