Header Ads Widget

Responsive Advertisement

Create Database and Table in database using Python program.


In this section we will learn how to create database and how to create table in database using python program. First we will create connection between database and python. User is root and password of mysql is 0000 and host must be localhost.  We have to import python library in python program.We need some more function to create connection.


PYTHON PROGRAM TO CREATE DATABASE and CREATE TABLE

import mysql.connector
#establishing the connection
conn = mysql.connector.connect(user='root', password='0000', host='localhost')

if conn.is_connected():

      print("Your program successfully connected with mysql")
      cursor = conn.cursor()
      cursor.execute("Create database mydb5") 
      cursor.execute("use mydb5")
     #print("database created successfully")
     sql ="""CREATE TABLE EMPLOYEE2(FIRST_NAME CHAR(20) NOT NULL,
     LAST_NAME CHAR(20),AGE INT,SEX CHAR(1),INCOME INT)"""
     cursor.execute(sql)
else:
    print("Your program is not connected with mysql")
   #Creating a cursor object using the cursor() method
   #Creating table as per requirement
   #Closing the connection

conn.close()
















mysql connection with python How do I connect MySQL with Python? Can we use Python with MySQL? How do you connect to a database in Python? What does MySQL connector connect () do? how to store list in mysql using python how to insert data into table using python and mysql python mysql executemany how to insert dynamic values in mysql using python python mysql insert multiple rows at once how to insert date in mysql using python cursor.execute python mysql insert query in python with variables

Post a Comment

0 Comments