[storm] execute batch sql

Gustavo Niemeyer gustavo at niemeyer.net
Mon Nov 3 00:06:46 GMT 2008


Hi Jean,

> i would like to keep and maintain my schema generation in a sql file,
> I would like to execute it from my initialization/installation
> function in my python code. But store.execute() only accept a single
> SQL statements and I could find another method to do it. I do :
>
> """
> store = Store( create_database('sqlite:%s' % dbfile) )
> store.execute( file(schemafile).read() )
> """
>
> While my schemafile is :
> """
> CREATE TABLE names ( name VARCHAR, id INTEGER PRIMARY KEY );
> CREATE TABLE rights ( right varchar, id INTEGER PRIMARY KEY );
> """
>
> I could open a subprocess which calls the SQL client, the sqlite
> command in this case, or psql for postgresql with my file as a
> parameter?
> I could split my file into statement embedded in the python code?
>
> I am satisfied with neither of these solution? do you see a better way?

If you want to execute it from Python, storing that information in a
way that is easy to read programatically will make your life easier.
That may be done storing the statements inside a Python file using
Python syntax, which may be the most straightforward, or any other
nicely readable text format of your choice.

Some databases will also allow you to do just what you suggest above,
since they'll know how to split a string into the statements in the
given string, so some experimentation may be interesting.

So there are really many ways to do it, depending on your own
preference and use cases.

-- 
Gustavo Niemeyer
http://niemeyer.net



More information about the storm mailing list