# 導入pymysql模塊import pymysql
mysql
# 鏈接databaseconn = pymysql.connect(
host="localhost",
user="root",
password="123456",
database="fanis",
charset="utf8"
)
sql
# 獲取一個光標cursor = conn.cursor()
sql = "show tables;"
cursor.execute(sql)
for tables in cursor.fetchall():
for table in tables:
if table == "user":
sql = "drop table user;"
cursor.execute(sql)
fetch
# 建立表sql = """
CREATE TABLE user(
id INT auto_increment PRIMARY KEY ,
name CHAR(10) NOT NULL UNIQUE,
age TINYINT NOT NULL
);"""
cursor.execute(sql)
code
# 插入數據sql = "insert into user(name, age) values('july', 14);"
cursor.execute(sql)
sql = "insert into user(name, age) values('june', 25);"
cursor.execute(sql)
sql = "insert into user(name, age) values('marin', 36);"
cursor.execute(sql)
sql = "select * from user;"
cursor.execute(sql)
for item in cursor.fetchall():
print(item)
rem
# 涉及寫操做要注意提交conn.commit()
it
# 關閉鏈接cursor.close()
conn.close()
table