Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of psycopg curs.mogrify on mysql?

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.

like image 731
thclpr Avatar asked Jan 15 '14 12:01

thclpr


People also ask

What is Mogrify SQL?

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.

What is cursor in python MySQL?

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.

What is cursor execute in SQL?

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.

What is Psycopg cursor?

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.

What is Mogrify () method in psycopg?

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.

Can I use Mogrify with Python DB?

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:

What are the basic psycopg commands?

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.

What version of PostgreSQL does psycopg support?

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.


2 Answers

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

like image 110
aydow Avatar answered Oct 03 '22 02:10

aydow


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';
like image 24
Barmar Avatar answered Oct 03 '22 02:10

Barmar