'Python RaspBerry Pi will not connect to mysql database

We have created a Python Script to put the socket string into our table if it does not exist yet inside the database.

When we run this script into visual studio it works fine as it is. But the moment we try to do it in our RaspBerry Pi the Select and update query does not come through.

The web service where we have the database does not even register that the connection has taken place, but in Visual studio it is completely fine. Yes we have installed mysql-connector python and yes our RaspBerry Pi has internet because we operating the RaspBerry Pi remotely.

NOTE: some information has been censored for confidentiality reasons.

import time
import mysql.connector
import socket
import pygame
from time import sleep
import RPi.GPIO as GPIO

UniqueUID = str(socket.gethostname())
def db_connect():
    ourdb = mysql.connector.connect(
        host="CENSORED",
        user="CENSORED",
        password="CENSORED",
       database="CENSORED"
                          )
    return ourdb


def UniqueUID_check():
    ourdb = db_connect()
    try:
        sql_select_query = "Select * From tablename where UniqueUID = %s"
        selectuidcursor = ourdb.cursor()
        selectuidcursor.execute(sql_select_query, (UniqueUID,))
        record = selectuidcursor.fetchall()
        print("Found UID")
        selectuidcursor.close()
        ourdb.close()
    except mysql.connector.Error as error:
        UIDinsertcursor = ourdb.cursor()
        print("Failed to get record from MySQL table: {}".format(error))
        sql = "INSERT INTO tablename (UniqueUID,OnlineStatus) VALUES (%s, %s)"
        val = (UniqueUID,1)
        UIDinsertcursor.execute(sql, val)
        ourdb.commit()
        UIDinsertcursor.close()
        ourdb.close()
        print("Added a new UniqueUID")


UniqueUID_check()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source