Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python sqlalchemy create_engine()如果数据库已经存在_Python_Python 2.7_Sqlalchemy - Fatal编程技术网

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

Python sqlalchemy create_engine()如果数据库已经存在,python,python-2.7,sqlalchemy,Python,Python 2.7,Sqlalchemy,如果我通过重新启动程序重复调用最后四条python语句,会对数据库的正确运行产生负面影响吗。?如果数据库已经存在,它会再次创建数据库吗?正如sberry所写的那样-调用创建引擎并通过重新运行相同的脚本多次创建会话将打开连接并参照此连接创建SQLAlchemy引擎对象。 因此,这样做不会创建新的sqlite数据库文件,也不会影响数据库功能 此外,我建议确保代码始终在脚本末尾执行session.close()。这将确保所有更改(若有)都将提交到数据库。 我所说的更改是指脚本可能对数据库进行的任何更新

如果我通过重新启动程序重复调用最后四条python语句,会对数据库的正确运行产生负面影响吗。?如果数据库已经存在,它会再次创建数据库吗?

正如sberry所写的那样-调用
创建引擎
并通过重新运行相同的脚本多次创建会话将打开连接并参照此连接创建SQLAlchemy引擎对象。 因此,这样做不会创建新的sqlite数据库文件,也不会影响数据库功能

此外,我建议确保代码始终在脚本末尾执行session.close()。这将确保所有更改(若有)都将提交到数据库。
我所说的更改是指脚本可能对数据库进行的任何更新/插入。

这不是创建数据库,而是连接到数据库。
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()