views:

51

answers:

2

How can I execute raw SQL after connect to database? I need to run script once, after connect to DB. Thanks.

UPD: question is not how to run raw SQL.

+1  A: 

Just visit the docs here.

It was I think the second match on google with "Django ORM".

EDIT See comments

If you look at this Django page you see that MySQLdb (the underlying layer) also accepts an init_command option which is run immediately after a connection is established. That's a feature of MySQLdb, and not so much of Django itself.

extraneon
sorry for the inaccuracy. I need to run a script once after connecting to db.
histrio
@histrio if the script can be used in the init_command (for instance calling a MySQL stored procedure) you can configure MySQLdb to run that. Otherwise some more details on the script are required.
extraneon
+1  A: 

If you are using django >= 1.2:

from django.db import connection, transaction

cursor = "SELECT foo FROM bar;"
connection.cursor().execute(query)
transaction.commit_unless_managed()
Matthew Schinckel