Final Revised MySQL Connectivity
Final Revised MySQL Connectivity
www.knowpythonbytes.blogspot.com
PYTHON-MYSQL
CONNECTIVITY
4. Switch on internet connection MySQL alone has the following interface modules
to choose:
5. On the command prompt type MySQL for Python (import MySQLdb)
MySQL Connector/Python (import mysql.connector)
pip install mysql-connector-python etc
and execute. But, remember to enter Download and install MySQL Connector/Python
inside the Python scripts folder before https://dev.mysql.com/downloads/connector/python/
3
FETCH DATA FROM DATABASE
Multiple ways to retrieve data:
fetchall()
Fetch all (remaining) rows of a query result, returning them as a sequence of sequences (e.g. a
List of Tuples). Fetchall( ) returns all the records/rows from the result set in the form of a
sequence.
fetchmany(size)
Fetch the next set of rows of a query result, returning a sequence of sequences. It will return
number of rows that matches to the size argument. fetchmany() returns a list of
tuples. If no more rows are available, it returns an empty list.
fetchone()
it returns only one record/row from the result set. i.e. first time it will return first record,
next time it will return second record and so on. fetchone() returns a single
sequence, or None if no more rows are available
▪ rowcount()
▪ The rowcount is a property of cursor object that returns the number of rows retrieved from the
cursor so far. Rowcount() returns the number of rows affected by the
last execute method for the same cur object and thus it returns -1
if no execute() method is done prior to that. i.e. the result set has not
been created yet.
OUTPUT ON THE SHELL
OUTPUT ON THE SHELL
A Sequence(LIST) of Sequences(Tuples)
OUTPUT ON THE SHELL
OUTPUT ON THE SHELL
BEFORE EXECUTING THE PYTHON CODE AFTER EXECUTING THE PYTHON CODE
AFTER EXECUTING
THE PYTHON CODE
https://dev.mysql.com
https://google.com
And other websites