Answer the question
In order to leave comments, you need to log in
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`')
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question