2

I'm trying to convert a program from python 2 to python 3. The MYSQL database I setup in my program had to be updated. I've ran into a problem very similar to this one, but I don't understand how to change my object from 'connection' to 'Connection' since there's no casting in Python

Here's my code:

import _mysql as mc

db = mc.connect (host = "localhost", user = "root", passwd = "password1234")
cursor = db.cursor()

It looks correct, but for some reason the connect() function is returning a 'connection' object instead of 'Connection'. Any help is appreciated.

4
  • mc.Connection(...) ? Commented Apr 27, 2018 at 1:36
  • I tried that earlier. I get AttributeError: module '_mysql' has no attribute 'Connection' Commented Apr 27, 2018 at 1:38
  • There are several different MySQL libraries for Python. Which one are you using? Commented Apr 27, 2018 at 1:43
  • mysqlclient (1.3.7). Do you have a recommendation for a different one? Commented Apr 27, 2018 at 1:47

1 Answer 1

9

For mysqlclient

import MySQLdb

Do not use _mysql as it is low level interface and does not include all the methods

Sign up to request clarification or add additional context in comments.

2 Comments

That's actually what I had originally when I was working with python 2, but I got ImportError: No module named 'mysql' EDIT: posted before you added the mysqlclient part. Now it works. Thank you!
I would think it is a documentation design error if _mysql is mentioned right at the beginning of MySQLdb User’s Guide, and the first code examples use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.