Uwe Grauer wrote:
> Oleg Deribas wrote:
>> Hello,
>>
>>
>> I'm trying to investigate possibility to use SQLAlchemy with Firebird
>> SQL. And I've wrote this simple script following tutorial:
>>
>> ---------------------------------------------------------------------
>> from sqlalchemy import *
>>
>> db = create_engine('firebird://SYSDBA:[EMAIL PROTECTED]/employee')
>>
>> metadata = BoundMetaData(db)
>> metadata.engine.echo = True
>>
>> try:
>> # Drop users table if it exists
>> users_table = Table('users', metadata, autoload=True)
>> users_table.drop()
>> del(users_table)
>> except:
>> pass
>>
>> users_table = Table('users', metadata,
>> Column('user_id', Integer, primary_key=True),
>> Column('user_name', String(40)),
>> Column('passwd', String(10)),
>> redefine=True
>> )
>>
>> users_table.create()
>> i = users_table.insert()
>> print i
>> ---------------------------------------------------------------------
>>
>> And it gives me this:
>> AttributeError: 'NoneType' object has no attribute 'has_key'
>>
>> My config:
>> win2k, python 2.4, SQLAlchemy 0.2.6, kinterbasdb 3.2rc1, firebird 2.0rc3
>>
>
> Firebird needs exclusive access for DDL.
> Normally you don't do DDL and DML in the same script.
> Try creating the needed table, commit it and drop the connection.
> Than create a new connection and do your data inserts.
>
> At least, that's how i am doing it.
> It works!
>
> Uwe
>
>
Here is an example of a working script:
def def_db_meta(metadata):
telcall_table = Table('telcall', metadata,
Column('iid', Integer,
Sequence('gen_telcall'), primary_key=True),
Column('calltype', Integer, nullable=False,
default=4),
Column('calltime', DateTime, nullable=False),
Column('callnumber', VARCHAR(40)),
Column('callline', VARCHAR(40)),
Column('callfrom', VARCHAR(40), nullable=False),
Column('callduration', Integer, nullable=False),
Column('callvoip', Integer, nullable=False),
Column('crestamp', DateTime, nullable=False,
default=func.now()),
)
i_telcall_unique = Index('telcall_unique',
telcall_table.c.calltype,
telcall_table.c.calltime,
telcall_table.c.callnumber,
telcall_table.c.callfrom,
unique=True)
return telcall_table
def create_db(fdout, dburi):
engine = create_engine(dburi)
metadata = BoundMetaData(engine)
telcall_table = def_db_meta(metadata)
session = create_session(bind_to=engine)
if engine.has_table('TELCALL'):
print_err('Table TELCALL exists.\nCreate operation aborted.')
return
print_err('creating table TELCALL')
trans = session.create_transaction()
try:
telcall_table.create()
trans.commit()
except:
trans.rollback()
raise
def insert_into_db(fdout, dburi, telcalls):
print 'insert_into_db()'
engine = create_engine(dburi)
metadata = BoundMetaData(engine)
telcall_table = def_db_meta(metadata)
session = create_session(bind_to=engine)
if not engine.has_table('TELCALL'):
print_err('Table TELCALL does not exist.\nPlease create metadata.')
return
dup_count = 0
ins_count = 0
# now insert data
print_err('inserting data.')
i = telcall_table.insert()
for call in telcalls.telcalllist:
#call.Print(fdout)
trans = session.create_transaction()
try:
i.execute(calltype = call.calltype,
calltime = call.calltime,
callnumber = call.callnumber,
callline = call.callline,
callfrom = call.callfrom,
callduration = call.callduration,
callvoip = call.callvoip,
crestamp = datetime.datetime.now())
trans.commit()
ins_count = ins_count + 1
except Exception, detail:
dup_count = dup_count + 1
trans.rollback()
print detail
print_err('%d duplicates not inserted.' % dup_count)
print_err('%d record inserted.' % ins_count)
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users