Python简单操作MySQL命令安装

安装平台:windows、py3.x
pymysql是python中操作mysql的模块
#运行pip3 install pymysql命令安装

mysql版本:mysql-installer-community-8.0.12.0
基本命令
创建数据库 create database student;
使用数据库 use student;
显示所有表 show tables;

import pymysql
#1、创建数据库连接对象
connect = pymysql.connect(
# host:表示主机地址
# 127.0.0.1 本机ip
# 172.16.21.41 局域网ip
# localhost (local:本地 host:主机 合在一起为本地主机的意思)
# host表示mysql安装的地址
host=“127.0.0.1”,
user=“root”,
passwd=“123456”,
# mysql默认的端口号是3306
port=3306,
# 数据库名称
db=“student”
)

#2、创建游标,用于操作表
cursor = connect.cursor()

3、创建表
create_table = “create table if not exists stu (name varchar(30), age integer, phone varchar(11))”
cursor.execute(create_table)

#4、表的增、删、改、查
#增加
insert_table = “insert into stu (name,age,phone) values (‘张三’, 19, ‘13332001256’)”
cursor.execute(insert_table)

删除
delete_table = “delete from stu where name=‘张三’”
cursor.execute(delete_table)

修改
update_table = “update stu set age = 20 where id < 10”
cursor.execute(update_table)

查询
select_table = “select * from stu”
res = cursor.execute(select_table)

s = res.fetchone()
print(s)

ss = res.fetchall()
print(ss)

5、提交sql语句
connect.commit()
6、关闭游标、数据库
cursor.close()
connect.close()

参考文档

https://blog.csdn.net/zjy_android_blog/article/details/81460033

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值