What is PostgreSQL driver is most stable?
Jacob Carlborg via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Mar 17 00:55:35 PDT 2017
On 2017-03-15 15:08, Suliman wrote:
> Could you give an example when it's better to use DBRow and where to get
> data in structure?
Use PGCommand and call "executeQuery" to get back a result set that is
iteratable:
auto query = "SELECT * FROM foos"
auto cmd = new PGCommand(connection, query);
auto resultSet = cmd.executeQuery!(string, int); // these types are the
column types
foreach (row ; resultSet)
{
auto name = row["name"];
auto bar = row[1];
// use the column name or index to get the value from a row
}
resultSet.close(); // the result set needs to be closed to be able to
execute additional queries
You can also directly create structs out of the rows:
struct Foo
{
string name;
string bar;
}
auto resultSet = cmd.executeQuery!(typeof(Foo.tupleof));
foreach (row ; resultSet)
{
auto foo = Foo(row[0 .. Foo.tupleof.length]);
assert(foo.name == row["name"]);
}
executeQuery - for returning a set of rows
executeRow - for returning a single
executeNonQuery - for executing a query without any result
See the examples for more information [1].
[1] https://github.com/pszturmaj/ddb/blob/master/examples/basic.d
--
/Jacob Carlborg
More information about the Digitalmars-d-learn
mailing list