Using PyMySQL python 3.6.3 versions, getting an DictCursor
, and then fetchall()
. I get all the data and .description
says:
(('recdate', 12, None, 19, 19, 0, False), ('outdoorhumidity', 246, None, 9, 9, 3, True), ('outdoortemperature', 246, None, 9, 9, 3, True))
.
Printing the rows I get, f.ex:
2005-12-31 23:12:00 89.000 -6.667
2005-12-31 23:13:00 89.000 -6.667
2005-12-31 23:15:00 89.000 -6.650
2005-12-31 23:16:00 89.000 -6.650
2005-12-31 23:17:00 89.000 -6.640
Note the missing minute ...23:14:00 - but I do this for a bigger missing data imputations project. So this is sample data around the missing data. Via the dictionary I want to get the incomplete time series as well as f ex 3rd column, in the best way -simple easy readable code? Do I in each case have to know how many rows there are?
import pymysql
dbServerName = "127.0.0.1"
dbUser = "root"
dbPassword = "mypwd"
dbName = "dbname"
charSet = "utf8"
cursorType = pymysql.cursors.DictCursor
connectionObject = pymysql.connect(host=dbServerName, user=dbUser, password=dbPassword,
db=dbName, charset=charSet,cursorclass=cursorType)
try:
cursorObject = connectionObject.cursor()
sqlQuery = "SELECT recdate, outdoorhumidity, outdoortemperature FROM mytable WHERE recdate BETWEEN '2005-12-31 23:12:00' AND '2005-12-31 23:17:00';"
cursorObject.execute(sqlQuery)
#Fetch all the rows - within the cursor? Can this be done?
rows = cursorObject.fetchall()
print(cursorObject.description)
for row in rows:
print(row["recdate"], row["outdoorhumidity"], row["outdoortemperature"])
except Exception as e:
print("Exeception occured:{}".format(e))
finally:
cursorObject.close()
connectionObject.close()