This question already has answers here:
EXECUTE...USING statement in PL/pgSQL doesn't work with record type?
(2 answers)
Closed 6 years ago.
As part of my k-medoid algorithm, I have declared a record variable called o_random in my function, which I use to store a random row retrieved from a table of crimes as in this query:
EXECUTE 'SELECT * FROM algorithms.kmedoid_crimes_' ||k||' WHERE
cluster_id='||k_count||' OFFSET floor(random()*'||row_count||') LIMIT 1'
INTO o_random;
However, the below query is giving me an error saying "could not identify column "latitude" in record data type". The crimes table I am getting the row from definitely has a field called latitude.. so I'm thinking I'm trying to get the data from the record variable wrongly?
EXECUTE 'UPDATE algorithms.km_cluster_centres_' ||k||'
SET latitude = $1.latitude, longitude = $1.longitude, geom =
ST_Transform(ST_SetSRID(ST_MakePoint($1.longitude, $1.latitude), 4326),3435)
WHERE id=' ||k_count
USING o_random;
Found some solutions on stack but none of them seem to work.. help would be appreciated.
Just a guess
EXECUTE 'UPDATE algorithms.km_cluster_centres_' || quote_literal(k) ||'
SET latitude = ($1).latitude, longitude = ($1).longitude, geom =
ST_Transform(ST_SetSRID(ST_MakePoint(($1).longitude,($1).latitude), 4326),3435)
WHERE id=' || quote_literal( k_count )
USING o_random;
UPDATE: If it dosen't work try to log with http://www.postgresql.org/docs/current/static/plpgsql-errors-and-messages.html
Related
This question already has an answer here:
DB2 error Improper use of a string column, host variable, constant, or function
(1 answer)
Closed 8 months ago.
I´ve been facing some weird issues when studding SQL raw queries using SqlAlchemy.
sqlstr = 'SELECT "City" from CHICAGO_SCHOOLS;'
with engine.connect() as conn:
result = conn.execute(text(sqlstr))
print (result.all())
The query above returns hundreds of "Chicago" as results. So I just tried to get unique results:
sqlstr = 'SELECT DISTINCT "City" from CHICAGO_SCHOOLS;'
with engine.connect() as conn:
result = conn.execute(text(sqlstr))
print (result.all())
Now, all I got is a weird error :
Exception: SQLNumResultCols failed: [IBM][CLI Driver][DB2/LINUXX8664]
SQL0134N Improper use of a string column, host variable, constant, or
function "City". SQLSTATE=42907
At first I thought it was somehow related to the DISTINCT set quantifier. So I tried the same query with another column.
sqlstr = 'SELECT DISTINCT "School ID" from CHICAGO_SCHOOLS;'
with engine.connect() as conn:
result = conn.execute(text(sqlstr))
print (result.all())
And in this query I got all expected results.
I am not being able to truly understand what is wrong!
The issue was related to the column type. It was a CLOB type and that does not allow use of DISTINCT. Thanks to HoneyBadger
This is my table:
I want to change the TargetCondition column to 'TRUE' when the ICD9CODE column contains a particular range (like between 250 and 250.93).
Please help me do it. Thanks
This is the code here:
update demo.fea_02 set TargetCondition = TRUE where ICD9Code like between '%250%' and '%250.93%'
got the error:
Syntax error: Unexpected keyword BETWEEN at [6:17]
This answers the original version of the question.
You would use update:
update mytable
set targetcondition = 'TRUE'
where icd9code like '%value%';
I'm trying to retrieve some data from a postgresql database using psycogp2, and either exclude a variable number of rows or exclude none.
The code I have so far is:
def db_query(variables):
cursor.execute('SELECT * '
'FROM database.table '
'WHERE id NOT IN (%s)', (variables,))
This does partially work. E.g. If I call:
db_query('593')
It works. The same for any other single value. However, I cannot seem to get it to work when I enter more than one variable, eg:
db_query('593, 595')
I get the error:
psycopg2.DataError: invalid input syntax for integer: "593, 595"
I'm not sure how to enter the query correctly or amend the SQL query. Any help appreciated.
Thanks
Pass a tuple as it is adapted to a record:
query = """
select *
from database.table
where id not in %s
"""
var1 = 593
argument = (var1,)
print(cursor.mogrify(query, (argument,)).decode('utf8'))
#cursor.execute(query, (argument,))
Output:
select *
from database.table
where id not in (593)
This question already has an answer here:
How to use ADO Query Parameters to specify table and field names?
(1 answer)
Closed 5 years ago.
Whenever I try to run this code it returns a syntax error, I have followed a few formats from what was posted here, but nothing I do seems to work. How can I fix this?
with adoQuerysupplier do
begin
sql.Clear;
sql.Add('UPDATE SupplierList');
SQL.Add('SET :Column = :newdetail');
SQL.Add('WHERE :Column1 = :OldDetail');
Parameters.ParseSQL(adoQuerysupplier.SQL.Text, True);
Parameters.ParamByName('Column').Value:= column;
Parameters.ParamByName('Column1').Value:= column;
Parameters.ParamByName('newdetail').Value:= newdetail;
Parameters.ParamByName('OldDetail').Value:= olddetail;
ExecSQL;
all variables are strings and are successfully being captured. This is my first question so I apologize in advance if I have any format errors. Please let me know if I need to post anymore more code. The table name is not a variable
you can't use parameters for field names. if you really have to do that you need to write a big switch statement. alternatively build the query as a string:
sql.Add('UPDATE SupplierList');
SQL.Add('SET [' + column2 + '] = :newdetail');
SQL.Add('WHERE [' + column1 + '] = :OldDetail');
Parameters.ParseSQL(adoQuerysupplier.SQL.Text, True);
Parameters.ParamByName('newdetail').Value:= newdetail;
Parameters.ParamByName('OldDetail').Value:= olddetail;
Consider possible SQL injection!
I need to update an XML node that contains a specific number. My query works if I hard code in the number (see figure 1), but I would like to make this dynamic by passing through a variable that contains a string (variableToBeReplaced). I currently wrote this (see figure 2) but it isn't reading the variable correctly so no changes are being made to the xml. Does anyone know how I can include a variable in an updatexml() function?
Figure 1
select updateXML(xmltype(xmlbod),'/LpnList/Lpn/LicensePlateNumber[text() = "12345"]','67890').getClobVal() doc
from myTable
where id = '1'
Figure 2
select updateXML(xmltype(xmlbod), '/LpnList/Lpn/LicensePlateNumber[text()= '|| variableToBeReplaced || ']','67890').getClobVal() doc
from myTable
where id = '1'
I was just missing "" around the variable.
select updateXML(xmltype(xmlbod), '/LpnList/Lpn/LicensePlateNumber[text()= "'|| variableToBeReplaced || '"]','67890').getClobVal() doc
from myTable
where id = '1'