from sqlalchemy import *
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
engine = create_engine('sqlite:///database.db', echo=True)
Base = declarative_base()
########################################################################
class User(Base):
""""""
__tablename__ = "users"
id = Column(Integer, primary_key=True)
username = Column(String)
password = Column(String)
#----------------------------------------------------------------------
def __init__(self, username, password):
""""""
self.username = username
self.password = password
# create tables
Base.metadata.create_all(engine)
这应该创建一个文件名为database.db的db结构,但是弹出错误,我该如何解决呢?任何帮助,将不胜感激。
这是一个缩进问题。解决了它。解:
from sqlalchemy import *
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
engine = create_engine('sqlite:///tutorial.db', echo=True)
Base = declarative_base()
########################################################################
class User(Base):
""""""
__tablename__ = "users"
id = Column(Integer, primary_key=True)
username = Column(String)
password = Column(String)
#----------------------------------------------------------------------
def __init__(self, username, password):
""""""
self.username = username
self.password = password
# create tables
Base.metadata.create_all(engine)
在我的情况下,我收到此错误,因为我写的像
Column('id', Integer)
Column('username', String)
VS
id = Column('id', Integer)
username = Column('username', String)