To create a database in MySQL, use the "CREATE DATABASE" statement:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="ram1234"
)
my1 = mydb.cursor()
my1.execute("CREATE DATABASE ramesh")
-----------
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="ram1234"
)
mycursor = mydb.cursor()
mycursor.execute("SHOW DATABASES")
for x in mycursor:
print(x)
----------------------
Creating a Table
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="ram1234",
database="ramesh"
)
mycursor = mydb.cursor()
mycursor.execute("CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))")
---------------
You can check if a table exist by listing all tables in your database with the "SHOW TABLES" statement:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="ram1234",
database="ramesh"
)
mycursor = mydb.cursor()
mycursor.execute("SHOW TABLES")
for x in mycursor:
print(x)
Primary Key
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.
We 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.
Example
Create primary key when creating the table:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="ram1234",
database="ramesh"
)
mycursor = mydb.cursor()
mycursor.execute("CREATE TABLE customers (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), address VARCHAR(255))")
If the table already exists, use the ALTER TABLE keyword:
Example
Create primary key on an existing table:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="ram1234",
database="ramesh"
)
mycursor = mydb.cursor()
mycursor.execute("ALTER TABLE customers ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY")
-----------------------
Microsoft Windows [Version 10.0.19042.1110]
(c) Microsoft Corporation. All rights reserved.
C:\Users\VLR Training>cd desktop
C:\Users\VLR Training\Desktop>pyton ram.py
'pyton' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\VLR Training\Desktop>python ram.py
C:\Users\VLR Training\Desktop>python ram.py
('information_schema',)
('mysql',)
('performance_schema',)
('ram',)
('ramesh',)
('sakila',)
('sys',)
('world',)
C:\Users\VLR Training\Desktop>python ram.py
File "C:\Users\VLR Training\Desktop\ram.py", line 7
database="ramesh"
^
SyntaxError: invalid syntax
C:\Users\VLR Training\Desktop>python ram.py
C:\Users\VLR Training\Desktop>python ram.py
C:\Users\VLR Training\Desktop>python ram.py
File "C:\Users\VLR Training\Desktop\ram.py", line 16
Primary Key
^
SyntaxError: invalid syntax
C:\Users\VLR Training\Desktop>python ram.py
('customers',)
('sal',)
C:\Users\VLR Training\Desktop>python ram.py
C:\Users\VLR Training\Desktop>70 python with mysql create db ,table sql videos in telugu
No comments:
Post a Comment
Note: only a member of this blog may post a comment.