C++ postgis prepared statement failing, invalid geometry - c++

I'm using libpqxx and a prepared statements, when I try to insert programmatically it fails with an error "Invalid geometry", but I can do the same insert via sql prompt.. Am I missing something? I tried to escape the ' in the prepare statement call but same error
con.prepare("chat_insert", "INSERT INTO chat values (nextval('chat_seq'), $1, ST_GeomFromText('POINT($2 $3)', 4326), $4)");
worker.prepared("chat_insert")(chatid)(lon)(lat)(msg).exec();
worker.commit();
I also tried
con.prepare("chat_insert", "INSERT INTO chat values (nextval('chat_seq'), $1, ST_GeomFromText(\'POINT($2 $3)\', 4326), $4)");
Output:
Chat id: chat:user:128946234
Lat: 14.6049
Lon: 121.033
ERROR: parse error - invalid geometry
HINT: "POINT(" <-- parse error at position 6 within geometry
If I go into the sql prompt I can run this and it'll insert
insert into chat values (nextval('chat_seq'), 'chat:user:128946234', ST_GeomFromText('POINT(121.033 14.6049)', 4326), 'This is a test msg....');

You are attempting to parse a WKT string of POINT($2 $3) which has dollar signs in it. These are not parameters in this context, since WKT is a string.
Use a function that accepts numeric parameters, such as ST_MakePoint(x, y):
con.prepare("chat_insert", "INSERT INTO chat (chatid, geom, msg) "
"VALUES ($1, ST_SetSRID(ST_MakePoint($2, $3), 4326), $4)");
Note that I've listed the columns to insert after chat, which is considered a best practice.

Related

Format timestamp inside a set-column sentence

I'm developing a data fusion pipeline. It contains a wrangler node where I'm trying to create a new field that will contain the system date in timestamp format (yyyy-MM-dd'T'HH-mm-ss).
I've tried using the sentence:
set-column :sysdate (${logicalStartTime(yyyy-MM-dd'T'HH-mm-ss)})
But I receive the error:
Caused by: io.cdap.wrangler.api.DirectiveParseException: Error encountered while parsing 'set-column' : Error encountered while compiling '( 2022 -12-01T16-29-32 ) ' at line '1' and column '14'. Make sure a valid jexl transformation is provided.
Which would be the correct sentence?
I've tried:
set-column :sysdate (${logicalStartTime(yyyy-MM-ddHH-mm-ss)})
Which will result in something like "1877", as it substracts the numbers, and also tried:
set-column :sysdate (${logicalStartTime(yyyyMMddHHmmss)})
but the format isn't correct and can only be written if the field is a String.
You have the correct method, just incorrect syntax. The syntax you are looking for is set-column :sysdate ${logicalStartTime(yyyy-MM-dd'T'HH-mm-ss)}, you have to remove (). Then you can convert the string in datetime pattern in this format parse-as-datetime :sysdate "yyyy-MM-dd'T'HH-mm-ss".

SQLite 3 Error when using parameter string, but not when implicitly typed

I have a snippet of code from my python 2.7 program:
cur.execute("UPDATE echo SET ? = ? WHERE ID = ?", (cur_class, fdate, ID,))
that when run, keeps throwing the following error:
sqlite3.OperationalError: near "?": syntax error
The program is supposed to insert today's date, into the class column that matches the student ID supplied. If I remove the first "?" like so and hard code the parameter:
cur.execute("UPDATE echo SET math = ? WHERE ID = ?", (fdate, ID,))
everything works just fine. I've googled all over the place and haven't found anything that works yet so I'm throwing out a lifeline.
I've tried single quotes, double quotes, with and without parenthesis and a few other things I can't remember now. So far nothing works other than hard coding that first parameter which is really inconvenient.
As a troubleshooting step I had my program print the type() of each of the variables and they're all strings. The data type of the the cur_class field is VARCHAR, fdate is DATE, and ID is VARCHAR.
Thanks to the tip from #Shawn earlier I solved the issue with the following code and it works great:
sqlcommand = "UPDATE echo SET " + cur_class + " = " + fdate + " WHERE ID = " + ID
cur.execute(sqlcommand)
This way python does the heavy lifting and constructs my string with all the variables expanded, then has the db execute the properly formatted SQL command.

Cyper clojure necons tquery: How to Match by Label with {_variable}?

I am trying to use clojure neocons to perform a cypher query where I need to use a variable placeholder for the label:
(def node-query "MATCH (n:{_nodetype})
RETURN n;")
I am getting an error when I add {_nodetype} but no error when I remove {_nodetype}
"Invalid input '{': expected whitespace or a label name (line 1, column 10 (offset: 9))\\n\\\"MATCH (n:{_nodetype})\\\"\\n ^\",\n \"exception\"...
Here is my call to tquery:
(cy/tquery conn node-query {:_nodeid _nodeid :_nodetype "Folder"})
What is the cypher / neocons syntax to MATCH by label with a variable?
I am very grateful for help you could suggest.
You cannot parameterize a label (or a relationship type). The rationale for this is that a different parameter value might result in a completely different query plan. Internally the query plan is cached with the query string.
For now just do string concatenation in Clojure to have "semi-dynamic" labels or relationship types.

Remove incorrect quotes from generated SQL files

I have 100000 lines as such:-
/** http://www.marinetraffic.com/en/ais/details/ships/8720175/vessel:SEMUTIK_NO_1 **/INSERT INTO `vessel`(id,name,`imo`, `flag`, `type`, `speed`, `callsign`, `tonnage`, `length`, `deadweight`, `year`, `status`, `draught`,mmsi) VALUES (27797,'SEMUTIK_NO_1'','8720175','-','Fishing Vessel','N/A','-','152','0m x 0m','0','1974','Active','0m',-8720175)
^
I've generated these queries and saved it in various files.However now i found that there is a problem with the query and it will be taking me a lot of time to regenerate the query.See the name value in the query, there is an extra quote. I wanna know a regex to find and remove it, mostly in Java or even php.
Just open your file with queries in a text editor of your choice (Sublime, TextMate, vim, what have you) and issue find and replace '', for ',
you can try this,
String str = "INSERT INTO vessel(id,name,imo, flag, type, speed, callsign, \n" +
"tonnage, length, deadweight, year, status, draught,mmsi) \n" +
"VALUES (27797,'SEMUTIK_NO_1'','8720175','-','Fishing Vessel',\n" +
"'N/A','-','152','0m x 0m','0','1974','Active','0m',-8720175)";
System.out.println(str.replaceAll("''", "'"));

Firebird IF ELSE

I have this line in my query:
IF (TEST_DESC CONTAINING 'OPEN') THEN TEST_DESC = 'OPEN';
but that is not working. Firebird says Can't Prepare , because query is empty,
when I remove the ; it says Token unknown - line 8, column 10.
(.
I want to use IF ELSE to look if the TEST_DESC field contains OPEN, if it has, it will output the word OPEN in the field. The TEST_DESC field contains SCL_OPEN I only want to show the OPEN. And the other one is that the other value contains DRV_SHORT, and I want to show SHORT only.
Thanks in advance!
IF/THEN/ELSE is a PSQL construct.
For a plain SQL query, which I infer you're using from your syntax error, use a CASE statement:
...
CASE
WHEN TEST_DESC LIKE '%OPEN%'
THEN 'OPEN'
WHEN TEST_DESC LIKE '%SHORT%'
THEN 'SHORT'
ELSE 'UNKNOWN'
END
...
There is also an IIF() function with approximately the syntax you want: IIF(TEST_DESC CONTAINING 'FOO', 'FOO', 'NO FOO').
See also this Firebird FAQ entry.