Mysql + Python (Save Variable Login To Connect)

Asked

Viewed 882 times

-1

import the import getpass import mysql.

class Acessdatabase():

UserName = "" PassWard = "" DataBase = "" def LoginDB(self): self.UserName = input("Username: ") os.system("cls") self.PassWard = getpass.getpass("Passward") os.system("cls") self.DataBase = input("DataBase:") self.ConectDB() def ConectDB(self): os.system("cls") conexao = mysql.connector.connect(user = self.Username, password = self.Passward, host = "localhost", database = self.DataBase ) os.system("pause") Sistema = AcessDataBase() Sistema.LoginDB()

how can I store the database name, password and name information within a variable and then use the variable to access the database? I tried to Use Program above but error....

  • Attention to the indentation of variables

  • I believe your problem lies in host = "localhost". Try putting the address directly, something like host = "127.0.0.1"

  • I tried to do that too....

  • So it works when I do this: (user = "root", password = "", host = "localhost", database = "name..." )

1 answer

-1

The first one I found in your code is this

Sistema.LoginDB()

This syntax pulls a variable that does not exist in the Acessdatabase class
As far as I could see (I’m also quite beginner) it was that its functions were not accessing the variables of the public class and so were not changing or saving anything.
My tip is to study more examples that use classes, functions and variables among themselves.

import os
import getpass
import mysql.connector

class AcessDataBase():

    UserName = ""
    PassWard = ""
    DataBase = ""
    conexao = ""


def LoginDB():
    #utilizando o sistema.(variavel) a função estará alterando ou visualizando as
    #Variaveis da classe
    sistema.UserName = input("Username")

    os.system("cls")

    sistema.PassWard = getpass.getpass("Passward")

    os.system("cls")

    sistema.DataBase = input("Database")

    os.system("cls")

def ConectDB():

    os.system("cls")

    sistema.conexao = mysql.connector.connect(user=sistema.UserName, password=sistema.PassWard,
                                              host="localhost", database=sistema.DataBase)

    os.system("pause")

sistema=AcessDataBase()

LoginDB()

ConectDB()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.