MySQL Python

  • MySQL is a fast, easy to use a relational database. It is currently the most popular open-source database
  • MySQL is used for many small and big businesses. It is developed, marketed and supported by MySQL AB, a Swedish company. It is written in C and C++.
  • MySQL is an open-source database, so you don’t have to pay a single penny to use it.
  • MySQL is a fast, easy to use a relational database.
  • MySQL is used for many small and big businesses.
  • MySQL is an open-source database, so you don’t have to pay for it.
>>> import mysql.connector
>>> import mysql.connector
>>>from mysql.connector import Error
>>> import mysql.connectorTo establish a connection to the database we should know the following parameters,Host= localhost (In general it is same for all)Database=mysql (You can set as per your wish)User=root (It is a username)Password= root@123 (password set by me while installation of MyQL)>>> mysql.connector.connect( host = 'localhost', database = 'mysql', user = 'root', password = 'root@123')
>>> my_database = mysql.connector.connect( host = 'localhost', database = 'mysql', user = 'root', password = 'root@123')>>> cursor = my_database.cursor()>>> cursor.execute( " show databases " )>>> for db in cursor:...  print(db)...
('bank',)('information_schema',)('mysql',)('performance_schema',)('sakila',)('sys',)('world',)>>>
>>> my_database = mysql.connector.connect( host = 'localhost', user = 'root', password = 'root@123' )>>> cursor = my_database.cursor()>>> cursor.execute( " CREATE DATABASE college "  )>>> for db in cursor:...  print(db)...>>> cursor.execute( " show databases " )>>> for db in cursor:...  print(db)...
create table_name(column 1 datatype,column 2 datatype,column 3 datatype,…………………………………………,column n datatype)
>>> my_database = mysql.connector.connect ( host = 'localhost', database = 'college', user = 'root', password = 'root@123' )>>> cursor = my_database.cursor()>>>cursor. execute( " CREATE TABLE students ( stud_id varchar(200), stud_name VARCHAR(215), address VARCHAR(215), city char(100)) " )>>> cursor. execute( " CREATE TABLE department ( dept_id varchar(200), dept_name VARCHAR(215)) " )>>> cursor.execute( "CREATE TABLE faculty (  faculty_id varchar(200),faculty_name VARCHAR(215) )"  )
>>> cursor. execute ( " SHOW TABLES " )>>> for x in cursor:...      print(x)...('department',)('faculty',)('students',)
>>> my_database = mysql.connector.connect ( host = 'localhost', database = 'college', user = 'root', password = 'root@123' )>>> cursor = my_database.cursor()>>>cursor. execute( " CREATE TABLE students2 ( stud_id varchar(200) PRIMARY KEY, stud_name VARCHAR(215), address VARCHAR(215), city char(100)) " )
>>> my_database = mysql.connector.connect ( host = 'localhost', database = 'college', user = 'root', password = 'root@123' )>>> cursor = my_database.cursor()>>>cursor.execute( " ALTER TABLE student ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY " )
>>> cursor.execute("desc students")>>> for x in cursor:...     print(x)...('stud_id', 'varchar(200)', 'YES', '', None, '')('stud_name', 'varchar(215)', 'YES', '', None, '')('address', 'varchar(215)', 'YES', '', None, '')('city', 'char(100)', 'YES', '', None, '')>>>
>>> cursor.execute("desc students2")>>> for x in cursor:...     print(x)...('stud_id', 'varchar(200)', 'NO', 'PRI', None, '')('stud_name', 'varchar(215)', 'YES', '', None, '')('address', 'varchar(215)', 'YES', '', None, '')('city', 'char(100)', 'YES', '', None, '')>>>
>>> my_database = mysql.connector.connect ( host = 'localhost', database = 'college', user = 'root', password = 'root@123' )>>> stm = " INSERT INTO students ( stud_id, stud_name, address, city ) VALUES ('101','Nitin Shelke', 'Congress Nagar', 'Amravati' ) ">>> cursor = my_database.cursor()>>> cursor.execute(stm)
>>> cursor.execute(" select * from students")>>> for x in cursor:...     print(x)...('101', 'Nitin Shelke', 'Congress Nagar', 'Amravati')

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store