奇怪的错误,插入到数据库的数据读出来不对,问题在哪儿?
小白一枚,请高手指教;
def get_last_date(conn):ret=''
try:
cursor = conn.cursor()
query = '''select date from lastdate limit 1;'''
cursor.execute(query)
conn.commit()
count_wm = cursor.fetchall()
ret= count_wm[0][0]
print(count_wm,ret)
except Exception as e:
print(e)
'''
try:
# 查询语句,选出employee表中的所有数据
sql = 'select * from lastdate limit 1;'
# read_sql_query的两个参数: sql语句, 数据库连接
df = pd.read_sql_query(sql, engine)
# 输出employee表的查询结果
ret=df['lastdate'].values[0]
except Exception as e:
print(e)
'''
return ret
def set_last_date(conn):cursor = conn.cursor()
cursor.execute("DROP TABLE IF EXISTS lastdate")####
sql = 'create table if not exists lastdate(date varchar(12) not null,temp int,primary key(`date`))'
cursor.execute(sql)
conn.commit()
dt=datetime.datetime.now().date().strftime("%Y-%m-%d")
sql = '''insert into lastdate (date,temp) values({},0);'''.format(dt)
print(sql)
print('hhh')
try:
cursor.execute(sql)
conn.commit()
except Exception as e:
print(e)
conn.rollback()
print('插入失败')
#finally:
# 关闭连接
#conn.close()
set_last_date(conn)
lastdt=get_last_date(conn)
print(type(lastdt),lastdt)#str
显示
insert into lastdate (date,temp) values(2020-09-24,0);
hhh
(('1987',),) 1987
1987