Question

I would like to get some data from mysql in OpenERP.

In one way I can do it like that:

#!/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]

from How do I connect to a MySQL Database in Python?

But is it maybe smarter way to do it? To use cr like standard OpenERP object?

Was it helpful?

Solution

Your way is ok, but:

  1. You don't need db.commit() after SELECT. It's necessary only if you change something in database.

  2. Instead of getting the number of rows and for x in range(0, numrows) you can use for x in cursor.fetchall():. To get only n elements you can use cursor.fetchmany(n).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top