What would be the equivalent of psycopg's cur.mogrify
on mysql?
From: http://initd.org/psycopg/docs/cursor.html
mogrify(operation[, parameters]) Return a query string after arguments binding. The string returned is exactly the one that would be sent to the database running the execute() method or similar.
>cur.mogrify("INSERT INTO test (num, data) VALUES (%s, %s)", (42, 'bar'))
>"INSERT INTO test (num, data) VALUES (42, E'bar')"DB API extension The mogrify() method is a Psycopg extension to the DB API 2.0.
Thanks in advance.
mogrify() method is used to create a formatted SQL to insert values into the table. Cursor. mogrify() gives a bytes string, but we want it to be in string format, thus we only need to use the decode('UTF-8') technique to decode the output of mogrify back to a string.
A cursor is an object which helps to execute the query and fetch the records from the database. The cursor plays a very important role in executing the query. This article will learn some deep information about the execute methods and how to use those methods in python.
A cursor encapsulates a SQL query and returning results. To make a new cursor you should call cursor() on your database: db=apsw. Connection("databasefilename") cursor=db.
class cursor. Allows Python code to execute PostgreSQL command in a database session. Cursors are created by the connection. cursor() method: they are bound to the connection for the entire lifetime and all the commands are executed in the context of the database session wrapped by the connection.
The mogrify () method is a Psycopg extension to the DB API 2.0. This method is exposed in compliance with the DB API 2.0. It currently does nothing but it is safe to call it. Results retrieval methods The following methods are used to read data from the database after an execute () call.
But There’s a Workaround Unfortunately, mogrify is not a method defined by the Python DB API, but instead an add-on of the Psycopg driver. If you're using MySQL, you have a workaround to this problem so that you can see the actual query:
The basic Psycopg usage is common to all the database adapters implementing the DB API 2.0 protocol. Here is an interactive session showing some of the basic commands: The main entry points of Psycopg are: The function connect () creates a new database session and returns a new connection instance.
the server version is at least PostgreSQL 9.3 ( server_version must be >= 90300 ). If Psycopg was built with 64 bits large objects support (i.e. the first two contidions above are verified), the psycopg2.__version__ constant will contain the lo64 flag.
As per this example, you can see the results of the "mogrify" after the statement has been executed.
import MySQLdb
conn = MySQLdb.connect()
cursor = conn.cursor()
cursor.execute('SELECT %s, %s', ('bar', 1))
print cursor._executed
Which will print
SELECT 'bar', 1
Moreover, you don't need to use mogrify before execution
query = cursor.mogrify('SELECT %s, %s', ('bar', 1))
cursor.execute(query)
You can simply do the following
cursor.execute('SELECT %s, %s', ('bar', 1))
This will work across all DBs that use the Python DB API
MySQL doesn't have anything built in that returns a query string like this. If you want to perform a query with variables substituted in, you use PREPARE
and EXECUTE:
PREPARE stmt AS "INSERT INTO test(num, data) VALUES (?, ?)";
EXECUTE stmt USING 42, 'bar';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With