0

我想从 OpenERP 中的 mysql 获取一些数据。

在一种方式中,我可以这样做:

#!/usr/bin/python
import MySQLdb

# connect
db = MySQLdb.connect(host="localhost", user="appuser", passwd="",
db="onco")

cursor = db.cursor()

# execute SQL select statement
cursor.execute("SELECT * FROM LOCATION")

# commit your changes
db.commit()

# get the number of rows in the resultset
numrows = int(cursor.rowcount)

# get and display one row at a time.
for x in range(0,numrows):
    row = cursor.fetchone()
    print row[0], "-->", row[1]

来自如何在 Python 中连接到 MySQL 数据库?

但这可能是更聪明的方法吗?要像标准 OpenERP 对象一样使用 cr?

4

1 回答 1

0

你的方法没问题,但是:

  1. 你不需要db.commit()之后SELECT。仅当您更改数据库中的某些内容时才有必要。

  2. 而不是获取行数,for x in range(0, numrows)您可以使用for x in cursor.fetchall():. 要仅获取n您可以使用的元素cursor.fetchmany(n)

于 2014-05-04T14:30:18.213 回答