提问者:小点点

向sqlite db python pandas添加列


我有两个独立的脚本在linux CRON服务上以不同的时间间隔运行。 在这两个脚本上运行的代码非常相似,我试图做的是将数据从pandas保存到相同的sqlitedb中。 我认为现在发生的情况是CRON服务首先运行一个文件,创建db,然后当第二个。py文件从CRON服务运行时,就没有创建一列来保存数据。

脚本1:(运行一次,每小时)

weatherStuff['Date'] = datetime.fromtimestamp(weatherStuff['Date']) #dictionary, code not shown how its created
master_data = pd.DataFrame(weatherStuff,index=[0])
print("DataFrame to sql",master_data)
engine = create_engine('sqlite:///save_pandas_pi.db', echo=True)
sqlite_connection = engine.connect()
sqlite_table = "OutsideConds_MeterReadings"
master_data.to_sql(sqlite_table, sqlite_connection, if_exists='append')
sqlite_connection.close()
print("Data saved to sql!")

脚本2:(每15分钟运行一次)

stuff['Date'] = datetime.fromtimestamp(stuff['Date']) #dictionary, code not shown how its created
master_data = pd.DataFrame(stuff,index=[0])
print("DataFrame to sql",master_data)
engine = create_engine('sqlite:///save_pandas_pi.db', echo=True)
sqlite_connection = engine.connect()
sqlite_table = "OutsideConds_MeterReadings"
master_data.to_sql(sqlite_table, sqlite_connection, if_exists='append')
sqlite_connection.close()
print("Data saved to sql!")

做一些测试如果没有创建db,weatherscript运行良好创建db&; 保存数据。 但是当我运行第二个脚本时,这个错误就出现了。 非常感谢任何提示。。。

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) table OutsideConds_MeterReadings has no column named MeterReading
[SQL: INSERT INTO "OutsideConds_MeterReadings" ("index", "Date", "MeterReading") VALUES (?, ?, ?)]
[parameters: (0, '2020-06-22 09:46:17.460552', 71.56999206542969)]

共1个答案

匿名用户

您的第一个脚本似乎没有在表上创建MeterReading列,因为我们没有看到dict,我们无法确定,但可能是轻微的打字错误或其他差异导致了问题。