-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path70.mysql.py
More file actions
44 lines (40 loc) · 1.85 KB
/
70.mysql.py
File metadata and controls
44 lines (40 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 70 . Python MySql
"""
# Python can be used in database applications. One of the most popular databases is MySQL.
# To be able to experiment with the code examples in this tutorial, you should have MySQL
installed on your computer.
# Python needs a MySQL driver to access the MySQL database. In this tutorial we will use the
driver "MySQL Connector".
# use PIP to install "MySQL Connector". PIP is most likely already
installed in your Python environment.
# DB:
# To create a database in MySQL, use the "CREATE DATABASE" statement.
# You can check if a database exists by listing all databases in your system by using the
"SHOW DATABASES" statement.
# 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
# check if a table exist by listing all tables in your database with the "SHOW TABLES" statement
# When creating a table, you should also create a column with a unique key for each record.
This can be done by defining a PRIMARY KEY
# use the statement "INT AUTO_INCREMENT PRIMARY KEY" which will insert a unique
number for each record. Starting at 1 and increased by one for each record
# If the table already exists, use the ALTER TABLE keyword.
"""
import mysql.connector
mydb = mysql.connector.connect(
host = "localhost",
user = "root",
password= "",
database= "pythondb"
)
# cursor : use to send queries to the db
myquery = mydb.cursor()
#myquery.execute("CREATE DATABASE pythondb")
#myquery.execute("SHOW DATABASES")
#for x in myquery:
#print(x)
#myquery.execute("CREATE TABLE categories(name VARCHAR(255), description VARCHAR(255))")
#myquery.execute("ALTER TABLE categories ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY")
myquery.execute("SHOW TABLES")
for x in myquery:
print(x)