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.
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.
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".
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.
found it here:
http://dev.mysql.com/doc/connector-python/en/connector-python-example-cursor-transaction.html
the same would be for querying too..
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