Home > Back-end >  Python Msql Unread result found
Python Msql Unread result found

Time:10-31

I got this error while trying to get variable from a DataBase in MySql

0x000002A MySQL Connection Error!

MySql error is provided when version/value we're trying to get from database is not a number

output when trying to print value Program is trying to get from database: None

I tried to get variable named: "gameVersion" from DataBase Named: "gameversions", using Name of game but Instead of getting variable which has: "beta" I got mysql connector error

file and code that caused this error: functions.py:

import mysql.connector

database = mysql.connector.connect(
    host = "localhost",
    user = "root",
    passwd = "s W2DrcPQK5dada2^!dV!RUSZ@2$PbEX*3eacT",
    database = "PythonDB",
)

my_cursor = database.cursor(buffered=True)

user = ""
game = ""
db_version = my_cursor.execute(f"SELECT gameVersion FROM gameversions WHERE gameName = 'Grand Thiefs'")

sqlStuff = "INSERT INTO users (usersUid, usersEmail, usersPwd, UsersPerms, usersMoney, gameVersion) VALUES (%s, %s, %s, %s, %s, %s)"
sqlStuffv = "INSERT INTO gameversions (gameVersion, gameName) VALUES (%s, %s)"

def CreateDataBase(name):
    my_cursor.execute(f"CREATE DATABASE {name}")

#my_cursor.execute("CREATE TABLE gameVersions (gameVersion varchar(255) NOT NULL, gameName varchar(255) NOT NULL);")
def ShowTables():
    my_cursor.execute("SHOW TABLES")

def ShowDataBases():
    my_cursor.execute("SHOW DATABASES")

def CreateUser(Name, Password, Email, Version):
    global user
    user = [{Name}, {Email}, {Password}, "User", "0", {Version}]
    my_cursor.execute(sqlStuff, user)
    database.commit()

def checkversion(version, name):
    if version != db_version:
        if type(db_version) == float:
            return "Failure"
        else:
            return "MySQL Connection Error"
    else:
        return "Correct"

CodePudding user response:

My code to connect at MySQL is :

connection = mysql.connector.connect(
 host="localhost",
 user="root",
 password="root",
 port="8889",
 database="registration",
 auth_plugin="mysql_native_password"
 )

 c = connection.cursor()

Make sure you are connected to Mamp,Xampp,Wamp and other but try this to connect

CodePudding user response:

I fixed my problem by changing code from:

def checkversion(version):
    #! Code Does Not Work Due To db_version error returning "None"!
    my_cursor.execute("SELECT gameVersion FROM gameversions WHERE gameName='Grand Thiefs' limit 1;")
    if version != db_version:
        if type(db_version) != str or float:
            #print(db_version)
            return "Failure"
        else:
            #print(db_version)
            return "MySQL Connection Error"
    else:
        return "Correct"

To:

def checkversion(version):
    my_cursor.execute("SELECT gameVersion FROM gameversions")
    db_version = my_cursor.fetchone()
    if version != db_version[0]:
        if not type(db_version) in [str, float]:
            print(db_version)
            return "Failure"
        else:
            #print(db_version)
            return "MySQL Connection Error"
    else:
        return "Correct"

Thanks to youtube tutorial Help is not needed anymore

  • Related