使用Python将Excel表导入SQL数据库
我有由第 3 方程序创建的 SQL 数据库,我正在使用 python 将一些数据从 Excel 表导入到 SQL 数据库。这里是数据库和excel表格的预览;
sql 和 excel 列的名称完全匹配。这是我用来导入的代码;
import pandas as pd
import sqlite3
#Paths
excel_path="C:/users/user/desktop/ACC_Import.xlsx"
sql_db_path="c:/users/P6_BD_DataBase_001"
#Defs
df=pd.read_excel(excel_path, dtype={"ACCT_SHORT_NAME":object}) #this dtype important, pandas turns to int which we don't want to ...
conn=sqlite3.connect(sql_db_path)
cur=conn.cursor()
def insert_excel_to_sql(df):
for row in df.itertuples():
data_tuple=(row.ACCT_ID,
row.PARENT_ACCT_ID,
row.ACCT_SEQ_NUM,
row.ACCT_NAME,
row.ACCT_SHORT_NAME,
row.ACCT_DESCR,
row.CREATE_DATE,
row.CREATE_USER,
row.UPDATE_DATE,
row.UPDATE_USER,
row.DELETE_SESSION_ID,
row.DELETE_DATE)
sqlite_insert_with_param='''
INSERT INTO ACCOUNT (ACCT_ID,PARENT_ACCT_ID,ACCT_SEQ_NUM,ACCT_NAME,
ACCT_SHORT_NAME,ACCT_DESCR,CREATE_DATE,CREATE_USER,
UPDATE_DATE,UPDATE_USER,DELETE_SESSION_ID,DELETE_DATE)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?);
'''
cur.execute(sqlite_insert_with_param,data_tuple)
conn.commit()
我仍然一一输入所有列的名称,我确信它们是完全相同的。
有没有其他方法可以通过不一一输入所有列名来将 excel 表(sql 和 excel 列名完全相同)导入到 sql 中?
I have SQL database created by 3rd party program and I am importing some datas from excel table to sql db with python. Here is the previews of database and excel table;
As you see sql and excel columns' name are matched exactly. and here is code I use to import;
import pandas as pd
import sqlite3
#Paths
excel_path="C:/users/user/desktop/ACC_Import.xlsx"
sql_db_path="c:/users/P6_BD_DataBase_001"
#Defs
df=pd.read_excel(excel_path, dtype={"ACCT_SHORT_NAME":object}) #this dtype important, pandas turns to int which we don't want to ...
conn=sqlite3.connect(sql_db_path)
cur=conn.cursor()
def insert_excel_to_sql(df):
for row in df.itertuples():
data_tuple=(row.ACCT_ID,
row.PARENT_ACCT_ID,
row.ACCT_SEQ_NUM,
row.ACCT_NAME,
row.ACCT_SHORT_NAME,
row.ACCT_DESCR,
row.CREATE_DATE,
row.CREATE_USER,
row.UPDATE_DATE,
row.UPDATE_USER,
row.DELETE_SESSION_ID,
row.DELETE_DATE)
sqlite_insert_with_param='''
INSERT INTO ACCOUNT (ACCT_ID,PARENT_ACCT_ID,ACCT_SEQ_NUM,ACCT_NAME,
ACCT_SHORT_NAME,ACCT_DESCR,CREATE_DATE,CREATE_USER,
UPDATE_DATE,UPDATE_USER,DELETE_SESSION_ID,DELETE_DATE)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?);
'''
cur.execute(sqlite_insert_with_param,data_tuple)
conn.commit()
I still type all columns' names one by one which I am sure that they are exactly the same.
Is there any other way importing excel table (sql and excel column names are exactly same) to sql by NOT typing all column names one by one ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 sqlite INSERT 文档:
因此,不必在 INSERT 语句中键入所有列名。
From the sqlite INSERT doc:
Therefore it is not necessary to type all the column names in the INSERT statement.
首先,@DinoCoderSaurus 是正确的,没有必要在 INSERT 语句中键入列名。对于 df 列名称,我使用此路径
Reading excel table with dtypes as an object
随着读取“df dtype=object”返回 row_tuple 项目的类型“int,str,float”,因此,我能够将Excel数据导入SQL数据库。图片如下;
如果我不使用 dtype=object 读取 df;
row_tuple 项的 dtypes 返回“numpy.int64、numpy.float64”,这会在将 excel 表导入到 sql db 时导致“数据不匹配错误”。图片如下;
First, @DinoCoderSaurus is right, not necessary to type column names in INSERT statement. For df column names, I used this path
Reading excel table with dtypes as an object
With reading "df dtype=object" returns row_tuple items' type "int,str,float" so, I was able to import excel datas to SQL database. image as below;
If I don't read df with dtype=object;
row_tuple items' dtypes return "numpy.int64, numpy.float64" which causes "data mismatch error" when importing excel table to sql db. Image as below ;