Psycopg2 - not all arguments converted during string formatting - python-3.x

I'm trying to insert the value of the variable test_text into a Postgres 9.6 database, each time the database_insert function is triggered.
I'm using Python 3.6 and psycopg2 v 2.7
If I use the below code without the placeholder: e.g replace %s with 'test' and remove , (test_text) - it works as I would expect...
def database_insert(update):
test_text = 'This is some test text'
with psycopg2.connect("DB CONNECTION DETAILS ARE HERE'") as conn:
cur = conn.cursor()
cur.execute("INSERT INTO test VALUES(%s);", (test_text))
conn.commit()
cur.close()
conn.close()
However when the function trys to insert the value of the test_text variable using the %s placeholder, I get the error below...
cur.execute("INSERT INTO test VALUES(%s);", (test_text))
TypeError: not all arguments converted during string formatting
Any help on where I am going wrong with this will be much appreciated!

There's a subtle issue here.
You need a comma to make a tuple not just the parens/brackets.
So simply change to:
cur.execute("INSERT INTO test VALUES(%s);", (test_text,))
And you should be good!

Related

Python3 Sqllite Normal query works but not while passing it a tuple

I have a Db from where I have to display all the columns which match the substring of the column given by user.
The following code works:
c.execute("select *from Transactions where Description like '%sh%' ")
conn.commit()
print(c.fetchall())
conn.close()
But when I try to run this code it returns me an empty list:
def search(col,val):
conn = sqlite3.connect('test.db')
c = conn.cursor()
c.execute("Select *from Transactions where ? Like ? ",(col,'%'+val+'%'))
print(c.fetchall())
search('description',"sh")
Also the result will always be a blank list even if the col name is wrong. as opposed the usual error which says column not found.
Please Help

How to add ( and ) in Python result

