我想从访问数据库后端创建一个sqlite数据库。
对于64位/ 32位-> pyocdb不起作用。所以我导出了一些excel文件。
一个快速的解决方案是:
import os
import pandas as pd
from sqlalchemy import create_engine
#load all files in folder
folder = "...some start folder"
files = {file.split('.')[0]:os.path.join(folder, file)
for file in os.listdir(folder) if file.endswith('.xlsx')}
list_dfs = {name:pd.read_excel(file) for name,file in files.items()}
#initialize a sqlite database
engine = create_engine('sqlite:///sql.db', echo=False)
#drop tables to sql
for key, frame in list_dfs.items():
frame.to_sql(key, con=engine, if_exists='append',index=False,index_label='ID')
我可以在字典中的frame.to_sql
中添加一些dType。
我努力建立表之间的关系。
sqlalchemy似乎是一个很好的解决方案,但是可以格式化现有数据库吗?
致谢 inco
答案 0 :(得分:0)
@PowerStat,感谢您的纠正。
到目前为止,我当前的解决方案似乎仍然有效:
import os
import pandas as pd
from sqlalchemy import (create_engine, MetaData, Table, Column, Integer,
String, ForeignKey, DateTime, Float)
#%%load all excel filenames into two directories main/id
folder = "some_path"
ID_files = {file.split('.')[0]:os.path.join(folder, file)
for file in os.listdir(folder) if "ID_" in str(file) and file.endswith('.xlsx')}
main_files={file.split('.')[0]:os.path.join(folder, file)
for file in os.listdir(folder) if not "ID_" in str(file) and file.endswith('.xlsx')}
#create a list of dataframes
ID_dfs = {name:pd.read_excel(file) for name,file in ID_files.items()}
main_dfs = {name:pd.read_excel(file) for name,file in main_files.items()}
#%%initialize meta data for all tables
engine = create_engine('sqlite:///sql.db', echo=False)
meta = MetaData()
for key, frame in ID_dfs.items():
table = Table(key, meta,
Column('ID',Integer,primary_key = True),
Column('Title',String, unique=True))
table = Table('table_of_things', meta,
Column('ID',Integer, primary_key = True),
Column('Book_ID',Integer,ForeignKey('ID_Book.ID')),
Column('Article_ID',Integer,ForeignKey('ID_Article.ID')))
meta.create_all(engine)
#add the Dataframes to sql-database
for key, frame in ID_dfs.items():
frame.to_sql(key, engine, if_exists='append',index=False, index_label='ID')
main_dfs['table_of_things'].to_sql('table_of_things', engine,
if_exists='append',index=False,index_label='ID')
使用第二个脚本,我加载数据库并进行查询:
from sqlalchemy.ext.automap import automap_base, generate_relationship
from sqlalchemy.orm import Session
from sqlalchemy import create_engine
def _gen_relationship(base, direction, return_fn,
attrname, local_cls, refferred_cls, **kw):
return generate_relationship(base, direction, return_fn, attrname, local_cls, refferred_cls, **kw)
Base = automap_base()
# engine, suppose it has two tables 'user' and 'address' set up
engine = create_engine("sqlite:///sql.db")
# reflect the tables
Base.prepare(engine, reflect=True, generate_relationship=_gen_relationship)
#table to classvariable
tob = Base.classes['table_of_things']
session = Session(engine)
for inst in session.query(tob).order_by(tob.ID):
print(inst.Book_ID, inst.Article_ID)
输出给我ID值但不给标题值,如何正确使用one2many关系?
答案:
关系已充分定义! 实例对象(实例)已经包含id_book和id_article:
for inst in session.query(tob).order_by(tob.ID):
print(inst.id_book.Title, inst.id_article.Title)