SQLite

Vadim Lopatin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Oct 21 03:50:30 PDT 2016


On Wednesday, 19 October 2016 at 16:01:37 UTC, Alfred Newman 
wrote:
> Hello,
>
> I am trying to handle a SQLite3 table with D. During my 
> researchs, I discovered the lib 
> https://dlang.org/phobos/etc_c_sqlite3.html.
>
> However, for any reason, there is no code snippets or sample 
> codes available there. So, I am stucked.
>
> I have the following sample structure table:
>    sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "  \
>          "VALUES (1, 'Paul', 32, 'California', 20000.00 ); " \
>          "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "  \
>          "VALUES (2, 'Allen', 25, 'Texas', 15000.00 ); "     \
>          "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)" \
>          "VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );" \
>          "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)" \
>          "VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );";
>
> Can you pls provide a code snippet or some hints to the 
> following job:
> - Create a table with the layout above
> - Iterate through the records given a basic SELECT WHERE Query
>
> Thanks in advance, AN

Snippet how to do it using DDBC library 
https://github.com/buggins/ddbc

     import ddbc;

     string url = "sqlite:testdb.sqlite";
     // creating Connection
     auto conn = createConnection(url);
     scope(exit) conn.close();
     // creating Statement
     auto stmt = conn.createStatement();
     scope(exit) stmt.close();
     // execute simple queries to create and fill table
     stmt.executeUpdate("CREATE TABLE COMPANY (ID int, NAME 
varchar, AGE int,ADDRESS varchar, SALARY double)");
     string[] statements = [
         "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES 
(1, 'Paul', 32, 'California', 20000.00 )",
         "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES 
(2, 'Allen', 25, 'Texas', 15000.00 )",
         "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES 
(3, 'Teddy', 23, 'Norway', 20000.00 )"
     ];
     foreach(sql; statements)
         stmt.executeUpdate(sql);



More information about the Digitalmars-d-learn mailing list