CREATE DATABASE database_name;
USE database_name;
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
...
);
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
Example:
INSERT INTO students (id, name, age) VALUES (1, 'John Doe', 20);
SELECT column1, column2, ... FROM table_name WHERE condition;
Example:
SELECT * FROM employees WHERE department = 'IT';
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
Example:
UPDATE products SET price = 25.99 WHERE id = 101;
DELETE FROM table_name WHERE condition;
Example:
DELETE FROM customers WHERE last_purchase_date < '2022-01-01';
Inner Join:
SELECT columns FROM table1 INNER JOIN table2 ON table1.column = table2.column;
Left Join:
SELECT columns FROM table1 LEFT JOIN table2 ON table1.column = table2.column;
pip install mysql-connector-python
import mysql.connector
mydb = mysql.connector.connect(
host="your_host",
user="your_username",
password="your_password",
database="your_database"
)
mycursor = mydb.cursor()
sql_query = "SELECT * FROM your_table;"
mycursor.execute(sql_query)
result = mycursor.fetchall()
for row in result:
print(row)
sql_insert = "INSERT INTO your_table (column1, column2, ...) VALUES (%s, %s, ...)"
values = (value1, value2, ...)
mycursor.execute(sql_insert, values)
mydb.commit()