global name 'unhex' is not defined - python

I am working on flask and on executing this statement
salt=os.urandom(32)
db_session.execute('insert into posUsers values (?, ?, ?, ?)',[usern,unhex("sjg"),salt,row.clientId])
its giving this error that unhex is not defined!
Update- Now I changed to this
db_session.execute('insert into posUsers (username,passwd,salt,clientId ) values (?,UNHEX("6568"),UNHEX("haf"), ?)',[usern,row.clientId])
its giving a new error 'list' object has no attribute 'keys'
Any solutions ?

This is because when you are passing it to an python list it is treated as an python function which doesnot exist.
You need to include it in sql query string.
salt=os.urandom(32)
db_session.execute('insert into posUsers values (?,unhex("sjg"),?, ?)',[usern,salt,row.clientId])
unhex is a mysql function and it is being treated as python function resulting in error

Related

psycopg2 TypeError: not all arguments converted during string formatting

I'm trying execute a simple query, but getting this error no matter how I pass the parameters.
Here is the query (I'm using Trac db object to connect to a DB):
cursor.execute("""SELECT name FROM "%s".customer WHERE firm_id='%s'""" % (schema, each['id']))
schema and each['id'] both are simple strings
print("""SELECT name FROM "%s".customer WHERE firm_id='%s'""" % (schema, each['id']))
Result:
SELECT name FROM "Planing".customer WHERE firm_id='135'
There is on error is a remove quote after firm_id=, but that way parameter is treated a an integer and ::text leads to the very same error.
In my case I didn't realize that you had to pass a tuple to cursor.execute. I had this:
cursor.execute(query, (id))
But I needed to pass a tuple instead
cursor.execute(query, (id,))
I got this same error and couldn't for the life of me work out how to fix, in the end it was my mistake because I didn't have enough parameters matching the number of elements in the tuple:
con.execute("INSERT INTO table VALUES (%s,%s,%s,%s,%s)",(1,2,3,4,5,6))
Note that I have 5 elements in the values to be inserted into the table, but 6 in the tuple.
It is recommended to not use string interpolation for passing variables in database queries, but using string interpolation to set the table name is fine as long as it's not an external input or you restrict the allowed value. Try:
cursor.execute("""
SELECT name FROM %s.customer WHERE firm_id=%%s
""" % schema, (each['id'],))
Rules for DB API usage provides guidance for programming against the database.
Use AsIs
from psycopg2.extensions import AsIs
cursor.execute("""
select name
from %s.customer
where firm_id = %s
""",
(AsIs(schema), each['id'])
)
You could try this:
cursor.execute("INSERT INTO table_name (key) VALUES(%s)",(value1,))
You will get an error if you are missing a (,) after value1.
The correct way to pass variables in a SQL command is using the second argument of the execute() method. And i think you should remove single quotes from second parameter, read about it here - http://initd.org/psycopg/docs/usage.html#the-problem-with-the-query-parameters.
Note that you cant pass table name as parameter to execute and it considered as bad practice but there is some workarounds:
Passing table name as a parameter in psycopg2
psycopg2 cursor.execute() with SQL query parameter causes syntax error
To pass table name try this:
cursor.execute("""SELECT name FROM "%s".customer WHERE firm_id=%s""" % (schema, '%s'), (each['id'],))
Every time I have this kind of error, I am passing the wrong amount of values. Try check it

sqlite3. ValueError: operation parameter must be str or unicode

I used the command below in my python code to update the data base
"""UPDATE dbtest SET id = ?, value = ? WHERE name=? ,("%s", "%s","%s")""" (data[0],data[1],data[2] )
data[0],data[1],data[2] where data is a list.
Python shows
ValueError: operation parameter must be str or unicode
when running the script.
I am not able to figure out why the issue happens.
Your values list should be outside the SQL string, e.g.
db.execute("""UPDATE dbtest SET id = ?, value = ? WHERE name=?""", (data[0],data[1],data[2]))

How do I correctly map a Python "list of lists" into an SQL table (built with SQLite3)?

I have data coming out of an XML parser that contains instrument readings. Samples of the data are reproduced below:
cimReading = [['2012-08-15 10:05:13.101485', ['0x46'], ['0x32'], ['1.234'], ['5.678'], ['9.123'],
['4.567'], ['0x98'], ['0x97']],
['2012-08-15 10:05:13.101979', ['0x47'], ['0x33'], ['8.901'], ['2.345'], ['6.789'],
['0.123'], ['0x96'], ['0x95']]]
I'm trying to loop over the list items and put them away in a table, cim76_dmt that was previously defined in Python with sqlite3. Here is the code I'm using to try to do this:
for devList in cimReading:
print devList
cim76TabPopulate.execute('INSERT INTO cim76_dmt VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)',
devList)
BenchDB.commit()
cim76TabPopulate.close()
Here is the error message I receive upon execution:
['2012-08-15 10:40:21.110140', ['0x46'], ['0x32'], ['1.234'], ['5.678'], ['9.123'], ['4.567'], ['0x98'], ['0x97']]
EXCEPTION...
(<class 'sqlite3.InterfaceError'>, InterfaceError('Error binding parameter 1 - probably unsupported type.',), <traceback object at 0x1007a7518>)
So, I'm having a problem mapping a list item to a SQL table field. I think there should be a way to do this, but another possibility may be to tweak the XML parsing so I generate a single list of multiple strings for each record. I'd welcome any suggestions or advice on this.
Thanks in advance!
Your problem is that all the parameters after the first one are lists, not strings or integers. The part win the error message that is complaining about unsupported type is complaining about the data element ['0x46'].
You need to massage your data a little bit more; you want to unpack ['0x46'] to '0x46'. This needs to be done to the other data elements in single length lists in your example data.

Python MySQL insert NULL (None) value error

I am trying to insert a NULL value into a MySQL db int field with a python script. NOT NULL is off on the field so its not that. I have manually inserted a NULL value into database and that worked fine and my code works fine if I put a literal value in the place of None.
I have looked a several people's examples of how to do this and as far as I can tell there is nothing wrong in syntax.
Code:
length2 = None
cursor.execute("INSERT INTO tableName(Length) VALUES(%s)", length2)
Error:
Error 1064: You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near
'%s)' at line 1
Any ideas?
The second argument to execute() should be a tuple (or list).
Please change your code to:
cursor.execute("INSERT INTO tableName (Length) VALUES (%s);", (length2,))

psycopg2 "TypeError: not all arguments converted during string formatting"

I'm trying to insert binary data (a whirlpool hash) into a PG table and am getting an error:
TypeError: not all arguments converted during string formatting
code:
cur.execute("""
INSERT INTO
sessions
(identity_hash, posted_on)
VALUES
(%s, NOW())
""", identity_hash)
I tried adding conn.Binary("identity_hash") to the variable before insertion, but get the same error.
The identity_hash column is a bytea.
Any ideas?
The problem you have is that you are passing the object as second parameter: the second parameters should be either a tuple or a dict. There is no shortcut as in the % string operator.
You should do:
cur.execute("""
INSERT INTO
sessions
(identity_hash, posted_on)
VALUES
(%s, NOW())
""", (identity_hash,))
Encountered the same problem and found that this is actually covered in their FAQ
I try to execute a query but it fails with the error not all arguments
converted during string formatting (or object does not support
indexing). Why? Psycopg always require positional arguments to be
passed as a sequence, even when the query takes a single parameter.
And remember that to make a single item tuple in Python you need a
comma! See Passing parameters to SQL queries.
cur.execute("INSERT INTO foo VALUES (%s)", "bar") # WRONG
cur.execute("INSERT INTO foo VALUES (%s)", ("bar")) # WRONG
cur.execute("INSERT INTO foo VALUES (%s)", ("bar",)) # correct
cur.execute("INSERT INTO foo VALUES (%s)", ["bar"]) # correct
Have you taken a look at the "examples/binary.py" script in the psycopg2 source distribution? It works fine here. It looks a bit different than your excerpt:
data1 = {'id':1, 'name':'somehackers.jpg',
'img':psycopg2.Binary(open('somehackers.jpg').read())}
curs.execute("""INSERT INTO test_binary
VALUES (%(id)s, %(name)s, %(img)s)""", data1)

Categories

Resources