sqlalchemy - 使用session.commit()在数据库中不存在数据

时间:2015-03-16 16:55:15

标签: python postgresql sqlalchemy

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

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

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

python PostGreSQLTest.py -j JSONFile.json
1012

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

紧接着之后,我在没有JSON文件的情况下调用脚本,并尝试从数据库中获取行数,它显示0行(作为脚本输出和GUI内的输出。)

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)

1 个答案:

答案 0 :(得分:0)

  

数据在会话之间不会持久存储在数据库中,

呃......你是说你的脚本单独调用之间没有数据?放弃所有表格时会出现这种情况:

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

摆脱那条线。如果没有&#34;放弃一切,它应该做什么!&#34;