Interface Python With MySQL
Interface Python With MySQL
MYSQL
Or Any valid
identifier
import mysql.connector as ms
Example :
if mycon.is_connected():
print(“Successfully connected to MYSQL database”)
Example: To establish connection with MySQL
❑ SYNTAX
Cursor_name.execute(query)
For e.g.
mycursor.execute(“ create table emp(empno
int,name varchar(30),dept varchar(30),salary float)” )
Example: To create a table in mysql through python code
Example: To insert rows into emp table in mysql through python code
Table to work (emp)
Example: To see the content of the table
Output shows cursor is created and query is fired and stored, but no data is
coming. To fetch data we have to use functions like fetchall(), fetchone(),
fetchmany() are used
Step 6: Extract Data from Resultset
(i) <data>=<cursor>.fetchall()
(ii) <data>=<cursor>.fetchone()
(iii) <data>=<cursor>.fetchmany(<n>)
(iv) <variable>=<cursor>.rowcount
Step 6:Fetching(extracting) data from ResultSet
fetchall() : it will return all the records in the form of
tuple.
fetchone() : it return one record from the result set. i.e.
first time it will return first record, next time it will return
second record and so on. If no more records it will
return None
fetchmany(n) : it will return n number of records. If no
more records it will return an empty tuple.
rowcount : it will return number of rows retrieved from
the cursor so far.
Example 1 – fetchall()
Example 2 – fetchall()
Example 3: fetchone()
Example 4: fetchmany(n)
Step 7: Clean Up the Environment
Syntax :
<connection_object>.close()
Example:
mycon.close()
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()
In this method in place of %s we will use {} and
to pass values for these placeholder format() is
used. Inside we can optionally give 0,1,2…
values for e.g.
{0},{1} but its not mandatory. we can also
optionally pass named parameter inside {} so
that while passing
Values through format function we need not
to remember the order of value to pass. For e.g.
{roll},{name} etc.,
str.format() is one of the string formatting methods in Python3,
which allows multiple substitutions and value formatting. This
method lets us concatenate elements within a string through
positional formatting.
Using a Single Formatter :
Formatters work by putting in one or more replacement fields and
placeholders defined by a pair of curly braces { } into a string and
calling the str.format().
The value we wish to put into the placeholders and concatenate
with the string passed as parameters into the format function.
Syntax : { } .format(value)
Parameters :
(value) : Can be an integer, floating point numeric constant, string,
characters or even variables.
Returntype : Returns a formatted string with the value passed as
parameter in the placeholder position.
Example:
Output :
Welcome, A computer science portal for beginners.
This article is written in Python
Hello, I am 18 years old!
String template with {} and format()
String template with {} and format()
Alternate code :
format(s,d) or format(sales,1)
Inserting data in MySQL table from Python