Python sqlalchemy.engine.base.engine COMMIT在运行seed程序后显示在终端中,但在查询表时不显示数据
我已经编写了一个种子程序,使用SQLAlchemy模式将数据种子植入PostgreSQL。我创建了一个“课程”表,其中包含id、组织、标题、作者、开始日期、结束日期、持续时间和日程安排注释 当我运行程序时,我会看到insert命令和插入的信息,然后是COMMIT语句。然而,当我打开PostgreSQL并运行“select*from courses”时,我什么也得不到。桌子是空的 以下是我运行程序时在终端中看到的内容: 2015-02-18 16:28:17353信息sqlalchemy.engine.base.engine 2015-02-18 16:28:17353信息sqlalchemy.engine.base.engine插入课程>组织、标题、作者、开始日期、结束日期、持续时间、日程注释值%组织、%标题、%作者、%开始日期、%结束日期、%持续时间、%日程注释返回课程.id 2015-02-18 16:28:17353信息sqlalchemy.engine.base.engine{'schedule_notes':'没有开放的会议。\n','start_date':无,'duration':'None','title':'有摩擦的市场','authors':['Randall','Wright','end_date':无,'organization':'University of Wisconsin\xe2\x80\x93Madison'} 2015-02-18 16:28:17354信息sqlalchemy.engine.base.engine提交 这是我的种子.py: 导入模型Python sqlalchemy.engine.base.engine COMMIT在运行seed程序后显示在终端中,但在查询表时不显示数据,python,postgresql,sqlalchemy,command-line-interface,Python,Postgresql,Sqlalchemy,Command Line Interface,我已经编写了一个种子程序,使用SQLAlchemy模式将数据种子植入PostgreSQL。我创建了一个“课程”表,其中包含id、组织、标题、作者、开始日期、结束日期、持续时间和日程安排注释 当我运行程序时,我会看到insert命令和插入的信息,然后是COMMIT语句。然而,当我打开PostgreSQL并运行“select*from courses”时,我什么也得不到。桌子是空的 以下是我运行程序时在终端中看到的内容: 2015-02-18 16:28:17353信息sqlalchemy.engi
import datetime
def load_courses(session):
# use u.user
#reads in file and parses data
users_table = open("courses.txt", "r")
for line in users_table:
aline = line.split('\t')
organization, title, authors, start_date, duration, schedule_notes = aline
#creates instance of user
course = model.Course()
course.title = title
course.organization = organization
author_list = authors.split()
course.authors = author_list
starting = start_date.split()
try:
month, day, year = starting
day = day.replace(',', '').replace('st', '').replace('th', '').replace('nd', '').replace('rd', '')
months = {"Jan":1,"Feb":2, "Mar":3,"Apr":4,"May":5,"Jun":6,"Jul":7, "Aug":8, "Sep":9, "Oct":10,"Nov":11,"Dec":12}
date = datetime.date(int(year), months[month], int(day))
course.start_date = date
converted_duration = duration.replace('st', '').replace(' weeks long', '').replace('th', '').replace('nd', '').replace('rd', '')
lasting = datetime.timedelta(days=((int(converted_duration))*7))
course.end_date = lasting + date
except ValueError:
course.start_date = None
course.end_date = None
try:
course.duration = duration
except ValueError:
course.duration = None
course.schedule_notes = schedule_notes
print(type(course.end_date))
# adds course to session
session.add(course)
#commits session changes
session.commit()
def main(session):
# You'll call each of the load_* functions with the session as an argument
load_courses(session)
if __name__ == "__main__":
s= model.connect()
main(s)
这是my model.py的内容:
from sqlalchemy import create_engine, Column, Integer, String, Date
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.engine.url import URL
# from sqlalchemy.dialects import postgresql
from sqlalchemy.orm import sessionmaker, scoped_session
import settings
DeclarativeBase = declarative_base()
ENGINE = create_engine(URL(**settings.DATABASE), echo=True)
session = scoped_session(sessionmaker(bind=ENGINE,
autocommit = False,
autoflush = False))
def connect():
global ENGINE
global Session
ENGINE = create_engine(('postgresql:///courses'), echo=True)
Session = sessionmaker(bind=ENGINE)
return Session()
def create_courses_table(engine):
""""""
DeclarativeBase.metadata.create_all(engine)
class Course(DeclarativeBase):
"""Sqlalchemy courses model"""
__tablename__ = "courses"
id = Column(Integer, primary_key=True)
organization = Column('organization', String)
title = Column('title', String)
authors = Column('authors', String)
start_date = Column('start_date', Date(timezone=False), nullable=True)
end_date = Column('end_date', Date(timezone=False), nullable=True)
duration = Column('duration', Integer, nullable=True)
schedule_notes = Column('schedule_notes', Integer, nullable=True)
我已经搜索了堆栈溢出,但找不到表存在的任何情况,也没有错误消息,但结果仍然为空
任何帮助都将不胜感激。希望我犯了一个简单而愚蠢的错误。我了解到,为了使数据种子化到表中,我需要手动将一些值插入到表列中 一旦我添加了一行,我就可以使用我的脚本为其余的数据播种 愚蠢的错误