I am inserting array values to mysql but it throws some error.
Could not find where does the error occured and where is the error.
Error:
TypeError: 'Connection' object does not support indexing
Where is the error and what is the fix?
Your db variable contains a Connection object instance. This Connection objects are not indexable, so db[0]....db[n] will not provide any values to be inserted in the SQL query.
You probably meant this
db1[1],db1[2]
Instead of this
db[1],db[2]
Related
I want to send strings to my SQL server via pyodbc.
Some of my strings are conaining Character like "Ä,Ü,Ö,ã,ó".
While trying to send them i got an Error saying ="quotation mark after the character..."
I Read that i have to use "connection.setencoding(encoding='utf-8') and connection.setdecoding(encoding='utf-8')"
and here comes my Error.
After starting my script i get the following Error:
"Attribute Error: 'pyodbc.Connection' object has no attribute ' setencoding'"
here is my Connections String:
cnxn = pyodbc.connect('DRIVER={FreeTDS},Server=-Serverip-;PORT=1433;DATABASE=-DataBase-;UID=-UID-;PWD=-PWD-;TDS_Version=7.2;charset=utf8')
cnxn.setencoding(encoding='utf-8')
Anyone an idea why im getting this kind of error?
or is there any other way?
I'm using MySQL connector in Python and trying to insert an integer data, but I keep getting this error:
mysql.connector.errors.ProgrammingError: 1064 (42000): 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
My code looks like this:
medDosage = int(''.join(filter(str.isdigit, '42mg')))
myCursor.execute("INSERT INTO dosage (dosageDesc) VALUES (%s)", medDosage)
db.commit()
And this statement has been working just fine for all other variables, and somehow for INT value, it does not work. I tried inserting the string variable instead of int, but doesn't work. Also tried to convert the value such as int(medDosage) to make sure it's the right type, but still doesn't work. I know my syntax is correct so I cannot really understand the error. Can you please help why this error is showing?
Thank you in advance.
You need to ensure the last argument is a tuple:
myCursor.execute("INSERT INTO dosage (dosageDesc) VALUES (%s)", (medDosage,))
I'm getting this error when using Python's MySQL Connector library:
Incorrect date value: 'STR_TO_DATE('2017-10-19T16:57:56Z','%Y-%m-%d%#%H:%i:%s%#')' for column 'estimated_delivery'
Essentially I'm using this matching string: %Y-%m-%d%#%H:%i:%s%#
For this input: 2017-10-19T16:57:56Z
I'm confused about where this error possibly could be =/
Ah I see, I was inserting this STR_TO_DATE function into the SQL statement as a parameter to the execute function. This obviously made it inserted as a string instead of SQL syntax, silly mistake on my part.
I am querying the table using session.query().filter. I get an output like:
(u'1234'). But when I try to translate this using str.translate(None, '5), it throws an error
KeyedTuple' object has no attribute 'translate'
Where am I going wrong?
You get a tuple with one element. You have to retrieve this one element first before using result_string.translate on it.
If you save the return of your session.query().filter inside a variable result do
if result: # Check that result is not empty
result[0].translate(...)
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