15-MySQL Connectivity cs 12
15-MySQL Connectivity cs 12
15-MySQL Connectivity cs 12
Example:
import mysql.connector
Or
import mysql.connector as ms
TO CREATE CURSOR
Cursor_name = connectionObject.cursor()
For e.g.
mycursor = mycon.cursor()
Step 4: Executing Query and fetching records
TO EXECUTE QUERY
We use execute() function to send query to connection
Cursor_name.execute(query)
For e.g.
mycursor.execute("select * from emp‟)
nrec is
working as
record set
Example 2 – fetchall()
Example 3 – fetchall()
Example 4: fetchone()
Example 5: fetchmany(n)
Parameterized Query
We can pass values to query to perform
dynamic search like we want to search for any
employee number entered during runtime or
to search any other column values.
To Create Parameterized query we can use various
methods like:
Concatenating dynamic variable to query in which
values are entered.
String template with % formatting
String template with {} and format function
Concatenating variable with query
String template with %s formatting
In this method we will use %s in place of values to
substitute and then pass the value for that place.
String template with %s formatting
String template with {} and format()
Showing list of
databases
We can also create our database through
python
Creating
Database
See a
newdatabase
telephonedir
is created
output
Creating a table ‘customer’
Creati
ng
table
Displaying
list of
tables
OUTPUT
Inserting data into table customer
Inserti
ng a
record
Inserting records by taking its input by user
OUTPU
T
Inserting many
records by
creating list of
tuples
34
Code to update record
Name of 1003
customer is
changed to
Sudha
Deleting record: lets delete the record
Record with
cust_id
1006 is
deleted now