Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python mysql connector - What is the right way for multiple queries

import mysql.connector
cnx=mysql.connector.connect(user,password,host,database)
cursor=(cnx.cursor)

and, i am trying to query the database twice with different queries as below

query1result=[]
query2result=[]

cursor.execute(query1)
for each in cursor:
    query1result.append[each]
cursor.execute(query1)
for each in cursor:
    query1result.append[each]

I get an error at python/mysql/connector/cursor.py. line 474

I want to know what is the right way for multiple queries.

like image 519
user2015144 Avatar asked Jul 09 '15 12:07

user2015144


People also ask

How do I pass multiple SQL queries in Python?

A simple way is to execute the query and use fetchall(). This has been already discussed in SET 1. This is a convenience method for executing multiple SQL statements at once. It executes the SQL script it gets as a parameter.

Which of the following is correct to connect MySQL database in python?

Python needs a MySQL driver to access the MySQL database. In this tutorial we will use the driver "MySQL Connector". We recommend that you use PIP to install "MySQL Connector".

What does the Executemany () method do?

executemany() Method. This method prepares a database operation (query or command) and executes it against all parameter sequences or mappings found in the sequence seq_of_params . In Python, a tuple containing a single value must include a comma.


1 Answers

found it here:

http://dev.mysql.com/doc/connector-python/en/connector-python-example-cursor-transaction.html

the same would be for querying too..

like image 159
user2015144 Avatar answered Sep 28 '22 03:09

user2015144