Thread overview
How do I execute a sql-file inside D code
Aug 20, 2019
Anders S
Aug 20, 2019
Andre Pany
Aug 26, 2019
Anders S
Aug 22, 2019
XavierAP
Aug 26, 2019
Anders S
August 20, 2019
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 ?
August 20, 2019
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
August 22, 2019
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
August 26, 2019
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
August 26, 2019
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