Subject | RE: [firebird-python] Is it possible to execute a sql script with KInterbasDB? |
---|---|
Author | Michael D. Spence |
Post date | 2009-04-23T00:06:33Z |
> -----Original Message-----The thing is, python's so adept at string handling that having this sort
> From: firebird-python@yahoogroups.com
> [mailto:firebird-python@yahoogroups.com] On Behalf Of Sasha Matijasic
> Sent: Wednesday, April 22, 2009 7:02 PM
> To: firebird-python@yahoogroups.com
> Subject: Re: [firebird-python] Is it possible to execute a
> sql script with KInterbasDB?
>
>
> > Long anwser:
> > import kinterbasdb
> > if not kinterbasdb.initialized:
> > kinterbasdb.init(type_conv=200,concurrency_level=1)
> >
> > con = kinterbasdb.connect(
> > host='192.168.0.101', database='/data/db/pool.fdb',
> > user='sysdba', password='masterkey',
> > dialect=3, # necessary for all dialect 1 databases
> > charset='UTF8' # specify a character set for the connection
> > )
> > cur = con.cursor()
> > cur.execute('HERE YOU SCRIPT')
> > cur.execute('HERE YOU SCRIPT')
> >
> > don't forget the commits.
> >
>
> Thanks, but that's not what I was looking for. I have a sql script
> containing multiple statements, and with your example I can only
> execute a single sql statement in one cur.execute() call.
>
> For example, my script is like this:
> sql = """create table foo(a char(1) not null); create table bar(a
> char(1)); commit;"""
> cur.execute(sql)
>
> Can this be done without any 3rd party library? (and if it can't, do
> you know of any?)
>
> Thanx,
> sasha
of feature in KInterbasDB would be redundant.
If each line of SQL is one command, then you can do something like
this:
fi = open("C:\path\to\file\script.sql")
for line in fi:
cur.execute(line)
Or, this ought to work, although I've never tried it:
fi = open("C:\path\to\file\script.sql")
txt = fi.read()
lines = txt.split(';')
for line in lines:
cur.execute(line)
Michael D. Spence
Mockingbird Data Systems, Inc.