PyMySQL - 通过fetchall in从字典中获取列数据

时间:2018-03-01 20:34:22

标签: python-3.x pymysql fetchall

使用PyMySQL python 3.6.3版本,获取DictCursor,然后获取fetchall()。我得到了所有数据,.description说: (('recdate', 12, None, 19, 19, 0, False), ('outdoorhumidity', 246, None, 9, 9, 3, True), ('outdoortemperature', 246, None, 9, 9, 3, True))

打印我得到的行,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

注意缺失的一分钟... 23:14:00 - 但我这样做是为了更大的缺失数据插补项目。所以这是丢失数据周围的示例数据。通过字典我想得到不完整的时间序列以及f ex第3列,以最好的方式 - 简单易读的代码?在每种情况下,我是否必须知道有多少行?

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()

1 个答案:

答案 0 :(得分:0)

我想要实现的东西更容易使用熊猫,sqlalchemy,日期时间和时间。并且还围绕不同的方式制作了关于引用Pandas DataFrames的评论。

有些事情对我的情况有点特别,但对其他人来说可能很有趣,尤其是使用.iloc和.loc,并输入转换处理时间。

# Python version
# '3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 
       bit (AMD64)]'
# Running Spyder IDE version 3.2.6
# PANDAS VERSION '0.22.0'enter code here
import pandas as pd
import time
from datetime import datetime
from sqlalchemy import create_engine



def GetSqlData(begdatetime,enddatetime):
    temp_df = pd.read_sql_query("""SELECT recdate, outdoortemperature
                   FROM osterasen 
                   WHERE recdate BETWEEN 
                   %(c1)s
                   AND
                   %(c2)s """,
                   engine,
                   params={'c1': begdatetime, 'c2': enddatetime}
                   )
    temp_df['recdate'] = temp_df['recdate'].astype('datetime64[ns]')
    return temp_df

iso_datetimeformat = "%Y-%m-%d %H:%M:%S"

# BEGIN - CREATE MOCK MISSING DATES
# would have to come from the WeatherData.missings table
print("Step 0.0: ")
print("     Get missing data info from weatherdata.missings...")

num_of_missings=21
range_good_m = num_of_missings * 2
print(range_good_m)
range_good_s = range_good_m * 60
print(range_good_s)
a_complete_end_str = '2005-09-24 09:09:00'

b_complete_beg_str = '2005-09-24 09:31:00'

a_complete_end = time.mktime(datetime.strptime(a_complete_end_str, 
    iso_datetimeformat ).timetuple())
print("a_complete_end: ", a_complete_end)

b_complete_beg = time.mktime(datetime.strptime(b_complete_beg_str, i 
iso_datetimeformat ).timetuple())
print("b_complete_beg: ", b_complete_beg)

a_complete_beg = a_complete_end - range_good_s 
print("a_complete_beg: ", a_complete_beg)

b_complete_end = b_complete_beg + range_good_s
print("b_complete_end: ", b_complete_end)
print("b_complete_end: type: ", type(b_complete_end))

m_missing_beg = a_complete_end + 60
m_missing_end = b_complete_beg - 60
m_missing_beg_str = str(datetime.fromtimestamp(m_missing_beg))
m_missing_end_str = str(datetime.fromtimestamp(m_missing_end))

a_complete_beg_str = str(datetime.fromtimestamp(a_complete_beg ))
b_complete_end_str = str(datetime.fromtimestamp(b_complete_end ))

# Print out the ranges
print(a_complete_beg_str)
print(a_complete_end_str)
print(m_missing_beg_str)
print(m_missing_end_str)
print(b_complete_beg_str)
print(b_complete_end_str)

# END   - CREATE MOCK MISSING DATES

connection_str = 
    'mysql+pymysql://root:mypassword@127.0.0.1:3306/weatherdata'

engine = create_engine(connection_str,encoding='utf8')




print("Step 1.0: ")
df_a = GetSqlData( a_complete_beg_str, a_complete_end_str )
print(type(df_a),"   ", type(df_a.head))
print(df_a)
x1 = df_a.loc[5, 'outdoortemperature']
y1 = df_a.loc[5, 'recdate']
print("x1 = ", x1, type(x1))
print("y1 = ", y1, type(y1))



print("Step 2.0: ")
df_b = GetSqlData( b_complete_beg_str, b_complete_end_str )
print(type(df_b),"   ", type(df_b.head))
print(df_b)
x2 = df_b.loc[5, 'outdoortemperature']
y2 = df_b.loc[5, 'recdate']
print("x2 = ", x2, type(x2))
print("y2 = ", y2, type(y2))