我正在尝试将数据从excel文件导入mysqlAlchemy数据库,我正在使用一些库,例如openpyxl,pandas,ExcelWriter,ExcelFile 因为这部分读取python中的excel文件
import pandas as pd
xl_file = pd.ExcelFile(r'C:\Users\user 3\Desktop\Tracker Sheet\DT
template(AutoRecovered).xlsx')
dtTemplate = xl_file.parse('Overall RD (Sep-18)')
dtTemplate.head()
print(dtTemplate)
这是应该将数据从excel文件导入sqlAlchemy
的部分import os
import openpyxl
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
engine = create_engine('sqlite:///:memory:', echo=True)
Base = declarative_base()
Session = sessionmaker(bind=engine)
session = Session()
# define the user sql model
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
region = Column(String)
site = Column(String)
status = Column(String)
comment = Column(String)
rdt_date = Column(String)
pp = Column(String)
history = Column(String)
latAndlong = Column(String)
def __repr__(self):
return '<User(Region={}, SITE={}, Status={}, comment={}, RDT date={},
PP={}, History={}, LAT & LONG={}'.format(self.first_name, self.last_name,
self.last_name, self.last_name, self.last_name, self.last_name,
self.last_name, self.last_name)
# create the database in memory
Base.metadata.create_all(engine)
my_excel = r'C:\Users\user 3\Desktop\Tracker Sheet\DT
template(AutoRecovered).xlsx'
sheet_name = 'Overall RD (Sep-18)'
# check to see if the file exists
if not os.path.isfile(my_excel):
raise Exception('File does not exist.')
# open the spreadsheet
wb = openpyxl.load_workbook(my_excel)
# get the sheet
sheet = wb.get_sheet_by_name(sheet_name)
# iterate through the rows of the spreadsheet, starting at the second row
# add the data to a list
excel_contents = []
for row in range(8, sheet.max_row +1):
region = sheet['A'+str(row)].value
site = sheet['B'+str(row)].value
status = sheet['C'+str(row)].value
comment = sheet['D'+str(row)].value
rdt_date = sheet['E'+str(row)].value
pp = sheet['F'+str(row)].value
history = sheet['G'+str(row)].value
latAndlong = sheet['H'+str(row)].value
temp_dict = {'Region': region, 'SITE': site, 'Status': status, 'comment':
comment, 'RDT date': rdt_date, 'PP': pp, 'History': history, 'LAT & LONG':
latAndlong}
excel_contents.append(temp_dict)
# put our excel contents into the database
for u in excel_contents:
user = User(region=u['Region'], site=u['SITE'], status=u['Status'],
comment=u['comment'], rdt_date=u['RDT date'], pp=u['PP'],
history=u['History'], latAndlong=u['LAT & LONG'])
session.add(user)
# commit the changes to the database
session.commit()
# query the database so we know the data is actually there.
query = session.query(User)
for q in query:
print(q.region, q.site, q.status, q.comment, q.rdt_date, q.pp, q.history,
q.latAndlong)
注意:我没有创建数据库,所以……假设此代码创建数据库并创建列并在这些列中填充数据? 还缺少哪些步骤来完成并解决我的案子
这是我尝试运行第二部分时的错误
Traceback (most recent call last):
File "C:\Anaconda\lib\site-
packages\sqlalchemy\dialects\sqlite\pysqlite.py", line 334, in dbapi
from pysqlite2 import dbapi2 as sqlite
ModuleNotFoundError: No module named 'pysqlite2'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:/Users/user 3/Desktop/My Python Refrence/impDataToSQLAlchemy.py",
line 8, in <module>
engine = create_engine('sqlite:///:memory:', echo=True)
File "C:\Anaconda\lib\site-packages\sqlalchemy\engine\__init__.py", line
425, in create_engine
return strategy.create(*args, **kwargs)
File "C:\Anaconda\lib\site-packages\sqlalchemy\engine\strategies.py", line
81, in create
dbapi = dialect_cls.dbapi(**dbapi_args)
File "C:\Anaconda\lib\site-
packages\sqlalchemy\dialects\sqlite\pysqlite.py", line 339, in dbapi
raise e
File "C:\Anaconda\lib\site-
packages\sqlalchemy\dialects\sqlite\pysqlite.py", line 337, in dbapi
from sqlite3 import dbapi2 as sqlite # try 2.5+ stdlib name.
File "C:\Anaconda\lib\sqlite3\__init__.py", line 23, in <module>
from sqlite3.dbapi2 import *
File "C:\Anaconda\lib\sqlite3\dbapi2.py", line 27, in <module>
from _sqlite3 import *
ImportError: DLL load failed: The specified module could not be found.
PS C:\Users\user 3>
答案 0 :(得分:0)
尝试通过pip3安装pysqlite2:
pip3 install psyqlite2
要使用conda安装此软件包,请运行:
conda install -c kbchoi pysqlite