Howto execute custom SQL from the command line

4,797 views
Skip to first unread message

Tourneur Henry-Nicolas

unread,
Mar 25, 2008, 12:32:27 PM3/25/08
to Django users
Hello,

At the installation stage of the software (Django bases, of course) I'm
developing, I have to insert data in a database. Is there any easy way to
tell django to execute the SQL directives contained in a .sql file ?

I tried using manage.py but I don't know the good options.
So I would like to do sth like :
./manage.py action file.sql
That is what I was trying to do.

TIA.

Amit Ramon

unread,
Mar 26, 2008, 12:25:18 PM3/26/08
to django...@googlegroups.com
You can do that easily enough without django...
one way is to do:
mysql -u<user> -p<password> <database> < file.sql

the other is to go into mysql and there do:
\. file.sql

You can open a mysql shell with:
manage.py dbshell


--- Amit


* Tourneur Henry-Nicolas <henry-nicol...@mactelecom.com> [2008-03-25 17:32 +0100]:

Tourneur Henry-Nicolas

unread,
Mar 27, 2008, 3:27:59 AM3/27/08
to django...@googlegroups.com
On Wednesday 26 March 2008 17:25:18 Amit Ramon wrote:
> You can do that easily enough without django...
> one way is to do:
> mysql -u<user> -p<password> <database> < file.sql
>
> the other is to go into mysql and there do:
> \. file.sql
>
> You can open a mysql shell with:
> manage.py dbshell
>
>
I prefer the second way because I have to stay database independant.
The problem is (with pgsql at least) that when I type manage.py dbshell it
asks me for the password even if the password is present in me settings.py.

Amit Ramon

unread,
Mar 27, 2008, 4:54:27 AM3/27/08
to django...@googlegroups.com
* Tourneur Henry-Nicolas <henry-nicol...@mactelecom.com> [2008-03-27 08:27 +0100]:

>
> On Wednesday 26 March 2008 17:25:18 Amit Ramon wrote:
> > You can do that easily enough without django...
> > one way is to do:
> > mysql -u<user> -p<password> <database> < file.sql
> >
> > the other is to go into mysql and there do:
> > \. file.sql
> >
> > You can open a mysql shell with:
> > manage.py dbshell
> >
> >
> I prefer the second way because I have to stay database independant.
> The problem is (with pgsql at least) that when I type manage.py dbshell it
> asks me for the password even if the password is present in me settings.py.

I'm not familiar with pgsql, so I don't know why is it asking you for a password. There is a third method for doing what you want, although it is quiet awkward. You can write a simple python/django script, that reads the sql statements from your sql file and execute them using custom sql. It'll probably be slower, especially for large files, than using the db shell, so I wouldn't really recommend it, unless you can justify it somehow.

For custom sql, see:
http://www.djangoproject.com/documentation/model-api/#executing-custom-sql

--- Amit

Thomas Guettler

unread,
Mar 27, 2008, 11:47:38 AM3/27/08
to django...@googlegroups.com
Tourneur Henry-Nicolas schrieb:
You can do this (at least on unix like systems):

echo 'SELECT * from app_mytable;' | ./manage.py dbshell

or
cat file.sql | ./manage.py dbshell


--
Thomas Guettler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de

Reply all
Reply to author
Forward
0 new messages