使用cr连接OpenERP和mysql?

时间:2014-04-30 09:27:47

标签: openerp openerp-7 openerp-8

我想在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吗?

1 个答案:

答案 0 :(得分:0)

你的方式还可以,但是:

  1. db.commit()之后您不需要SELECT。只有在数据库中更改内容时才需要它。

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