Connect Python-3.6.4 to MySQL database

Questions about applications and software
Forum rules
Before you post please read how to get help
Post Reply
pantr
Level 1
Level 1
Posts: 1
Joined: Sat Jan 20, 2018 7:22 am

Connect Python-3.6.4 to MySQL database

Post by pantr » Sun Jan 21, 2018 8:14 am

Hi,
I run Mint 18.3, 32 bits. It seems that Mint 18.3 has a "native" python version 2.7. I have installed python-3.6.4, which seems to have been stored in usr/src/Python-3.6.4. I have till now used C/C++ but I want to give python a try.
I would also like to connect to MySQL databases for several reasons. I have done that earlier using PHP, but now it is python-time.
According to googled instructions a python script connecting to MySQL may be as follows (this is a simple test script to see if DB connection is obtained):

#!/usr/bin/python
# -*- coding: utf-8 -*-
import MySQLdb as mdb
import sys

try:
con=mdb.connect(host="localhost", port=3306, user="root", passwd="parSQL")
if(con):
print("connection OK")
else:
printf("Not connected")

cur = con.cursor()
cur.execute("SELECT VERSION()")

ver = cur.fetchone()

print ("database version : %s " % ver)
---------------------------------------------------------------------------------------------------
I get the following error message:

Traceback (most recent call last):
File "fib.py", line 8, in <module>
import MySQLdb as mdb
ModuleNotFoundError: No module named 'MySQLdb'

Obvously no NySQLdb is found.
Also, there is no /usr/bin/python directory created in Mint 18.3. I have tried several times to install the interface MySQLdb, but I cannot discover its location,

The question is: Is Mint 18.3 ready for this python version? How can I make a python3.6 script connect to MySQL? Where could I find MySQLdb?
Grateful for hints.

brgds
pantr

Post Reply

Return to “Software & Applications”