1

So I want to import a function which is within a class in mysqlConnector.py to another python file excelread.py I get a Name Error : 'dataBase' not defined which I guess is because I did not import the function right Can Someone review the code and tell me why, thanks !

Below is the code to the file in which I want to import (excelread.py)

#importing python package to read excel file (.xlsx and .xlsm ) & write onto (.xlsx)
import pylightxl as xl      
#importing mysql Package
import mysql.connector

from mysqlConnector import SQLConnector

newConnector = SQLConnector
newConnector.connector()


#TITLE
print("Hello, welcome to XL Read") 
                       
#Reading file & storing data to a var
xcelFile = xl.readxl(fn='C:/Users/Mojo/Desktop/py_excel/excel/test.xlsx')





#interation
for row in xcelFile.ws(ws='Sheet1').rows:
    if(row != ['Name', 'ID', 'Dpt']):
        name,id,dept = row
        #using db cursor and storing it to a variable 'mycursor'
        mycursor = dataBase.cursor()
        mycursor.execute(f"INSERT INTO data_id (NAME, ID) VALUES ('{name}',{id});")
        #executing db commands via the velow funcion
        dataBase.commit() 

Here is the file which contains the code to a function inside a class (mysqlConnector.py)

import mysql.connector

class SQLConnector:
    def connector():
        #Database connector
        dataBase = mysql.connector.connect(
        host="localhost",
        database="excelread_py",
        user="pma",
        password="123456"
        )
        return dataBase
wwii
  • 23,232
  • 7
  • 37
  • 77
shefreyn
  • 19
  • 1
  • 1
    Assign the return value of the *function* to a name/variable: `newConnector.connector()` --> `dataBase = newConnector.connector()` – wwii Mar 21 '21 at 18:04

0 Answers0