I am new Python3 so for give me for asking such question but I couldn't find answer on Google, I have Python scanning a file directory and I need to add in an open and close bracket and a new var into the Python result, so that I can insert it into the database.
Mysql requires inserts to be wrapped in brackets in the val =[('test.mp4',newip) ] This works as I get 1 was inserted when I run the hard coded script.
So what I am trying to archive is to modify the result of the scan and add the following
open/close brackets and the new newip into the result of the scan like the following example
Scan result
['test.mp4', 'test_2.mp4', 'test_3.mp4', test_4.mp4']
Insert new result (modified)
[('test',newip), ('test_2.mp4',newip), ('test_3.mp4',newip), ('test_4.mp4',newip)]
When hard coded its works
root#ubuntu:~# python3 testscan.py
['test.mp4', 'test_2.mp4', 'test_3.mp4', test_4.mp4']
1 was inserted.
Please can anyone advise how to achieve this, below is the full code
import os, mysql.connector, re, uuid
files = [f.name for f in (os.scandir('/var/www/html/media/usb1')) if f.name.endswith('.mp4')]
print(files)
newip = (':'.join(re.findall('..', '%012x' % uuid.getnode())))
mydb = mysql.connector.connect(
host="127.0.0.1",
user="user",
password="password",
database="database"
)
mycursor = mydb.cursor()
sql = "INSERT IGNORE INTO files (file,ip) VALUES (%s,%s)"
val =[('test.mp4',newip)]
mycursor.executemany(sql, val)
mydb.commit()
print(mycursor.rowcount, "was inserted.")
So if you want to add the newip to the scan you can use a list comprehension:
files = ['test.mp4', 'test_2.mp4', 'test_3.mp4', 'test_4.mp4']
sql_values = [(file, newip) for file in files]
the result looks like this:
[('test.mp4', newip), ('test2.mp4', newip), ('test3.mp4', newip), ('test4.mp4', newip)]

Deleting a sqlite3 database Row in python

I have been reading a number of threads on how to accomplish this but for some reason it is not working.
I need to delete a Row from a database using a string variable from an entry widget as the "WHERE (variable name)=" is used in the DB query.
The entry widget data is stored as Snippet_Name and the same name is being used as a column name in the DB.
The database has 7 columns but I am only using the 1 column for the query and I want to delete the complete row which contains the entry variable. I have tried variations of DELETE with no success.
The code being used is:
def delete_code():
try:
snippetname = Snippet_Name.get()
sql_delete_query = ('DELETE FROM Code WHERE Snippet_Name = "?"', (snippetname))
c.execute(sql_delete_query)
conn.commit()
except:
messagebox.showerror('PYSnippet', 'Failed to delete record')
A little help and hint would be appreciated.
I went over the query and found 2 errors that needed to be address to correct the problem. The first problem was I had the ? enclosed in quotations which should not be there. Second problem was forgetting to a comma to the variable.
def delete_code():
try:
snippetname = Snippet_Name.get()
sql_delete_query = ('DELETE FROM Code WHERE Snippet_Name = ?' (snippetname,))
c.execute(sql_delete_query)
conn.commit()
except:
messagebox.showerror('PYSnippet', 'Failed to delete record')

Python insert string with random mixture of " and ' to database

I'm running the linux terminal command 'strings' on a file and storing the result, which is a series of readable strings, in a variable. The results are also written to a text file. I then need to upload the results to a database. The problem is that the result often contains ' and " characters in an unpredictable order. These cause an SQL error. I've tried to string.replace with an empty string and a \ escape. I've also tried """ or ''' round the string but neither work as I don't know which type of quotation mark will be first. Any suggestions would be appreciated.
fileName = filePath.rsplit('/', 1)[1]
stream = os.popen('strings ' + filePath)
output = stream.readlines()
file = open(filePath + "/" + fileName + "_StringsResults.txt", "w+")
for o in output:
file.write(str(o))
results += str(o)
file.close()
dsn = 'postgresql://############localhost:########/test?sslmode=disable'
conn = psycopg2.connect(dsn)
with conn.cursor() as cur:
cur.execute(
"UPSERT INTO test (testID) VALUES ('%s')" % (results))
conn.commit()
yes that worked, thanks a million. For anyone else who's interested the solution was roughly:
query = """UPSERT INTO test (testID) VALUES (%s)"""
#Connection code etc.
with conn.cursor() as cur:
cur.execute(query, [results])
conn.commit()
The [] round the parameter was necessary to avoid a type error.

Python SQL Selecting Data

I'm trying to test a small piece of code I wrote to search a column of a table for a specific string. The code should search the Users column of the LoginInfo table and print out the result. I have tried two versions of this code but both get different errors.
VERSION ONE:
import sqlite3
import sys
Username = "('Hello1',)"
print(Username)
connection= sqlite3.connect("Logininfo.db")
c = connection.cursor()
c.execute('''SELECT Username FROM LoginInfo WHERE Username=Hello1''')
for row in c :
print(row)
VERSION ONE ERROR:
c.execute('''SELECT Username FROM LoginInfo WHERE Username=Hello1''')
sqlite3.OperationalError: no such column: Hello1
VERSION TWO:
import sqlite3
import sys
Username = "('Hello1',)"
print(Username)
connection= sqlite3.connect("Logininfo.db")
c = connection.cursor()
c.execute("SELECT Username FROM LoginInfo WHERE Username="Hello1"")
for row in c :
print(row)
VERSION TWO ERROR:
Invalid syntax error pop up highlighting Hello1
Any help would be appreciated as I'm really new at this.
You have problems with your syntax in both cases. When you send a string into cursor.execute you are sending the DB a complete command. This means strings need to be quoted correctly, SQL syntax needs to be correct, etc.
This command: SELECT Username FROM LoginInfo WHERE Username=Hello1 means:
"Give me the Username column from the LoginInfo table where Username matches the Hello1 column"
On the other hand this command: SELECT Username FROM LoginInfo WHERE Username='Hello1' (note the quotes) means:
"Give me the Username column from the LoginInfo table where Username = Hello1"
Your second attempt is simply invalid Python. You should be using triple quotes on the outside or escaping the quotes that are part of the query.

Resources