开发者

SQLAlchemy and me

开发者 https://www.devze.com 2023-02-18 07:30 出处:网络
I love OOP and immediately I learned about SQLAlchemy is was very interested in. I am new in it though and having issues one not really an issue but somewhat annoying for a beginner is having to impor

I love OOP and immediately I learned about SQLAlchemy is was very interested in. I am new in it though and having issues one not really an issue but somewhat annoying for a beginner is having to import each module explicitly even after importing the master module, I mean sqlalchemy.

I have this script to create a new database file and to alert m开发者_运维知识库e if a database of the same name already exist but I am getting it wrong unlike working straight on sqlite3.

import os
import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

def CreateDB(dbName=None):
    dbName = 'ABC-DB' # should be the name of the new database file
    db = create_engine('sqlite:///dbName', echo=False) # should be a link from above
    Base.metadata.create_all(db) 
    if os.path.isfile(dbName): # should check for existence
        print('DataBase already exist') # should alert on existence.

I get 'dbName' instead of 'ABC-DB'. The os.path... is a dead script as it does nothing.

Someone please help.


Correct, it did exactly what you told it to:

db = create_engine('sqlite:///dbName', echo=False) # should be a link from above

You want:

db = create_engine('sqlite:///' + dbName, echo=False) # should be a link from above
0

精彩评论

暂无评论...
验证码 换一张
取 消