3 Python MySQL Create Table Python MySQL Create Table¶To create a table in MySQL, use the “CREATE TABLE” statement. Make sure you define the name of the database when you create the connection In [4]: import mysql.connector mydb = mysql.connector.connect( host="localhost", user="root", passwd="", database="itronix" ) mycursor = mydb.cursor() mycursor.execute("CREATE TABLE student …
Read More »Python MySQL Insert Into Table
4 Python MySQL Insert Into Table Python MySQL Insert Into Table¶To fill a table in MySQL, use the “INSERT INTO” statement. In [8]: import mysql.connector mydb = mysql.connector.connect( host="localhost", user="root", passwd="", database="itronix" ) mycursor = mydb.cursor() name = input("Enter Name:") address = input("Enter Address:") sql = "INSERT INTO customers (name, address) …
Read More »Python MySQL Select From
5 Python MySQL Select From Python MySQL Select From Table¶To select from a table in MySQL, use the “SELECT” statement: In [11]: import mysql.connector mydb = mysql.connector.connect( host="localhost", user="root", passwd="", database="itronix" ) mycursor = mydb.cursor() mycursor.execute("SELECT * FROM customers") myresult = mycursor.fetchall() for x in myresult: print(x) (1, 'Karan', 'Ludhiana') (2, …
Read More »Python MySQL Where
6 Python MySQL Where Python MySQL Where¶ In [14]: import mysql.connector mydb = mysql.connector.connect( host="localhost", user="root", passwd="", database="itronix" ) mycursor = mydb.cursor() sql = "SELECT * FROM customers WHERE address ='Ludhiana'" mycursor.execute(sql) myresult = mycursor.fetchall() for x in myresult: print(x) (1, 'Karan', 'Ludhiana') Wildcard Characters¶You can also select the records that …
Read More »Python MySQL Order By
7 Python MySQL Order By Python MySQL Order By¶Sort the Result Use the ORDER BY statement to sort the result in ascending or descending order. The ORDER BY keyword sorts the result ascending by default. To sort the result in descending order, use the DESC keyword. Python MySQL Order by …
Read More »Python MySQL Delete From By
8 Python MySQL Delete From By Python MySQL Delete From By¶ You can delete records from an existing table by using the “DELETE FROM” statement: In [19]: import mysql.connector mydb = mysql.connector.connect( host="localhost", user="root", passwd="", database="itronix" ) mycursor = mydb.cursor() sql = "DELETE FROM customers WHERE address = 'Otawa 4'" mycursor.execute(sql) …
Read More »Python MySQL Drop Table
9 Python MySQL Drop Table Python MySQL Drop Database¶ Query: drop database database_name In [ ]: import mysql.connector mydb = mysql.connector.connect( host="localhost", user="root", passwd="" ) mycursor = mydb.cursor() mycursor.execute("drop database itronix") Python MySQL Drop Table¶ Query: drop table table_name In [6]: import mysql.connector mydb = mysql.connector.connect( host="localhost", user="root", passwd="", database="itronix" ) mycursor = …
Read More »Python MySQL Update Table
10 Python MySQL Update Table Python MySQL Update Table¶You can update existing records in a table by using the “UPDATE” statement: In [22]: import mysql.connector mydb = mysql.connector.connect( host="localhost", user="root", passwd="", database="itronix" ) mycursor = mydb.cursor() sql = "UPDATE customers SET address = 'Detroit' WHERE address = 'Ludhiana'" mycursor.execute(sql) mydb.commit() print(mycursor.rowcount, …
Read More »Python MySQL Limit
11 Python MySQL Limit Python MySQL Limit¶ You can limit the number of records returned from the query, by using the “LIMIT” statement: In [23]: import mysql.connector mydb = mysql.connector.connect( host="localhost", user="root", passwd="", database="itronix" ) mycursor = mydb.cursor() mycursor.execute("SELECT * FROM customers LIMIT 5") myresult = mycursor.fetchall() for x in myresult: …
Read More »Python MySQL Join
12 Python MySQL Join Python MySQL Join¶ Join Two or More Tables You can combine rows from two or more tables, based on a related column between them, by using a JOIN statement. Consider you have a “customers” table and a “product” table: Create Table Name Product¶ In [29]: import mysql.connector …
Read More »
Machine Learning Tutorials, Courses and Certifications