• Python中mysql命令安装


    pymysql是python中操作mysql的模块

    安装mysql
    pip 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()
  • 相关阅读:
    揭秘Amazon反应速度超快的下拉菜单
    CSS3滤镜
    雅虎团队经验:网站页面性能优化的34条黄金守则
    10条影响CSS渲染速度的写法与建议
    前端优秀网站
    视频媒体播放,最好的 HTML 解决方法
    zen-coding for notepad++,前端最佳手写代码编辑器
    ASP.NET中IsPostBack详解
    w3c教程
    怎样才能成为优秀的前端开发工程师
  • 原文地址:https://www.cnblogs.com/zqntx/p/11507343.html
Copyright © 2020-2023  润新知