zoukankan      html  css  js  c++  java
  • mysql-connector/python使用示例

    1.下载安装connector/python

    地址:https://dev.mysql.com/downloads/connector/python/

    下载的版本(mysql-connector-python-8.0.15-py3.5-windows-x86-64bit.msi)  下载后根据提示安装

     

    2.数据库中数据

     

    3.使用python中的mysql.connector模块操作mysql

    import mysql.connector                 
    
    # mysql1.py
    config = {
        'host': '127.0.0.1',
        'user': 'root',
        'password': 'root',
        'port': 3306,
        'database': 'test',
        'charset': 'utf8'
    }
    try:
        cnn = mysql.connector.connect(**config)
    except mysql.connector.Error as e:
        print('connect fails!{}'.format(e))
    cursor = cnn.cursor()
    try:
        sql_query = 'select name,age from stu ;'
        cursor.execute(sql_query)
        for name, age in cursor:
            print (name, age)
    except mysql.connector.Error as e:
        print('query error!{}'.format(e))
    finally:
        cursor.close()
        cnn.close()
    

    结果:

    (u'xiaoming', 10)
    (u'rose', 18)
    (u'jack', 19)
    (u'fang', 20)
    (u'Liang', 40)
    (u'Age', None)
    

      

     

     

  • 相关阅读:
    linux 硬件信息
    docker note
    Shell cmd set note
    mysql management note
    scp noneed passwd
    update kernel
    数据包处理过程
    tcp/ip分片
    sockopt note
    Python note
  • 原文地址:https://www.cnblogs.com/xiaohuomiao/p/10729818.html
Copyright © 2011-2022 走看看