0% found this document useful (0 votes)
8 views

Interface Python With MYSQL - Tutorial - 2

Python

Uploaded by

ritamdawn01
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Interface Python With MYSQL - Tutorial - 2

Python

Uploaded by

ritamdawn01
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

INTERFACE PYTHON WITH MYSQL……….

Tutorial_2
Class – XII – Computer science
Hopefully all of you go through the previous uploaded material and also practiced on your computer system.
So, now you have the idea about interaction between Python and MySQL database.

Now, we move forward on this topic………………………………..

In our last material we only performing simple query to display all records from a table using the command
like :

select * from Student;

now we discuss about Parameterised Queries like

select * from Student where marks > x;

( the value of variable x is given by the user at runtime )

Parameterised Queries:-
You may need to run queries in your Python program which are based on some parameters or values that you
provide from outside.

Like:

select * from Student where marks > x;

( the value of variable x is given by the user at runtime )

Such queries are called parameterised queries. To execute parameterised queries in a mysql.connector
connection, you nedd to form SQL query strings that include values of parameters.

Let us talk about how you can form such query strings:

There are two styles you can use any one of them as follows:

(1) Old Style: String Templates with % formatting:-


Here, string formatting uses this general form: f % v

Where f is a template string and v specifies the value or values to be formatted using that template. If multiple
values are to be formatted then v must be a tuple. For this you can write the SQL query in a string but use a %s
code in place of the value to be provided as a parameter.
for single value to be stored
For example:
in a tuple, comma use to
“select * from Student where marks > %s” %(75,) follow it
Program Coding Example:

Assume that the Student table is exists in the database Myschool ( which we created in MySQL in our previous
material – all programs shown here based on that table)

Program: to display the students details who scored more than 75.

Output:

OR

If you want to store the above query string in variable and then execute that variable through cursor.execute()
method as shown below:

mystr = “select * from Student where marks > %s” %(75,)

cursor.execute(mystr)

Then your program code:


In the similar manner, you may add multiple parameter values, but one caution you must exercise is that do
not forget to enclose palaceholder %s in single quotes for string type parameters.

For example:

mystr = “select * from Student where marks > %s and class = ‘%s’ “ %(75, ‘xi’)

see ‘%s’ enclosed tuple containing a


in quotes for number and a
string value string value

Program Example:

Let the Students table as follows:

Program: to display the student details who scored more than 75 and belongs to class xi

Now, If you want to set user input as follows:

m = int(input(“enter the minimum marks limit :”))

x = input(“enter the class :”)

Then create a tuple first based on user given values as follows:

p = (m, x) # tuple

Then form the query string as follows:

mystr = “select * from Student where marks > %s and class = %s “

(Note: here %s not enclosed in quotes)


Then pass the variable and tuple to the cursor.execute() function as follows:

cursor.execute(mystr, p)

Program Example: To display the student details who scored more than a specified number (given by
user) and belongs to a specified class (given by the user)

Output:

(2) New Style: String Templates with % formatting:-


The new style of creating SQL query strings involves the use of the .format() method of the str type.

for example:

mystr = “select * from Student where marks > {} “ .format(75)

the value 75 actually placed within


curly braces at runtime

OR
mystr = “select * from Student where marks > {} and class = ‘{}’ “ .format(75, ‘xi’)

( Here, 75 place at the first { } and


placeholder enclosed in quotes
xi place at second { } area at runtime )
for string value
OR

mystr = “select * from Student where marks > {0} and class = ‘{1}’ “ .format(75, ‘xi’)

( Here {0} and {1} are the placeholders; values are substituted from (75, ‘xi) ;

75 in place of {0} and ‘xi’ in place of {1} )

Program example:

OR

Output:
Program example – based on user given values at runtime.

Output:

Another example – we supply the values using named arguments. The arguments may be supplied in
any order.

Program example:

Output:
OR

we supply the user given values using named arguments.

For example:-

Output:

INTERFACE PYTHON WITH MYSQL – CONCEPT IS


REQUIRED FOR DEVELOPING YOUR PROJECT – So,

Read the whole thing carefully and practice it……


If any problems then message / call me……………
In the next material I will discuss insert, update and
delete records from python program………..

You might also like