views:

70

answers:

4

Hi guys!!

Is there a way to programatically import an entire database from a file with SQL?? (either .CSV , .SQL and .DB files are fine)

Thanks!!

EDITED AFTER TO CLARIFY:

I am interested in a solution that is Database independent (has to works with all types of databases (Mysql, SQL Server, PostGres, Oracle...)

+1  A: 

mysql -u -p < dumpfile.sql

importing csv would require a script (using eg. PHP) to put the right fields in the right bit of the query.

Emyr
Is this command part of PHP or is part of Mysql and has to be inserted into the command line shell?
Giorgio
that command should be run at the shell.
Emyr
+1  A: 

MySQL: LOAD DATA INFILE for csv's; for .sql files generated with MySQL, use the shell.


For SQLite: see this SO question.

SQL Server: apparently there's the BULK INSERT command.

You are not going to find a database-independent syntax for an SQL command because there isn't one.

There may be a wrapper library around databases but I'm not aware of it. (Or you could try to use ODBC, but that's connection oriented and wouldn't allow direct access to a file)

Perhaps there is an interactive GUI-related software tool out there to do this.

Note also that loading data directly from a file on a database server into a database almost certainly requires security privileges to do so (otherwise it is a security risk).

Jason S
Hey! I want to do that programatically inside of the code!I need to write an algorithm that each time imports a different database file so I can't type commands in the shell
Giorgio
A: 

If you are using SQL Server check out SSIS

Manjoor
A: 
Giorgio
That's not database independent, that's for SQLite.
Daniel DiPaolo