database - unable to connect python to external sql datatbase -
i have python program works fine xamp server when trying access database on hostinger. getting following errors :
traceback (most recent call last): file "c:\python34\lib\site-packages\mysql\connector\network.py", line 472, in open_connection self.sock.connect(sockaddr) timeouterror: [winerror 10060] connection attempt failed because connected party did not respond after period of time, or established connection failed because connected host has failed respond during handling of above exception, exception occurred: traceback (most recent call last): file "f:/python_3.4/rpi_connect.py", line 5, in <module> class database: file "f:/python_3.4/rpi_connect.py", line 13, in database cnx = mysql.connector.connect(user='u341930730_m', password='abcd', host='mysql.hostinger.in', database='u341930730_rooms') file "c:\python34\lib\site-packages\mysql\connector\__init__.py", line 179, in connect return mysqlconnection(*args, **kwargs) file "c:\python34\lib\site-packages\mysql\connector\connection.py", line 95, in __init__ self.connect(**kwargs) file "c:\python34\lib\site-packages\mysql\connector\abstracts.py", line 719, in connect self._open_connection() file "c:\python34\lib\site-packages\mysql\connector\connection.py", line 206, in _open_connection self._socket.open_connection() file "c:\python34\lib\site-packages\mysql\connector\network.py", line 475, in open_connection errno=2003, values=(self.get_address(), _strioerror(err))) mysql.connector.errors.interfaceerror: 2003: can't connect mysql server on 'mysql.hostinger.in:3306' (10060 connection attempt failed because connected party did not respond after period of time, or established connection failed because connected host has failed respond) process finished exit code 1
here python program
import mysql.connector class database: rooms = [] appliances = [] status = [] column_count = 0 # create connection server # enter server details here cnx = mysql.connector.connect(user='u341930730_m', password='abcd', host='mysql.hostinger.in', database='u341930730_rooms') # create cursor object cursor = cnx.cursor() # function room names server def get_all_rooms(self): # query room names database.cursor.execute("select roomname number_of_rooms") # store room names in list named rooms defined @ begining of class row in database.cursor.fetchall(): room = row[0] self.rooms.append(room) print("all rooms stored in list rooms") # function appliance names server # function requires table name in 'room' parameter # function should called after function get_all_rooms def get_column_names_of_room(self, room): query = "describe " + room database.cursor.execute(query) # print column names row in database.cursor.fetchall(): self.appliances.append(row[0]) #print(row[0]) database.column_count = database.column_count + 1 # return column_count column_count = database.column_count database.column_count = 0 return column_count # function status of appliances tables in database # function requires table name in 'room' , column count in 'column_count' # function should called after get_all_column_names def get_status_of_appliances(self, room, column_count): query = "select * " + room database.cursor.execute(query) # print data row in database.cursor.fetchall(): in range(1, column_count): self.status.append(row[i]) #print(row[i]) # function clear lists # need clear these lists storing data in dictionary , don't want duplicate data def clear_appliance_and_status_list(self): self.appliances.clear() self.status.clear() #function close connection def close_connection(self): database.cnx.close() # _______________________________________________________________________________________________________ # main function body # create dictionary workspace = {} # create class object db = database() db.get_all_rooms() print(db.rooms) room_name in db.rooms: print(room_name) column_count = db.get_column_names_of_room(room_name) print(column_count) print(db.appliances) db.get_status_of_appliances(room_name, column_count) print(db.status) index in range(0,5): app = db.appliances[index+1] status = db.status[index] workspace[app] = status db.clear_appliance_and_status_list() print(workspace) #close database connection db.close_connection()
please tell me wrong , corection if possible. in advance.
Comments
Post a Comment