I would like to get a dictionary object as the result of an "execute("insert...") " SQL command in Python 2.7 using Debain Stretch on a Raspberry Pi V3. The normal object is a tuple so elements are accessed via an integer index. From the web it would appear that two libraries will supply that discionary functionality: mysqldb and pymysql. When I use "import mysqldb" I get an error "ImportError: No module named mysqldb". If I use "import pymysql" I don't get any errors but the result is a tuple and not a dictionary, which is also what I would get it I just take the default cursor. If I modify my code to specify the column name, ie
print("row ", mycursor.rowcount, "id ", x['id'], " date ", x['date_time'])
Then I get an error about using a non-interger index. Here is example code:
#!/usr/bin/env python
import sys
import pymysql
import mysql.connector
db = mysql.connector.connect(
host="localhost",
user="xxx",
passwd="yyy",
database="zzz")
print(db)
mycursor = db.cursor(pymysql.cursors.DictCursor)
# mycursor = db.cursor()
mycursor.execute("select * from table;")
myresult = mycursor.fetchall()
for x in myresult:
print("row ", mycursor.rowcount, "id ", x[0], " date ", x[1])
# if I use "print("row ", mycursor.rowcount, "id ", x["id"], " date ", x[date_Time])" it throws an error
This is a fresh install of Stretch on an Raspberry Pi v3. I have installed the following:
- sudo apt-get install mariadb-server
- sudo apt-get install python-mysqldb
- sudo apt-get -y install python-mysql.connector
- sudo apt-get install libmariadbclient-dev
- sudo pip install PyMySQL
I have search the web for answers, including the two references below
But so far have not found a solution. Can anyone assist?....RDK