sqlalchemy - 数据不会在 session.commit() 的数据库中持久化

sqlalchemy - Data not persisting in database with session.commit()

我正在使用 PostGreSQL 和 sqlalchemy。我对数据库比较陌生,对 sqlalchemy 也是全新的。我目前有一个简单的数据库,其中包含用于 githash(主键)、提交者和日期的列。

我有一个 JSON 文件一直在收集数据,我的主要目标是将所有数据从文件传输到数据库(所有新数据也将放入数据库).我编写的 python 脚本允许传入 json 文件的参数 (-j) 和文件路径以将其添加到数据库中;但是这不是必需的,因为一旦数据在数据库中,我希望能够在不指定 JSON 文件的情况下从数据库中获取数据。

这就是我的问题所在。当我指定一个JSON文件时,如下:

python PostGreSQLTest.py -j JSONFile.json
1012

添加了 1012 行,然后从数据库中提取,数据库 GUI 也反映了这一点。

紧接着,当我在没有 JSON 文件的情况下调用脚本,并尝试从数据库中获取行数时,它显示 0 行(既是脚本的输出,又是在图形界面。)

python PostGreSQLTest.py
0

即使我确实调用了 session.commit(),数据似乎并没有在会话之间持久保存在数据库中。我已经包含了下面的代码示例。任何对此事的见解将不胜感激。

from argparse import ArgumentParser
import json
import os

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Text, Integer, Date, create_engine, ForeignKey
from sqlalchemy.orm import scoped_session, sessionmaker

Base = declarative_base()
session = scoped_session(sessionmaker())

class TestRecord(Base):
    __tablename__ = 'test_table'
    githash = Column(Text, primary_key=True)
    committer = Column(Text)
    date = Column(Date)

class TestDatabase(object):
    '''
    classdocs
    '''
    def __init__(self):
        '''
        Constructor
        '''
        self.db_name = "test_db"
        self.user = "un"
        self.pw = "pw"
        engine = self.__init_db()

    '''Initializers'''       
    def __init_db(self):
        engine_string = 'postgresql://{0}:{1}@localhost/{2}'.format(self.user, self.pw, self.db_name)
        engine = create_engine(engine_string, echo=False)
        session.remove()
        session.configure(bind=engine, autoflush=False, expire_on_commit=False)
        Base.metadata.drop_all(engine)
        Base.metadata.create_all(engine)
        return engine

    def __grab_json_file(self, filename):
        jsonFile = open(filename, 'rb')
        J = json.load(jsonFile)
        jsonFile.close()
        return J

    '''Helper Functions'''

    '''Main Functions'''
    def add_entries_from_JSON(self, filename):
        '''
        Add Entries From JSON reads in a JSON file and creates records in the database.
        :param filename: <String> Full path to the JSON File
        :return success: <bool> True if records are successfully written, False otherwise
        '''
        TR_entries = []
        success = False
        if os.path.isfile(filename):
            # If the JSON file exists, open it and create records
            entries = self.__grab_json_file(filename)
            for key in entries.keys():
                TR = TestRecord()
                TR.githash = entries[key]["gitHash"]
                TR.date = entries[key]["Date"]
                TR.committer = entries[key]["Committer"]       
                TR_entries.append(TR)
            # Add all records from the list of TestRecord objects to the database
            success = self.add_multiple_entries(TR_entries)
        else:
            print "JSON file '{0}' provided does not exist.".format(filename)
        return success    

    def add_multiple_entries(self, entries):
        '''
        Add multiple entries adds multiple entries to the database.
        :param entries: <list of TestRecord> list of records to add to the database
        :return success: <bool> True if successfully added, false otherwise
        '''
        success = True
        try:
            session.add_all(entries)
            session.commit()
        except:
            success = False
            raise
        return success

    def get_num_rows(self):
        '''
        Get the number of rows in the database.
        :return numRows: the number of rows in the database
        '''
        numRows = session.query(TestRecord).count()
        return numRows


    def cleanup(self):
        '''
        Cleanup wraps everything up.
        '''
        session.close()

def main (jsonFile=None):
    TD = TestDatabase()
    if jsonFile is not None:
        if TD.add_entries_from_JSON(jsonFile) is False:
            print "Failed to add entries from JSON File '{0}'.".format(jsonFile)
    rows = TD.get_num_rows()
    print rows
    TD.cleanup()


def argparser():
    argparser = ArgumentParser( description = 'PostGreSQL Database operations.' )
    argparser.add_argument('-j',
                           '--jsonFile',
                           help='Full file path to a JSON file containing databse entries.',
                           required = False
                           )
    return argparser

if __name__ == "__main__":
    args = argparser().parse_args()
    main(args.jsonFile)

data is not persisting in the database between sessions,

呃...你的意思是你的脚本的不同调用之间没有数据吗?当您删除所有表时,这是可以预期的:

def __init_db(self):
    # ... snip ...
    Base.metadata.drop_all(engine)
    #             ^^^^^^^^
    Base.metadata.create_all(engine)

去掉那一行。如果不是 "drop everything!"

它应该做什么