我想在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]
来自How do I connect to a MySQL Database in Python?
但这可能更聪明吗?要像标准的OpenERP对象那样使用cr吗?
答案 0 :(得分:0)
你的方式还可以,但是:
db.commit()
之后您不需要SELECT
。只有在数据库中更改内容时才需要它。
您可以使用for x in range(0, numrows)
,而不是获得行数和for x in cursor.fetchall():
。要仅获取n
元素,您可以使用cursor.fetchmany(n)
。