python 将excel导入数据库
功能如下:
将excel中每个sheet自动导入数据库,以sheet名称为表名在数据库中创建表;
将每个sheet中的数据添加至数据库对应表中;默认以第一行为该表的列表名,其它行作为数据;
查找数据中存在的表文件名;
查找数据库中某表的说有数据;
向数据库某表中添加数据;
自适应创建表;
小结:
使用python sqlite创建表,查看表数量及名称,查看表的列表名称,查看表的内容,表数据插入;
代码如下:
# This is a sample Python script.
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import xlrd
import sqlite3
import pprint
# 连接数据库
def connect_db(file_path):
conn = sqlite3.connect(file_path)
return conn
# 获取数据库中所有表的名字
def get_tables(conn):
sql = "SELECT * FROM sys.Tables"
cursor = conn.cursor()
# 获取表名
cu