本文介绍了sqlalchemy,如果不存在则创建一个sqlite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在试用 sqlalchemy,我正在使用此连接字符串连接到我的数据库
I am trying out sqlalchemy and i am using this connection string to connect to my databases
engine = create_engine('sqlite:///C:\\sqlitedbs\\database.db')
如果应该获取数据库文件的目录中不存在sqlalchemy 数据库,sqlalchemy 是否会为您创建一个sqlite 数据库?.
Does sqlalchemy create an sqlite database for you if one is not already present in a directory it was supposed to fetch the database file?.
推荐答案
是的,sqlalchemy 确实为您创建了一个数据库.我使用此代码在 Windows 上确认了它
Yes,sqlalchemy does create a database for you.I confirmed it on windows using this code
from sqlalchemy import create_engine, ForeignKey
from sqlalchemy import Column, Date, Integer, String
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('sqlite:///C:\\sqlitedbs\\school.db', echo=True)
Base = declarative_base()
class School(Base):
__tablename__ = "woot"
id = Column(Integer, primary_key=True)
name = Column(String)
def __init__(self, name):
self.name = name
Base.metadata.create_all(engine)
这篇关于sqlalchemy,如果不存在则创建一个sqlite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!