V
V
Vitaly Slyusar2016-04-19 16:07:36
MySQL
Vitaly Slyusar, 2016-04-19 16:07:36

What is the correct way to query MySQL using Python3?

There is a code

# -*- coding: utf-8 -*-
import mysql.connector as db
import socket

#Выполняем подключение с отловом ошибок
try:
    con = db.connect(host='localhost',database='shop',user='root',password='')

    #Проверям подключение
    if con.is_connected():
        print('Вы успешно подключились к базе данных\n')
        #обозначаем курсор
        cur = con.cursor()
        #Указываем, что все данные из тадлици хотим видеть в utf8
        cur.execute('SET NAMES `utf8`')

#ловим ошибку
except db.Error as e:
    print("Ошибка подключения: '"+str(e)+"'.")
finally:
    con.close()


cur.execute('SELECT * FROM `clients`')

Gives me an error:
mysql.connector.errors.OperationalError: 2055: Lost connection to MySQL server at 'localhost:3306', system error: 10038 An operation was attempted on a non-socket object
How do I fix this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2016-04-19
@dimonchik2013

https://github.com/PyMySQL/PyMySQL
PS this is another connector

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question