使用 Python 将 excel table 导入到 SQL 数据库
Importing excel table to SQL database with Python
我有 SQL 由第 3 方程序创建的数据库,我正在使用 python 从 excel table 导入一些数据到 sql 数据库。这是数据库的预览 excel table;
如您所见,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 table(sql 和 excel 列名称完全相同)导入 sql一一命名 ?
INSERT INTO table VALUES(...);
The first form (with the "VALUES" keyword) creates one or more new rows in an existing table. If the column-name list after table-name is omitted then the number of values inserted into each row must be the same as the number of columns in the table. In this case the result of evaluating the left-most expression from each term of the VALUES list is inserted into the left-most column of each new row, and so forth for each subsequent expression.
因此不必在 INSERT 语句中键入所有列名。
首先,@DinoCoderSaurus 是对的,不需要在 INSERT 语句中键入列名。对于 df 列名,我使用了这个路径
df=pd.read_excel("Acc_Import.xlsx",dtype=object)
阅读 excel table 并将数据类型作为 对象
for index in range(len(df)):
row_tuple=tuple(df.iloc[index].to_list())
cur.execute('''INSERT INTO ACCOUNT VALUES (?,?,?,?,?,?,?,?,?,?,?,?);
''',row_tuple)
通过读取“df dtype=object”returns row_tuple 项的类型“int,str,float”,我能够将 excel 数据导入 SQL 数据库。图片如下;
如果我不使用 dtype=object;
读取 df
row_tuple=tuple(df.iloc[index].to_list())
row_tuple 项目的 dtypes return "numpy.int64, numpy.float64" 在导入 excel table 到 [=40 时导致“数据不匹配错误” =]分贝。图片如下;
我有 SQL 由第 3 方程序创建的数据库,我正在使用 python 从 excel table 导入一些数据到 sql 数据库。这是数据库的预览 excel table;
如您所见,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 table(sql 和 excel 列名称完全相同)导入 sql一一命名 ?
INSERT INTO table VALUES(...);
The first form (with the "VALUES" keyword) creates one or more new rows in an existing table. If the column-name list after table-name is omitted then the number of values inserted into each row must be the same as the number of columns in the table. In this case the result of evaluating the left-most expression from each term of the VALUES list is inserted into the left-most column of each new row, and so forth for each subsequent expression.
因此不必在 INSERT 语句中键入所有列名。
首先,@DinoCoderSaurus 是对的,不需要在 INSERT 语句中键入列名。对于 df 列名,我使用了这个路径
df=pd.read_excel("Acc_Import.xlsx",dtype=object)
阅读 excel table 并将数据类型作为 对象
for index in range(len(df)):
row_tuple=tuple(df.iloc[index].to_list())
cur.execute('''INSERT INTO ACCOUNT VALUES (?,?,?,?,?,?,?,?,?,?,?,?);
''',row_tuple)
通过读取“df dtype=object”returns row_tuple 项的类型“int,str,float”,我能够将 excel 数据导入 SQL 数据库。图片如下;
如果我不使用 dtype=object;
读取 dfrow_tuple=tuple(df.iloc[index].to_list())
row_tuple 项目的 dtypes return "numpy.int64, numpy.float64" 在导入 excel table 到 [=40 时导致“数据不匹配错误” =]分贝。图片如下;