zoukankan      html  css  js  c++  java
  • python 3.5.2安装mysql驱动报错

    python 3.5.2安装mysql驱动报错

    python 3.5.2安装mysql驱动时出现如下异常:

    [root@localhost www]# pip install mysql-connector-python
    Collecting mysql-connector-python
      Could not find a version that satisfies the requirement mysql-connector-python (from versions: )
    No matching distribution found for mysql-connector-python
    

    这是由于官方驱动暂时只支持到python3.4所致。改用pymysql
    安装pymysql

    pip install PyMySQL
    

    使用例子:
    建表语句

    CREATE TABLE `users` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `email` varchar(255) COLLATE utf8_bin NOT NULL,
        `password` varchar(255) COLLATE utf8_bin NOT NULL,
        PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
    AUTO_INCREMENT=1 ;
    
    import pymysql.cursors
    
    # Connect to the database
    connection = pymysql.connect(host='localhost',
                                 user='user',
                                 password='passwd',
                                 db='db',
                                 charset='utf8mb4',
                                 cursorclass=pymysql.cursors.DictCursor)
    
    try:
        with connection.cursor() as cursor:
            # Create a new record
            sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
            cursor.execute(sql, ('webmaster@python.org', 'very-secret'))
    
        # connection is not autocommit by default. So you must commit to save
        # your changes.
        connection.commit()
    
        with connection.cursor() as cursor:
            # Read a single record
            sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
            cursor.execute(sql, ('webmaster@python.org',))
            result = cursor.fetchone()
            print(result)
    finally:
        connection.close()
    
    • 参考文档https://pypi.python.org/pypi/PyMySQL#example
  • 相关阅读:
    Eclipse SVN插件设置
    经典语录-每日积累-05
    Shell基础语法,运算符,循环和判断语句和设置启动参数
    iOS-Jenkins自动化打包集成
    App版本升级相关
    Java-数组和集合简单使用
    Java-内部类简单使用
    Callkit被拒
    Java-Finalize(GC)和类与类和接口之间的关系
    经典语录-每日积累-04
  • 原文地址:https://www.cnblogs.com/rwxwsblog/p/5765338.html
Copyright © 2011-2022 走看看