While trying to connect Python with SQL, it just does not work, with no errors, the execution stops itself, here's the code:
import mysql.connector
def appen():
print('k')
mysq = mysql.connector.connect(user = 'root',passwd = 'manager',host = 'localhost', database = 'krishna')
print('g')
cur = mysq.cursor()
print('h')
for i in range(int(input('How many : '))):
eno = int(input('Eno : '))
enm = input('Name : ')
job = input('Job : ')
mgr = int(input('Mgr : '))
hd = input('Hired date(YYYY-MM-DD) : ')
sal = int(input('Salary : '))
cm = int(input('Comm : '))
dn = int(input('Dn : '))
try:
cur.execute(f"insert into employee values({eno},'{enm}','{job}',{mgr},'{hd}',{sal},{cm},{dn});")
curmit()
except:
cur.rollback()
cur.close()
mysq.close()
appen()
the only output I am getting is :
k
While trying to connect Python with SQL, it just does not work, with no errors, the execution stops itself, here's the code:
import mysql.connector
def appen():
print('k')
mysq = mysql.connector.connect(user = 'root',passwd = 'manager',host = 'localhost', database = 'krishna')
print('g')
cur = mysq.cursor()
print('h')
for i in range(int(input('How many : '))):
eno = int(input('Eno : '))
enm = input('Name : ')
job = input('Job : ')
mgr = int(input('Mgr : '))
hd = input('Hired date(YYYY-MM-DD) : ')
sal = int(input('Salary : '))
cm = int(input('Comm : '))
dn = int(input('Dn : '))
try:
cur.execute(f"insert into employee values({eno},'{enm}','{job}',{mgr},'{hd}',{sal},{cm},{dn});")
curmit()
except:
cur.rollback()
cur.close()
mysq.close()
appen()
the only output I am getting is :
k
Share
Improve this question
edited Feb 2 at 16:57
Shadow
34.3k10 gold badges65 silver badges75 bronze badges
asked Feb 2 at 14:41
krishikrishi
171 silver badge6 bronze badges
5
- How are you running the code? – John Gordon Commented Feb 2 at 15:31
- @JohnGordon through the run command in IDLE – krishi Commented Feb 3 at 1:58
- I asked a friend, he also faces the same problem, maybe some installation process issue ? – krishi Commented Feb 3 at 1:59
- Does IDLE have a separate window or tab where errors would be displayed? – John Gordon Commented Feb 3 at 5:12
- Related – snakecharmerb Commented Feb 3 at 10:58
3 Answers
Reset to default 1It seems you have a connection issue and you can try to find the problem with a code like this
import mysql.connector
from mysql.connector import errorcode
try:
cnx = mysql.connector.connect(
host="localhost",
user="root",
password="manager",
database="krishna"
)
except mysql.connector.Error as err:
if err.errno == errorcode.CR_CONN_HOST_ERROR:
print("Can't connect to MySQL server")
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err.msg)
else:
cnx.close()
You must check if
- MySQL Server is Running
- the connection parameters are correct (host, user, password, database, port)
- the firewall is blocking the connection (port 3306 is the default MySQL port)
- in the MySQL configuration file
bind-address
is configured to only accept connections from specific IP addresses
Since your code only runs to
print('k')
please double check your connection parameter. Also check, if you created a database with you specified name. I was able to run your code just fine after setting up Python and a MySQL-server.
The issue was solved by using
use_pure=True
in
mysq.connect(...)
, as written here https://stackoverflow/a/79228700/16538566