How to Access MySQL with Python Version 3.4

http://askubuntu.com/questions/630728/how-to-access-mysql-with-python-version-3-4

 

Python comes in two versions: Python 2.7.6 which has now been superseded by Python 3.4.0. The critical line in Python to access MySQL is "import MySQLdb"

Thanks to a response from StackOverFlow, I realized that I was unintentionally accessing MySQL through Python version 2.7.6. Apparently, "MySQLdb" is not compatible with Python version 3.4.0.

Searching the internet has disclosed alternatives for connecting Python to MySql, however, many of the suggestions do not disclose whether the offered solutions would actually work with Python version 3.4.0.

This StackOverFlow Post listed some possible solutions. Python 3.4.0 with MySQL database. I installed mysqlclient 1.3.6, I also ran "sudo pip install PyMySQL" with out error.

However, upon entering python with "python3"; I still received the error message "ImportError: No module named 'MySQLdb'" after typing in: "import MySQLdb". So something was still wrong.

As an FYI MySQL evidently has its own connector, but I don't know whether it works or does not work with Python 3.4. Connecting to MySQL Using Connector/Python I will have to look into this more.

Any advice on how to connect Python 3.4 with MySQL?

share improve this question
 

1 Answer

up vote 0down voteaccepted

The documentation for MySQL Connector/Python says Version 2.0 should work with MySQL Server 5.5-5.7 and Python 3.3 and later. So I would say it's a good bet. If it doesn't work, follow up with what error you encounter and I'll see if I can help.

share improve this answer
 
   
Thanks. Will give it a try. – Steve R. Jun 1 '15 at 17:27
   
Let me know if it works please. – Synergist Jun 1 '15 at 17:43
   
Sort of. When I type in just plain python then " import mysql.connector" it works. When I type in python3then " import mysql.connector" it fails with the message "ImportError: No module named 'mysql'". Implies a configuration issue (such as path) with Python 3.4.0? – Steve R. Jun 1 '15 at 18:04
   
How did you install MySQL Connector? – Synergist Jun 1 '15 at 18:09
   
Turns out that the new version is available through the Ubuntu Software Center. However, I had to remove the old "python-mysql.connector" (version 1.1.6-1) before I could install the new version of the python connector. I only ran the install from the Ubuntu Software Center. Did not underake any other configuration. – Steve R.Jun 1 '15 at 18:20

转载于:https://www.cnblogs.com/kungfupanda/p/5772681.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值