Thread overview | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
December 24, 2014 Need example of usage DerelictPQ | ||||
---|---|---|---|---|
| ||||
Could anybody provide any simple examples of usage DerelictPQ. I do not have experience of C, and I can't understand how to use this driver. I need just basics like connect, select and insert. http://code.dlang.org/packages/derelict-pq thanks! |
December 24, 2014 Re: Need example of usage DerelictPQ | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | I have done next: string connString = "psql -h localhost -p 5432 -U postgres -d testdb"; package PGconn* conn; @property bool nonBlocking(){ return PQisnonblocking(conn) == 1; } this(parseConfig parseconfig) { void connect() { try { DerelictPQ.load(getcwd ~ buildPath("\\libs\\libpq.dll")); conn = PQconnectdb(toStringz(connString)); if( !nonBlocking && PQstatus(conn) != ConnStatusType.CONNECTION_OK ) throw new Exception("Can't connect to DB"); } catch ( DerelictException de ) { writefln("Failed to load libpq.dll: %s", de.msg); } } connect(); But it runtime I am getting next error: object.Exception@source\app.d(203): Can't connect to DB ---------------- 0x00402D31 in D3app10PostgreSQL6__ctorMFC3app11parseConfigZ7connectMFZv at D:\Pr oject\2014\seismoDownloader\source\app.d(203) 0x00402C3F in app.PostgreSQL app.PostgreSQL.__ctor(app.parseConfig) at D:\Projec t\2014\seismoDownloader\source\app.d(216) 0x0040208B in _Dmain at D:\Project\2014\seismoDownloader\source\app.d(30) What I am missing? Where I can to look right format of connection string for this driver? |
December 24, 2014 Re: Need example of usage DerelictPQ | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | I haven't used the derelict version but I have used the C library itself and wrapped it briefly in my postgres.d here: https://github.com/adamdruppe/arsd/blob/master/postgres.d (implements an interface from database.d in the same repo) I used very, very little of the library, but it is enough to get basic results. Your connection string can just be "dbname=NAME_HERE". Given your command line, "dbname=testdb" would be the first one I try. Here's the docs on those strings: http://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-PARAMKEYWORDS Your host and port are the default, so no need to list them. You might need to set the user, but a better option there might be to grant your regular user (whatever one you are logged into the operating system as) permission to access the database, then the default will work for that too. The second class in my file shows how to get basic results. string[] row; for(int i = 0; i < numFields; i++) { string a; if(PQgetisnull(res, position, i)) a = null; else { a = to!string(PQgetvalue(res, position, i), PQgetlength(res, position, i)); } row ~= a; } gets it as an array of strings. This simple option loses the type checking you can get from the database but it works for a lot of stuff too. |
December 24, 2014 Re: Need example of usage DerelictPQ | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Wednesday, 24 December 2014 at 14:08:53 UTC, Adam D. Ruppe wrote:
> I haven't used the derelict version but I have used the C library itself and wrapped it briefly in my postgres.d here:
>
> https://github.com/adamdruppe/arsd/blob/master/postgres.d
Thanks! But few questions:
1. how to build it with dub?
2. I tried to: dmd app.d database.d postgres.d but get message that "File Not Found pq.lib"
3. Can I build it's simply: dmd app.d ?
Would it able to import other 2 file?
|
December 24, 2014 Re: Need example of usage DerelictPQ | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | On Wednesday, 24 December 2014 at 19:26:11 UTC, Suliman wrote: > 1. how to build it with dub? I don't know, I don't use dub. > 2. I tried to: dmd app.d database.d postgres.d but get message that "File Not Found pq.lib" Download the lib file from me here: http://arsdnet.net/dcode/pq.lib You'll still need the DLLs, the difference between my lib and the Derelict one is I bind it statically whereas Derelict loads the dll at runtime. Both need the dll, but Derelict skips the lib step. It is the same API though, so you could also just use my source as an example to guide writing your derelict based program. > 3. Can I build it's simply: dmd app.d ? > Would it able to import other 2 file? Won't work since then the other functions won't be compiled, always need to pass them along to dmd too or build a separate .lib (but that's not really worth the hassle) |
December 25, 2014 Re: Need example of usage DerelictPQ | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | On 12/24/2014 8:56 PM, Suliman wrote: > Could anybody provide any simple examples of usage DerelictPQ. I do not > have experience of C, and I can't understand how to use this driver. > > I need just basics like connect, select and insert. > > http://code.dlang.org/packages/derelict-pq > > thanks! DerelictPQ is only a binding to libpq. The only difference is the DerelictPQ.load method. Just follow the libpq documentation. http://www.postgresql.org/docs/9.1/static/libpq.html |
December 25, 2014 Re: Need example of usage DerelictPQ | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | On 12/25/2014 11:23 AM, Mike Parker wrote: > On 12/24/2014 8:56 PM, Suliman wrote: >> Could anybody provide any simple examples of usage DerelictPQ. I do not >> have experience of C, and I can't understand how to use this driver. >> >> I need just basics like connect, select and insert. >> >> http://code.dlang.org/packages/derelict-pq >> >> thanks! > > DerelictPQ is only a binding to libpq. The only difference is the > DerelictPQ.load method. Just follow the libpq documentation. > > http://www.postgresql.org/docs/9.1/static/libpq.html Actually, Derelict binds to version 9.3, so the proper link should be http://www.postgresql.org/docs/9.3/static/libpq.html |
December 25, 2014 Re: Need example of usage DerelictPQ | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | >> DerelictPQ is only a binding to libpq. The only difference is the
>> DerelictPQ.load method. Just follow the libpq documentation.
>>
>> http://www.postgresql.org/docs/9.1/static/libpq.html
>
> Actually, Derelict binds to version 9.3, so the proper link should be
>
> http://www.postgresql.org/docs/9.3/static/libpq.html
But could you look at my example and say what I am doing wrong? For my regret I do not know C and I have some problems with understanding this doc.
|
December 25, 2014 Re: Need example of usage DerelictPQ | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | On 12/25/2014 3:03 PM, Suliman wrote:
>>> DerelictPQ is only a binding to libpq. The only difference is the
>>> DerelictPQ.load method. Just follow the libpq documentation.
>>>
>>> http://www.postgresql.org/docs/9.1/static/libpq.html
>>
>> Actually, Derelict binds to version 9.3, so the proper link should be
>>
>> http://www.postgresql.org/docs/9.3/static/libpq.html
>
> But could you look at my example and say what I am doing wrong? For my
> regret I do not know C and I have some problems with understanding this
> doc.
I've never used libpq. Any C samples you find should be mostly directly translatable to D, with just a few minor adjustments. Look around for such a sample, or a libpq tutorial if such a thing exists, and try to port it over to D. Then if you have any trouble, I'm sure you can find help here with translating it.
|
December 28, 2014 Re: Need example of usage DerelictPQ | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | Adam, I trying to build simple app: import std.stdio; import postgres; import database; void main() { auto db = new PostgreSql("dbname = test2"); foreach(line; db.query("SELECT * FROM customer")) { writeln(line[0], line["customer_id"]); } } and getting next error: D:\123>dmd app.d database.d postgres.d postgres.d(124): Error: struct database.Row member resultSet is not accessible postgres.d(139): Error: struct database.Row member row is not accessible Also I can't understand why I do not need specify login and pass when I connection to DB? |
Copyright © 1999-2021 by the D Language Foundation