简体   繁体   English

Pandas Dataframe 无法存储在 SQLite3 数据库中

[英]Pandas Dataframe can't store in SQLite3 database

I want to store a Pandas data frame in SQLlite3 database and I wrote this syntax, df.to_excel('January.xlsx',sheet_name='output-1') line is working.我想在 SQLlite3 数据库中存储一个 Pandas 数据框,我编写了这个语法, df.to_excel('January.xlsx',sheet_name='output-1')行正在工作。 but the database is not working.但数据库不工作。 Temp_data.db is my database file. Temp_data.db是我的数据库文件。 its empty after I run my code.在我运行我的代码后它是空的。

    `df=pd.DataFrame(B,index=[dates],columns=['06:00','12:00','18:00','00:00'])
     df.to_excel('January.xlsx',sheet_name='output-1')

    try:
        conn=sqlite3.connect('Temp_data.db')
        c=conn.cursor()
        c.execute('CREATE TABLE TEMP(Date int,06.00 decimal,12.00 decimal,18.00 decimal,00.00 decimal)')
        conn.commit()
        df.to_sql('TEMP',conn,if_exists='replace',index_label='Date')
        c.execute('''
        SELECT * FROM TEMP
        ''')
        for row in c.fetchall():
            print(row)
    except OperationalError:    
        print("Oparational error")

You just have a problem with the name of your columns in your sqlite database: you need to add quotes around the names like that:您只是在 sqlite 数据库中的列名称有问题:您需要在名称周围添加引号:

c.execute("CREATE TABLE TEMP(Date int,'06.00' decimal,'12.00' decimal,'18.00' decimal,'00.00' decimal)")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM