我无法将数据添加到数据库SQLAlchemy Python

时间:2016-06-13 07:27:27

标签: python database orm sqlalchemy

from sqlalchemy import Column, String, Integer, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
Base = declarative_base()

engine = create_engine('sqlite:///C://Users//Can-PC//Desktop//MuhasebePython//MuhasebePython//deneme.sqlite')
Session = sessionmaker(bind=engine)
session = Session()
class User(Base):

__tablename__ = 'user'
userid = Column(Integer, primary_key=True, autoincrement=True,unique=True)
username = Column(String, unique=True)
password = Column(String, unique=True)
Base.metadata.create_all(engine)

def create_user(self):
    john = User(username='John', password="deneme")
    # Add User john to the Session object
    session.add(john)
    # Commit the new User John to the database
    session.commit()

有我的代码。这段代码运行无例外但我仍然无法将数据添加到我的数据库中。我不知道为什么。请帮助我。

0 个答案:

没有答案