Tuesday , March 19 2024

Python

Python MySQL Installation

MySQL Installation

1 Python MySQL Installation Python MySQL Database¶SQL stands for Structured Query Language and is a widely used programming language for managing relational databases. Python can be used in database applications. One of the most popular databases is MySQL. Python needs a MySQL driver to access the MySQL database. pip install …

Read More »

Python MYSQL Create Database

Python MYSQL Create Database

2 Python MySQL Create Database Python MySQL Create Database¶To create a database in MySQL, use the “CREATE DATABASE” statement: Note: In MySQL, it’s mandatory to put a semicolon (;) at the end of a statement, which denotes the termination of a query. However, MySQL Connector/Python automatically appends a semicolon at …

Read More »

Python MySQL Create Table

Python MySQL Create Table

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

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

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

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

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

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

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

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 »