如何在MySQL数据库中使用python 3.5.1 [英] How to use python 3.5.1 with a MySQL database

查看:108
本文介绍了如何在MySQL数据库中使用python 3.5.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在一个我正在从事的Python项目中使用MySQL.我下载了连接器:mysql-connector-python-2.1.3-py3.4-winx64 此处.

I have been trying to use MySQL in a Python project I've been working on. I downloaded the connector: mysql-connector-python-2.1.3-py3.4-winx64 here.

我已经安装了Python 3.5.1.当我尝试安装连接器时,它无法正常工作,因为它需要python 2.7.我在很多站点上进行了搜索,甚至在StackOverflow上也找不到解决方案.

I already had Python 3.5.1 installed. When I tried to install the connector, it didn't work because it required python 2.7 instead. I have searched on many sites, even on StackOverflow I couldn't find a solution.

感谢您的帮助.

推荐答案

我使用Python 3.5.1执行了以下步骤,并且可以正常工作:

I did the steps below with Python 3.5.1 and it works:

  • 此处
  • 下载驱动程序
  • 在此文件夹中的cmd中安装驱动程序 Python \ Python35 \ PyMySQL-0.7.4 \ pymysql

  • Download driver from here
  • Driver installation in cmd, in this folder Python\Python35\PyMySQL-0.7.4\pymysql

python setup.py build
python setup.py install

  • 将文件夹Python \ Python35 \ PyMySQL-0.7.4 \ pymysql复制到 Python \ Python35 \ pymysql

  • Copy folder Python\Python35\PyMySQL-0.7.4\pymysql to Python\Python35\pymysql

    Python IDE中的示例代码

    Sample code in python IDE

    import pymysql
    import pymysql.cursors
    conn= pymysql.connect(host='localhost',user='user',password='user',db='testdb',charset='utf8mb4',cursorclass=pymysql.cursors.DictCursor)
    a=conn.cursor()
    sql='CREATE TABLE `users` (`id` int(11) NOT NULL AUTO_INCREMENT,`email` varchar(255) NOT NULL,`password` varchar(255) NOT NULL,PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;'
    a.execute(sql)
    

  • 享受吧!
  • 这篇关于如何在MySQL数据库中使用python 3.5.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