I want to remove the quotes from each end of string for SQL insert into Postgres.
At the moment my code looks like this:
label='ave_number'
sql_cmd=u"""UPDATE rpt.so_form2_test_noquotes SET %s=%s WHERE channel_id=%s and report_id=%s and load_date=current_date;"""
sql_params=(label, e[u"label"],channel_id,report_id,)
calldb(conn, sql_cmd, sql_params)
def calldb( db, sql_cmd, sql_params): # invoke backend function that is a INSERT/UPDATE statement
try:
cur = db.cursor() # use standard cursor as return is likely void
print 'Executing sql cmd "{0}"'.format(cur.mogrify(sql_cmd, sql_params))
cur.execute(sql_cmd, sql_params)
return # execute returns None expected
except Exception as e:
print u'Error ', e
raise
My Postgres column name is ave_number with no quotes. When the %s get substitued with the string it gives me an error saying there is no column called 'ave_number'.
Thanks in advance
I think you need to use AsIs from psycopg2.extensions
from psycopg2.extensions import AsIs
import psycopg2
def calldb( db, sql_cmd, sql_params): # invoke backend function that is a INSERT/UPDATE statement
try:
cur = db.cursor() # use standard cursor as return is likely void
print 'Executing sql cmd "{0}"'.format(cur.mogrify(sql_cmd, sql_params))
cur.execute(sql_cmd, sql_params)
return # execute returns None expected
except Exception as e:
print u'Error ', e
raise
conn = psycopg2.connect(host='localhost', port=5432, user='xxxxxxx', password='xxxxxx')
label='ave_number'
e={u'label':'test_label'}
channel_id=1
report_id=1
sql_cmd=u"""UPDATE rpt.so_form2_test_noquotes SET %s=%s WHERE channel_id=%s and report_id=%s and load_date=current_date;"""
sql_params=(AsIs(label), e[u"label"],channel_id,report_id,)
calldb(conn, sql_cmd, sql_params)
which outputs this
Executing sql cmd "UPDATE rpt.so_form2_test_noquotes SET ave_number='test_label' WHERE channel_id=1 and report_id=1 and load_date=current_date;"
Related
I always used psycopg2 to connect my Postgres database to my script, but, this is the first time that a get this error.
import psycopg2
import pandas as pd
conn = psycopg2.connect(
host='dabname',
database='db',
user='myuser',
password='mypassword')
cursor = conn.cursor()
cursor.execute("select relname from pg_class where relkind='r' and relname !~ '^(pg_|sql_)';")
print(cursor.fetchall())
and this returns:
[('tb_orcamento_mes',), ('tb_notificacao',), ('tb_grupo_premissa',), ('tb_premissa',), ('tb_etapas',), ('tb_orcamento_anual',)]
But, when I try to fetch all records from 'tb_premissa', I get an error:
cursor = conn.cursor()
cursor.execute("SELECT * FROM tb_premissa")
quantitativos_realizado = cursor.fetchall()
results in this error:
UndefinedTable: ERRO: relação "tb_premissa" LINE 1: SELECT *
FROM tb_premissa
Does anyone have an idea?
Just started using SnowFlake and experiencing issues making a basic snowflake insert builder procedure where if the insert parameter on call is process with a "''" it will error. In the example below.
SQL compilation error: error line 1 at position 55 N"'TEST'"
The procedure:
CREATE OR REPLACE PROCEDURE "INSERTTABLECOLUMNS"("TABLENAME" VARCHAR(250), "INSERTCOLUMNS" VARCHAR(250), "INSERTVALUES" VARCHAR(250))
RETURNS VARCHAR()
LANGUAGE JAVASCRIPT
AS
$$
var command = "INSERT INTO "+TABLENAME+" ("+INSERTCOLUMNS+") VALUES ("+INSERTVALUES+")";
var cmd1_dict = {sqlText: command};
var stmt = snowflake.createStatement(cmd1_dict);
var rs = stmt.execute();
return "Works";
$$;
The Call
call DEMO.PUBLIC.INSERTTABLECOLUMNS('TESTBEGIN','NAME',"'TEST'")
call DEMO.PUBLIC.INSERTTABLECOLUMNS('TESTBEGIN','NAME,COLUMN2',"'TEST',2")
the double quotes are mostly used for signifying object names when you have abnormal characters in your table/column names, for example.
I think what you want is to escape your ' characters. You can do that either with a double apostrophe: '' or a backslash: \' .
call DEMO.PUBLIC.INSERTTABLECOLUMNS('TESTBEGIN','NAME','''TEST''')
call DEMO.PUBLIC.INSERTTABLECOLUMNS('TESTBEGIN','NAME,COLUMN2','''TEST'',2')
Or
call DEMO.PUBLIC.INSERTTABLECOLUMNS('TESTBEGIN','NAME','\'TEST\'')
call DEMO.PUBLIC.INSERTTABLECOLUMNS('TESTBEGIN','NAME,COLUMN2','\'TEST\',2')
Hi I'm trying to create tables in sqlite via sqlite3 in python3.
The code I got so far is:
import sqlite3
text="hello"
db = sqlite3.connect('C:/DB.db')
c = db.cursor()
c.execute("CREATE TABLE %s (sida)" % name)
for i in range(10):
c.execute("INSERT INTO %s VALUES (%s)" % (name, str(text))) # This does not work
db.commit()
db.close()
c.close
Changing the value from str(text) to i works;
c.execute("INSERT INTO %s VALUES (%s)" % (name, i)) # This works
Passing an integer as the value works but not a string. I've been trying to find a solution but I'm stuck.
The error I get is:
c.execute("INSERT INTO %s VALUES (%s)" % (name, str(text)))
sqlite3.OperationalError: near "a2": syntax error
You should be using a prepared statement for your inserts, binding values to any placeholders which exist:
c = db.cursor()
c.execute("CREATE TABLE %s (sida)" % name)
for i in range(10):
c.execute("INSERT INTO %s VALUES (?)" % name, (str(text),))
db.commit()
db.close()
c.close
Actually, it would also be better to not make the table name itself dynamic.
I am trying to execute SQL query from a file in Python 2.7.13 and getting the following error while displaying the resultset.
The SQL statements in the file are simple like count(*) from table but if this logic works I need to replace it with complex queries.
Error
Info : (7,)
Traceback (most recent call last):
File "SQLserver_loop.py", line 19, in <module>
fields = c.fetchall()
File "pymssql.pyx", line 542, in pymssql.Cursor.fetchall (pymssql.c:9352)
pymssql.OperationalError: Statement not executed or executed statement has no re
sultset
Python Script:
import pymssql
conn = pymssql.connect(
host=r'name',
user=r'user',
password='credential',
database='Test')
c = conn.cursor()
fd = open('ZooDatabase.sql', 'r') # Open and read the file as a single buffer
sqlFile = fd.read()
fd.close()
sqlCommands = sqlFile.split(';') # all SQL commands (split on ';')
for command in sqlCommands: # Execute every command from the input file
c.execute(command)
fields = c.fetchall()
for row in fields:
print "Info : %s " % str(row)
c.close()
conn.close()
Error Message
**SQL File - ZooDatabase.sql**
select count(*) from emp2;
select count(*) from emp1;
**Error Log with SQL print statement output:**
C:\Python27\pycode>python SQLserver_loop.py
SELECT count(*) FROM emp2
Info : (7,)
SELECT count(*) FROM emp1
Info : (7,)
Traceback (most recent call last):
File "SQLserver_loop.py", line 20, in <module>
fields = c.fetchall()
File "pymssql.pyx", line 542, in pymssql.Cursor.fetchall (pymssql.c:9352)
pymssql.OperationalError: Statement not executed or executed statement has no re
sultset
fields = c.fetchall() was causing the error I commented it and works fine now.
for command in sqlCommands:
#print command
c.execute(command)
#fields = c.fetchall()
for row in c:
print (row)
I have a function in PostgreSQL database. However, every time I run the function in PostgreSQL, it auto-adda the character s behind of the query, therefore, I receive an error when it executes.
An example query ends up looking like this:
WHAT IN HERE: query SELECT uom_id FROM ps_quantity where id = 11s
My version is PostgreSQL 9.2.13. How can I solve this? Thanks.
CREATE OR REPLACE FUNCTION select_field(
selected_table text,
selected_field text,
field_type_sample anyelement,
where_clause text DEFAULT ''::text)
RETURNS anyelement AS
$BODY$ DECLARE
-- Log
ME constant text := 'selected_field()';
-- Local variables
_qry varchar := '';
_result_value ALIAS FOR $0;
where_clause1 varchar := 'asdasdsad';
BEGIN
RAISE NOTICE 'FUNCTION: SELECT_FIELD';
-- BANGPH
RAISE NOTICE 'BANGPH - CHANGE 11s to 11';
_qry := 'SELECT uom_id FROM ps_quantity where id = 11';
RAISE NOTICE 'WHERE = %s', where_clause1;
RAISE NOTICE 'WHAT IN HERE: query %s', _qry;
As the manual explains:
instead of:
RAISE NOTICE 'WHAT IN HERE: query %s', _qry;
you need to use:
RAISE NOTICE 'WHAT IN HERE: query %', _qry;
Placeholders for the RAISE statement don't have a "type modifier", it's a plain %