python-db-api

Escape SQL “LIKE” value for Postgres with psycopg2

为君一笑 提交于 2019-11-26 18:09:39
问题 Does psycopg2 have a function for escaping the value of a LIKE operand for Postgres? For example I may want to match strings that start with the string "20% of all", so I want to write something like this: sql = '... WHERE ... LIKE %(myvalue)s' cursor.fetchall(sql, { 'myvalue': escape_sql_like('20% of all') + '%' } Is there an existing escape_sql_like function that I could plug in here? (Similar question to How to quote a string value explicitly (Python DB API/Psycopg2), but I couldn't find

Python MySQL Connector database query with %s fails

让人想犯罪 __ 提交于 2019-11-26 02:10:57
I have a basic program that is supposed to query a database that contains user information. I am trying to select the information for a specific user and print it out to the console. Here is my code: import mysql.connector funcon = mysql.connector.connect(user='root', password='pass', host='127.0.0.1', database='fundata') funcursor = funcon.cursor() query = ("SELECT * FROM funtable WHERE userName=%s") uName = 'user1' funcursor.execute(query, uName) for (userName) in funcursor: print("{}".format(userName)) I have the username stored in a variable because later I plan on getting the user name

Python MySQL Connector database query with %s fails

眉间皱痕 提交于 2019-11-26 01:09:19
问题 I have a basic program that is supposed to query a database that contains user information. I am trying to select the information for a specific user and print it out to the console. Here is my code: import mysql.connector funcon = mysql.connector.connect(user=\'root\', password=\'pass\', host=\'127.0.0.1\', database=\'fundata\') funcursor = funcon.cursor() query = (\"SELECT * FROM funtable WHERE userName=%s\") uName = \'user1\' funcursor.execute(query, uName) for (userName) in funcursor: