MySQL Connectivity with Python
MySQL Connectivity with Python
CREATING A TABLE
Inside the .execute() – you just put the normal MySQL query that you would use while
working in MySQL as it is
SHOWING TABLES
OUTPUT
Whenever you’re trying to extract/fetch data from a record, you need to use loop in order
to iterate through the cursor object and print the data stored in it
But you don’t need to use any loops while creating, modifying, adding items, in a relation
(table)
ROWCOUNT attribute:
rowcount attribute gives you the total number of records inserted in the table after the
execution of the command
COMMIT() function:
commit function sends a COMMIT statement to MySQL server which commits
(confirms/locks) the current transaction (insertion/modification of data)
e.g. conobj.commit()
ROLLBACK() function:
reverts the changes made by the current transaction
e.g. conobj.rollback()
AUTOCOMMIT function:
it’s a method to set a value either True or False, which if set to true, auto-commits all the
transaction made
It will print the first 3 records of the table - [(Vaibhav, xii a),(Aliza, xii a),]
• Fetchall(): fetchall, prints all the records that are there in the relation/table. This
also returns a list of tuples. If some rows are already retrieved, using fetchone or
fetchmany, then fetchall returns remaining rows.
Whenever you use any of these commands you need to store it in a variable and then to
print the data, you have to iterate through the variable (basically using a loop, mostly
‘for’)
PARAMETERISED QUERY
Parameterized query is used when you want to deal with dynamic variable while
executing a MySQL command using python. Suppose you want to add the details of a
student in a given table, but the user inputs the value that he wants to add, now in this
case in order to make the program dynamic (usable for any values entered by the user)
we have to use parameterized queries
• Concatentation: only useful when you have the dynamic variable at the end of the
MySQL query.
• String template using %s formatting: useful when multiple variables are dynamic
Now here, whenever you want a dynamic variable, you put % sign and ‘s’ after it, then
after the query, you write %(parameter,parameter), where these parameters are the
dynamic variable that you want to use in the query. Note that the order is important,
meaning the first parameter that you put in %(parameter,parameter) will get linked to
the first ‘%s’ in the query.
Therefore specifically in this example, I have 2 dynamic variables, ‘rno’ and ‘nm’ which
are assigned some certain value. When I put %(nm, rno), nm gets assigned to the first
‘%s’ in the query and rno gets assigned to the second ‘%s’ in the query. Therefore the
resultant query becomes:
OUTPUT
• String template using {} and format: This is also used when there are multiple
dynamic variables involved
This is very similar to %s, only difference is, in place of ‘%s’ you have these curly braces
and in place of %(p1,p2), you have ___ .format(p1,p2,p3,p4) ___ (p = parameter). The
parameters in this formatting are also ordered, meaning p1 will get linked to the first
curly brace, p2 with the second curly brace and so on.
NOTE: You will require to put inverted comma around {} when dealing with string or
date data-type otherwise an error will be raised.
DELETING RECORDS FROM A TABLE
As ‘delete’ is a DML command (data manipulation language) the transaction needs to
be commited therefore, it’s important to use connection_object.commit()
OUTPUT
VERIFCATION
PRACTICE PROBLEM: write a code from scratch to display the first 3 records from the
table ‘student’ in the database ‘school’
PRACTICE PROBLEM: taking input from the user to input the roll_no to update the marks
of the student associated with that roll_no [DON’T FORGET TO USE commit()]
TABLE STRUCTURE:
HINTS:
• Make the connection and cursor object
• Input roll_no from user
• Fetch detail of the inputted roll no
• If no such record found, print “no record(s) found”
• Otherwise, display the found record
• Take input of new marks from the user
• update the detail of that inputted roll no
• Recommended to use {} and format function
PRACTICE PROBLEM: WAP to delete the record of a student on the basis of name
fetched from the user at run-time [DON’T FORGET TO USE commit()] [use either %s or
string formatting {}]