zoukankan      html  css  js  c++  java
  • python操作Mysql数据库

    1、通过 pip 安装 pymysql
    
    [root@bogon ~]# pip install pymysql
    
    
    2、测试连接
    
    [root@bogon ~]# python
    Python 3.6.5 (default, Nov 22 2018, 03:13:09) 
    [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import pymysql
    >>> 
    
    
    3.1、 创建mysql数据库、表、语句
    
    
    mysql> create database ceshi;
    Query OK, 1 row affected (0.02 sec)
    
    mysql> use ceshi;
    Database changed
    mysql> create table if not exists test ( id int(10) PRIMARY KEY AUTO_INCREMENT, name varchar(50) NOT NULL);
    Query OK, 0 rows affected (0.03 sec)
    
    mysql> insert into test values(1,'dahuju');
    Query OK, 1 row affected (0.00 sec)
    
    mysql> insert into test values(2,'yunjisuan');
    Query OK, 1 row affected (0.03 sec)
    
    mysql> select * from test;
    +----+-----------+
    | id | name      |
    +----+-----------+
    |  1 | dahuju    |
    |  2 | yunjisuan |
    +----+-----------+
    2 rows in set (0.00 sec)
    
    
    
    3.2、查询操作
    
    
    import pymysql  #导入 pymysql
    #打开数据库连接
    db= pymysql.connect(host="192.168.0.204",user="root",password="jenkins!666",db="ceshi",port=3306)
    
    # 使用cursor()方法获取操作游标
    cur = db.cursor()
    
    #1.查询操作
    # 编写sql 查询语句  test 对应的表名
    sql = "select * from test"
    try:
        cur.execute(sql)     #执行sql语句
    
        results = cur.fetchall()    #获取查询的所有记录
        print("id","name")
        #遍历结果
        for row in results :
            id = row[0]
            name = row[1]
            print(id,name)
    except Exception as e:
        raise e
    finally:
        db.close()    #关闭连接
    
    
    验证:
    
    ssh://root@192.168.0.204:22/usr/bin/python -u /home/progect/app/py_code/test3.py
    id name
    1 dahuju
    2 yunjisuan
    
    
    
    3.3、插入操作
    
    
    import pymysql
    #2.插入操作
    db= pymysql.connect(host="192.168.0.204",user="root",password="jenkins!666",db="ceshi",port=3306)
    # 使用cursor()方法获取操作游标
    cur = db.cursor()
    
    sql_insert ="""insert into test(id,name) values(3,'人工智能')"""
    
    try:
        cur.execute(sql_insert)
        #提交
        db.commit()
    except Exception as e:
        #错误回滚
        db.rollback()
    finally:
        db.close()
    
    
    验证:
    
    mysql> select * from test;
    +----+--------------+
    | id | name         |
    +----+--------------+
    |  1 | dahuju       |
    |  2 | yunjisuan    |
    |  3 | 人工智能     |
    +----+--------------+
    3 rows in set (0.00 sec)
    
    
    
    3.4、更新操作
    
    import pymysql
    #3.更新操作
    db= pymysql.connect(host="192.168.0.204",user="root",password="jenkins!666",db="ceshi",port=3306)
    
    # 使用cursor()方法获取操作游标
    cur = db.cursor()
    sql_update ="update test set name = '%s' where id = %d"
    
    try:
        cur.execute(sql_update % ("大数据",1))  #像sql语句传递参数
        #提交
        db.commit()
    except Exception as e:
        #错误回滚
        db.rollback()
    finally:
        db.close()
    
    
    验证:
    
    
    mysql> select * from test;
    +----+--------------+
    | id | name         |
    +----+--------------+
    |  1 | 大数据       |
    |  2 | yunjisuan    |
    |  3 | 人工智能     |
    +----+--------------+
    3 rows in set (0.00 sec)
    
    
    
    3.5、删除操作
    
    
    import pymysql
    #4.删除操作
    db= pymysql.connect(host="192.168.0.204",user="root",password="jenkins!666",db="ceshi",port=3306)
    
    # 使用cursor()方法获取操作游标
    cur = db.cursor()
    
    sql_delete ="delete from test where id = %d"
    
    try:
        cur.execute(sql_delete % (3))  #像sql语句传递参数
        #提交
        db.commit()
    except Exception as e:
        #错误回滚
        db.rollback()
    finally:
        db.close()
    
    
    验证:
    
    mysql> select * from test;
    +----+-----------+
    | id | name      |
    +----+-----------+
    |  1 | 大数据    |
    |  2 | yunjisuan |
    +----+-----------+
    2 rows in set (0.00 sec)
  • 相关阅读:
    如何快速建立自己的知识体系
    让你的网站变成灰色
    Java多线程-锁升级
    冰河正则大全
    Win硬盘/U盘设置图片
    MySQL——三范式
    MySQL——事务
    MySQL_explain
    HDFS的一些常用指令
    Hadoop集群的搭建准备
  • 原文地址:https://www.cnblogs.com/effortsing/p/10051209.html
Copyright © 2011-2022 走看看