gpt4 book ai didi

python - 从 Access 创建 sqlite 数据库

转载 作者:太空宇宙 更新时间:2023-11-03 21:40:51 25 4
gpt4 key购买 nike

我想创建一个来自 access 数据库后端的 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 似乎是一个很好的解决方案,但是是否可以格式化现有数据库?

问候因科

最佳答案

@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 关系?

答案:

关系已充分定义!实例对象(inst)已经包含id_book和id_article:

for inst in session.query(tob).order_by(tob.ID):
print(inst.id_book.Title, inst.id_article.Title)

关于python - 从 Access 创建 sqlite 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52869525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com