www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How do I execute a sql-file inside D code

reply Anders S <anders xore.se> writes:
I'm creating an application that connect to a database and write 
data from another application. Now when I start the application I 
want it to check if the database exists and if not create the 
database and it's tables.

I have everything working IF the database and tables exist.

Use this code to check
conn.exec("CREATE DATABASE IF NOT EXISTS boxweb;");

however haven't found a way to run the sql file that create the 
tables. The file is in the source folder

Any ideas ?
Aug 20 2019
next sibling parent reply Andre Pany <andre s-e-a-p.de> writes:
On Tuesday, 20 August 2019 at 11:33:33 UTC, Anders S wrote:
 I'm creating an application that connect to a database and 
 write data from another application. Now when I start the 
 application I want it to check if the database exists and if 
 not create the database and it's tables.

 I have everything working IF the database and tables exist.

 Use this code to check
 conn.exec("CREATE DATABASE IF NOT EXISTS boxweb;");

 however haven't found a way to run the sql file that create the 
 tables. The file is in the source folder

 Any ideas ?
You need to use an additional library to communicate with the database. The library to use depends on the type of database (Oracle, DB2, sqlite, Hana, postgres,...) Which database type do you target? Kind regards Andre
Aug 20 2019
parent Anders S <anders xore.se> writes:
On Tuesday, 20 August 2019 at 13:10:55 UTC, Andre Pany wrote:
 On Tuesday, 20 August 2019 at 11:33:33 UTC, Anders S wrote:
 I'm creating an application that connect to a database and 
 write data from another application. Now when I start the 
 application I want it to check if the database exists and if 
 not create the database and it's tables.

 I have everything working IF the database and tables exist.

 Use this code to check
 conn.exec("CREATE DATABASE IF NOT EXISTS boxweb;");

 however haven't found a way to run the sql file that create 
 the tables. The file is in the source folder

 Any ideas ?
You need to use an additional library to communicate with the database. The library to use depends on the type of database (Oracle, DB2, sqlite, Hana, postgres,...) Which database type do you target? Kind regards Andre
I'm using MariaDB on std port 3306 /a
Aug 26 2019
prev sibling parent reply XavierAP <n3minis-git yahoo.es> writes:
On Tuesday, 20 August 2019 at 11:33:33 UTC, Anders S wrote:
 Use this code to check
 conn.exec("CREATE DATABASE IF NOT EXISTS boxweb;");

 however haven't found a way to run the sql file that create the 
 tables. The file is in the source folder
I understand you're using some API to some SQL implementation which allows you to run SQL commands from strings, but not from files which is what you want? Just read the file into a string with the D std lib: import std:file; conn.exec( readText(fileName) ); https://dlang.org/phobos/std_file.html#.readText
Aug 22 2019
parent Anders S <anders xore.se> writes:
On Thursday, 22 August 2019 at 13:39:00 UTC, XavierAP wrote:
 On Tuesday, 20 August 2019 at 11:33:33 UTC, Anders S wrote:
 Use this code to check
 conn.exec("CREATE DATABASE IF NOT EXISTS boxweb;");

 however haven't found a way to run the sql file that create 
 the tables. The file is in the source folder
I understand you're using some API to some SQL implementation which allows you to run SQL commands from strings, but not from files which is what you want? Just read the file into a string with the D std lib: import std:file; conn.exec( readText(fileName) ); https://dlang.org/phobos/std_file.html#.readText
Thanks XavierAP, that did the trick /a
Aug 26 2019