GEEkSpoTTed GEEkSpoTTed - 2 months ago 7
MySQL Question

Error when connecting to MySQL database Python

I m trying 2 connect mysql database 2 python,but m getting a an error

Code==

import mysql.connector;
from mysql.connector import errorcode;

user="root";
password="toor";
host="localhost";
database="pydb";

try:
print("Hello")
conn=mysql.connector.connect('root','toor','localhost:8000','pydb');//have also tried localhost only still same error
print("Hello2");
mycursor=conn.cursor();
print("Hello3");
mycursor.execute("show tables;");
print("Hello4");
print(mycursor.fetchall());
print("Hello5");

conn.close()

except mysql.connector.Error as err:
print("Something went wrong: {}".format(err))


Error ==

Output ==
HELLO

//hello just gets printed once ;

when i tried 2 do the same on IDLE(3.4.3 32 bit) i type this

import mysql.connector;
conn=mysql.connector.connect(host='localhost', user='root', password='toor', port=8000 ,database='pydb');


then the this never appears >>>, it just hangs itself y is it happening with me ?

Answer

Do this ==(No errors occur here ) :P

conn=mysql.connector.connect(host='localhost', user='root', password='toor' );//don't specify db name here do it later see down
print("Hello");
mycursor=conn.cursor();
mycursor.execute("use pydb;");// pydb is a database name
mycursor.execute("show tables;");
print(mycursor.fetchall());
Comments