Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

Create MySQL table from Python

import mysql.connector
try:
    connection = mysql.connector.connect(host='localhost',
                                         database='techshop',
                                         user='root',
                                         password='')
    mySql_Create_Table_Query = """CREATE TABLE Mobile ( 
                             Id int(11) NOT NULL,
                             Name varchar(250) NOT NULL,
                             Price float NOT NULL,
                             Purchase_date Date NOT NULL,
                             PRIMARY KEY (Id)) """
    cursor = connection.cursor()
    result = cursor.execute(mySql_Create_Table_Query)
    print("Mobile Table created successfully ")
except mysql.connector.Error as error:
    print("Failed to create table in MySQL: {}".format(error))
finally:
    if connection.is_connected():
        cursor.close()
        connection.close()
        print("MySQL connection is closed")
Source by softhunt.net #
 
PREVIOUS NEXT
Tagged: #Create #MySQL #table #Python
ADD COMMENT
Topic
Name
7+4 =