首页 MySql数据库开发课程 Python操作MySQL数据表命令
pay pay

Python操作MySQL数据表命令

日期: 五月 9, 2023, 5:26 a.m.
阅读: 132
作者: Python自学网-村长

摘要: Python操作MySQL数据表命令

import pymysql


# 创建数据库表
db = pymysql.connect("localhost", "root", "wsw..177122", "wakey")
cursor = db.cursor()

sql = 'show tables;'
cursor.execute(sql)
data = cursor.fetchall()  # 查看所有结果
print(data)
sql = 'select * from student;'
cursor.execute(sql)
data = cursor.fetchone()
print(data)

# 检查表是不是存在,如果存在则删除
cursor.execute("drop table if exists boy")
# 建表
sql = 'create table boy(id int auto_increment primary key, name varchar(20) not null, age int not null)'
cursor.execute(sql)

cursor.close()
db.close()

 

原创视频,版权所有,未经允许,切勿转载,违者必究!
回顶部