Update multiple columns with psycopg2 - python

I can I update multiple columns for a particular row with psycopg2? I have tried this but I get the error I show bellow:
cursor = postgres_conn.cursor()
sql_update = "UPDATE table_name SET a=%s, b=%s, c=%s WHERE url=%s"
cursor.execute(sql_update,(1,2,3,url))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
psycopg2.errors.InFailedSqlTransaction: current transaction is aborted, commands ignored until end of transaction block

Related

psycopg2 execute('SELECT version()') failed

hi~ I want to access pgsql through psycopg2, but an error is reported.I use the same command and have no problem in the pgsql.
import psycopg2
connect = psycopg2.connect(database='xxx',
user='xxx',
password='xxx',
host='xxxx',
port=1521)
cursor=connect.cursor()
cursor.execute('SELECT version()')
rows = cursor.fetchall()
for row in rows:
print(row)
Traceback (most recent call last):
File "C:/Users/z30007746/PycharmProjects/untitled3/pt_test.py", line 14, in <module>
cursor.execute('SELECT version()')
psycopg2.errors.SyntaxError: syntax error at end of input
QUERY: DECLARE
BEGIN
CONTEXT: FUNCTION: plspl_anon_func
LINE 1 FROM "CREATE": BEGIN

"not a query" exception while executing query using cx_Oracle in python

I am trying to connect to oracle database and execute a query using cx_Oracle in python with following code
import cx_Oracle
mobileNumber='1234567890'
dbHost='some_value'
dbPortNumber='some_value'
dbUsername='some_value'
dbPasswd='some_value'
dsn_tns = cx_Oracle.makedsn(dbHost, dbPortNumber, service_name='ORCL')
conn = cx_Oracle.connect(user=dbUsername, password=dbPasswd, dsn=dsn_tns)
cur=conn.cursor()
query='insert into table_name values ((select max(ID)+1 from table_name),"sms","'+ mobileNumber + '")'
cur.execute(query)
res=cur.fetchall()
print (res)
But I am getting following exception when running this code
Traceback (most recent call last):
File "script.py", line 16, in <module>
result=cur.fetchall()
cx_Oracle.InterfaceError: not a query
When printing the value of variable query I am getting following
insert into table_name values ((select max(ID)+1 from table_name),"sms","1234567890")

Cannot fetch data from cassandra using cqlengine python model :

I am trying to use the cassandrapython cqlengine for accessing the cassandra db, I was able to filter when the columns are not of list type.
I get the following error message :
d =cClass().filter(lastname='text',age=2,input__contains='a')
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/cqlengine-0.21.0-py3.4.egg/cqlengine/operators.py", line 43,
in get_operator KeyError: 'CONTAINS' During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.4/dist-packages/cqlengine-0.21.0-py3.4.egg/cqlengine/models.py", line 562, in filter
File "/usr/local/lib/python3.4/dist-packages/cqlengine-0.21.0-py3.4.egg/cqlengine/query.py", line 507, in filter
File "/usr/local/lib/python3.4/dist-packages/cqlengine-0.21.0-py3.4.egg/cqlengine/operators.py", line 45,
in get_operator **cqlengine.operators.QueryOperatorException: contains doesn't map to a QueryOperator**
It looks like you're using a very old (and deprecated) version of cqlengine. If you can, upgrade to the version integrated with the DataStax driver, where the API has supported contains since version 3.1.0.

Why am I getting "FROM expression expected" from SQLAlchemy when I'm trying to update a row in a table?

I'm trying update a row in a table but I'm getting an Argument Error.
Here is the code:
inventory_host = InventoryHost(ipv4_addr=ipv4, ipv6_addr=ipv6, macaddr=mac, host_name=name)
try:
session.add(inventory_host)
session.commit()
except sqlalchemy.exc.IntegrityError:
session.execute(update(inventory_host))
session.commit()
session.close()
This is the error I'm getting:
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "perception.py", line 77, in <module>
main()
File "perception.py", line 66, in main
modules.nmap_parser.parse_nmap_xml(nmap_xml)
File "/Users/arozar/Documents/Scripts_Code/Python-Projects/perception/modules/nmap_parser.py", line 128, in parse_nmap_xml
session.execute(update(inventory_host))
File "<string>", line 2, in update
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/sqlalchemy/sql/dml.py", line 668, in __init__
ValuesBase.__init__(self, table, values, prefixes)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/sqlalchemy/sql/dml.py", line 183, in __init__
self.table = _interpret_as_from(table)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/sqlalchemy/sql/selectable.py", line 41, in _interpret_as_from
raise exc.ArgumentError("FROM expression expected")
sqlalchemy.exc.ArgumentError: FROM expression expected
The session.add(inventory_host) works for new hosts in the inventory_host table, but as soon as I try and update the row using session.execute(update(inventory_host)) I get the error.
update takes a table name as its first arg, not an instance of your table class. What value(s) do you want to update? If you wanted to update the host_name, for example, you might do this instead:
from sqlalchemy import update
# Ideally, just use your primary key(s) in your where clause; I'm not sure what they are
stmt = (update(InventoryHost).where(ipv4_addr=ipv4, ipv6_addr=ipv6, macaddr=mac)
.values(host_name=name) # updates the host_name, for example
session.execute(stmt)
...

redis send_command error not found

Using redis and following the tutorials from http://nullege.com/codes/search/redis.Redis.send_command
When trying this example:
results = r.send_command(
'ZRANGEBYSCORE %s 0 %s LIMIT 0 %s\r\n' % (
qk,
ts,
limit
In my case i am getting:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Redis' object has no attribute 'send_command'
And redis.Redis hasnt method send_command(). This is a method of the Connection class.
https://github.com/andymccurdy/redis-py/blob/master/redis/connection.py

Categories