【发布时间】:2021-11-18 07:51:17
【问题描述】:
我希望有人可以帮助我,我有一个使用 Python 和 SQlite 完成大学课程的任务。
我有两张 Excel 表格,我想将它们分成两张表格,并在一些财务信息上进行左连接。我已经开始将两张表导入熊猫,我认为是创建两张表,但老实说,我不知道我哪里出错了。电子表格包含的列比我想在 SQL 数据库中使用的多得多。这会是个问题吗?
我最终收到错误代码“OperationalError: table Lockedlist has no column named Site Ref”
import pandas as pd
Lockedlist_panda = pd.read_excel (r'Location of Lockedlist_panda')
PO1report_panda = pd.read_excel (r'Location of PO1report_panda')
import sqlite3
conn = sqlite3.connect("data_superstore1.db")
cursor = conn.cursor()
sql='''CREATE TABLE Lockedlist (
NR TEXT,
Programme TEXT NOT NULL,
Sub_Region TEXT NOT NULL,
Site_Type TEXT NOT NULL,
MS13_Actual TEXT NOT NULL,
PRIMARY KEY(NR)
)'''
cursor.execute(sql)
print("Table created successfully........")
#Commit your changes in the database
conn.commit()
#Closing the connection
conn.close()
conn = sqlite3.connect("data_superstore1.db")
cursor = conn.cursor()
sql='''CREATE TABLE PoReportTable (
Tracking_Field TEXT,
VENDOR TEXT NOT NULL,
Short_Text TEXT NOT NULL,
COST INT NOT NULL,
PRIMARY KEY(Tracking_Field)
)'''
cursor.execute(sql)
print("Table created successfully........")
#Commit your changes in the database
conn.commit()
#Closing the connection
conn.close()
conn = sqlite3.connect("data_superstore1.db")
Lockedlist_panda.to_sql('Lockedlist', conn, if_exists='append', index=False)
PO1report_panda.to_sql('PoReportTable', conn, if_exists='append', index=False)
#Commit your changes in the database
conn.commit()
#Closing the connection
conn.close()
【问题讨论】: