This code used to work, but I think a recent svn up busted it. Now using
Rev 1577
I am using sqlalchemy.mods.threadlocal
I execute the following code multiple times within a single process
invocation.
objectstore is sqlalchemy.objectstore.
If I comment out the objecstore.flush() call, then I don't get an error,
but nothing gets inserted into the database.
If I leave objectstore.flush() in-place, I get a traceback when loading
the 2nd csv file.
Perhaps I mis-understand how this is supposed to work?
def load_csv_file(file_name):
"""Load specified csv file into database"""
engine = get_db_engine(echo=True) # calls global_connect
# strategy=threadlocal
engine.begin()
tablename = os.path.splitext(os.path.basename(file_name))[0]
print "processing ", tablename
table = getattr(database,tablename)
c = csv.DictReader(file(file_name, 'rU'))
for row in c:
for k, v in row.items():
if v == '':
row[k] = None
if k == 'id':
row[k] = int(v)
id = row['id']
record = table.get(id)
if not record:
record = table()
for k, v in row.items():
setattr(record, k, v)
objectstore.flush()
engine.commit()
processing contact_type
processing county
Traceback (most recent call last):
File "..\..\Python\p2p\database\util.py", line 62, in ?
main()
File "..\..\Python\p2p\database\util.py", line 56, in main
load_directory(directory=options.load_directory)
File "..\..\Python\p2p\database\util.py", line 12, in load_directory
load_csv_file(file_name)
File "..\..\Python\p2p\database\util.py", line 32, in load_csv_file
record = table.get(id)
File "e:\prj\sqlalchemy\lib\sqlalchemy\ext\assignmapper.py", line 7,
in do
return getattr(query, name)(*args, **kwargs)
File "e:\prj\sqlalchemy\lib\sqlalchemy\orm\query.py", line 43, in get
return self._get(key, ident, **kwargs)
File "e:\prj\sqlalchemy\lib\sqlalchemy\orm\query.py", line 285, in _get
return self._select_statement(statement, params=params,
populate_existing=re
load)[0]
File "e:\prj\sqlalchemy\lib\sqlalchemy\orm\query.py", line 293, in
_select_sta
tement
return self.instances(statement, params=params, **kwargs)
File "e:\prj\sqlalchemy\lib\sqlalchemy\orm\query.py", line 255, in
instances
result = self.session.execute(self.mapper, clauseelement,
params=params)
File "e:\prj\sqlalchemy\lib\sqlalchemy\orm\session.py", line 114, in
execute
return self.connection(mapper,
close_with_result=True).execute(clause, param
s, **kwargs)
File "e:\prj\sqlalchemy\lib\sqlalchemy\engine\base.py", line 240, in
execute
return Connection.executors[type(object).__mro__[-2]](self, object,
*multipa
rams, **params)
File "e:\prj\sqlalchemy\lib\sqlalchemy\engine\base.py", line 264, in
execute_c
lauseelement
return self.execute_compiled(elem.compile(engine=self.__engine,
parameters=p
aram), *multiparams, **params)
File "e:\prj\sqlalchemy\lib\sqlalchemy\engine\base.py", line 267, in
execute_c
ompiled
cursor = self.connection.cursor()
File "e:\prj\sqlalchemy\lib\sqlalchemy\engine\base.py", line 184, in
_get_conn
ection
raise exceptions.InvalidRequestError("This Connection is closed")
sqlalchemy.exceptions.InvalidRequestError: This Connection is closed
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users