简体   繁体   English

sqlalchemy create_engine()(如果数据库已经存在)

[英]sqlalchemy create_engine() if the db already exists

from pox.core import core
import pox.openflow.libopenflow_01 as of
import re
import datetime

from sqlalchemy import create_engine, ForeignKey
from sqlalchemy import Column, Date, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, backref
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker


log = core.getLogger()



engine = create_engine('sqlite:///nwtopology.db', echo=False)
Base = declarative_base()
Session = sessionmaker(bind=engine)
session = Session()

if I create call the last four python statements repeateadly by restarting the program will it have a negative impact on the correct functioning of the database.? 如果我通过重新启动程序来重复调用最后四个python语句,将会对数据库的正常运行产生负面影响。 Will it create the database again if one already exists? 如果已经存在,它将重新创建数据库吗?

As sberry wrote - calling create_engine and creating session multiple times by rerunning same script will just open connection and create SQLAlchemy engine object with reference to this connection. 如sberry所写-调用create_engine并通过重新运行同一脚本多次创建会话将仅打开连接并参考该连接创建SQLAlchemy引擎对象。 Thus doing so won't create new sqlite database file and won't impact the database functioning. 因此,这样做不会创建新的sqlite数据库文件,也不会影响数据库的功能。

Also I would suggest to make sure that your code always do session.close() at the end of your script. 我也建议确保您的代码始终在脚本末尾执行session.close()。 This would ensure that all changes if there were any will be committed to database. 这样可以确保所有更改(如果有的话)都将提交到数据库。 By changes I mean any updates/inserts your script may do to database. 所谓更改,是指您的脚本可以对数据库进行的任何更新/插入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM