0

am trying out creating a web app using python flask framework. I've installed flask-mysql library to interact with mysql db. However, am having trouble getting rows as dictionaries rather than tuples. With the usual python-mysql library, it's a matter of adding "cursorclass=MySQLdb.cursors.DictCursor," to my database handle.

from flaskext.mysql import MySQL

app = Flask(__name__)

mysql = MySQL()
# MySQL configurations
app.config['MYSQL_DATABASE_USER'] = 'user'
app.config['MYSQL_DATABASE_PASSWORD'] = 'password'
app.config['MYSQL_DATABASE_DB'] = 'mydb'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'

mysql.init_app(app)

I've tried adding app.config['MYSQL_DATABASE_CURSORCLASS'] = 'DictCursor' but that doesn't work. It instead works with flask-mysqldb from documentation here.

Also am confused about flask-mysql and flask-mysqldb libraries, which one should I be using.

David Okwii
  • 7,352
  • 2
  • 36
  • 29
  • In your code i am seeing: `from flaskext.mysql import MySQL` while the example you are linking to states: `from flask.ext.mysqldb import MySQL` – Snuggert Apr 18 '16 at 09:38
  • flask-mysql uses flaskext.mysql import MySQL well as flask-mysqldb uses from flask.ext.mysqldb import MySQL, which is another point of confusion am seeking clarification – David Okwii Apr 25 '16 at 07:52
  • Ok, i did a bit of research but you should be referring to the new MySQL cursor dict classes, seen [here](https://dev.mysql.com/doc/connector-python/en/connector-python-api-cursor-subclasses.html). – Snuggert Apr 25 '16 at 10:14

1 Answers1

0

i found other way to config the flask server in flask-mysql(don't confuse with flask-mysqldb), you can do this:

from flaskext.mysql import MySQL
import pymysql

mysql = MySQL()
mysql = MySQL(app, prefix="mydb", host="localhost",password='password', user="user", db="mydb", cursorclass=pymysql.cursors.DictCursor)