因此,我尝试遵循将 MySQL 结果集转换为 NumPy 数组的最有效方法是什么?但我仍然有问题。
我的数据库行是 57 个无符号整数(Unix 纪元加上 28 个交换机端口中每一个的字节数,进出)。
我的代码如下所示:
import MySQLdb as mdb
import numpy
# get the database connector
DBconn = mdb.connect('localhost', 'root', '<Password>', 'Monitoring')
with DBconn:
# prepare a cursor object using cursor() method
cursor = DBconn.cursor()
# now get the data for the last 10 minutes
sql = "select * from LowerSwitchBytes where ComputerTime >= (unix_timestamp(now())-(60*10))"
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
print row
这样会打印出 10 行,例如:
(1378151928L, 615983307L, 517980853L, 25355784L, 117110102L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 267680651L, 288368872L, 84761960L, 337403085L, 224270992L, 335381466L, 27238950843L, 549910918625L, 240002569249L, 11167210734L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 222575491L, 335850213L, 223669465L, 339800088L, 310004136202L, 16635727254L, 0L, 0L, 16590672L, 147102083L, 0L, 0L, 0L, 0L)
但是当我改变:
results = cursor.fetchall()
for row in results:
print row
至
A = numpy.fromiter(cursor.fetchall(), count=-1, dtype=numpy.uint32)
print A
我得到:
Traceback (most recent call last):
File "min.py", line 23, in <module>
A = numpy.fromiter(cursor.fetchall(), count=-1, dtype=numpy.uint32)
ValueError: setting an array element with a sequence.
知道我做错了什么吗?