3
votes

I'm searching for a way to interact with a PostgreSQL database from an AHK script. All the ways I currently found include running psql.exe from the command-line and then parsing output. Of course, this leads to incredibly poor performance (execute process each time).

The question is, are there any ways to interact with database directly? Something that would allow you to connect and send queries via DllCall or another similar method known from other languages?

The library should make it possible to do something like:

psql_connection := psql_connect("localhost", "5432", "postgres", "dbpass", "TestDB")
rows := psql_query(psql_connection, "select * from TestTable limit 10") ; returns array of objects
2
A quick look suggests that autohotkey is written in C++. I'd be surprised if nobody's written an ODBC binding for it. If they haven't, well, I guess you're up! Using libpq and libpqtypes, or libpqxx, directly might be easier.Craig Ringer
There is useful lib github.com/IsNull/ahkDBA. It just doesn't seem to work with postgres.roma
@roma I don't think a library has been implemented yet... I suggest making a post in the forum here ahkscript.org and I'm sure plenty of people will offer to help. If not I'll try to ;)Joe DF
Posted message here: ahkscript.org/boards/viewtopic.php?f=5&t=4042 I'll see if someone already solved this problemroma
I've used this library in the past to connect to SQL Server. I'm not sure if that would work for postgresql. It does use the syntax you requested though.Elliot DeNolf

2 Answers

0
votes

You can use this library: https://autohotkey.com/board/topic/83542-func-adosql-uses-ado-to-manage-sql-transactions-v503l/

Or if feeling adventurous, use ComObjCreate("") for ADODB.Connection where you can use methods from https://docs.microsoft.com/en-us/sql/ado/reference/ado-api/connection-object-properties-methods-and-events

;test - may not be accurate
connection := ComObjCreate("ADODB.Connection")
connection.Open("Provider =PostgreSQL OLE DB Provider; Data Source =localhost; location =DBTest; User ID =postgres; password =pass")
recordset := connection.Execute("SELECT * FROM tablename")