Reputation: 128
I'm making a form which allows me to add questions to a database. I've written code but when I try it I get this error ('AttributeError: 'module' object has no attribute 'GetTable')
The error lies with this part of my code 't = self.dbu.GetTable()'
@QtCore.pyqtSignature("on_add_btn_clicked()")
def Add_btn(self):
Question = self.Question_lineEdit.text()#Grabs the text from all line edit fields
Answer = self.Answer_lineEdit.text()
IsmultiChoice = self.IsMultiChoice_lineEdit.text()
if not Question:
QtGui.QMessageBox.warning(self, 'Warning', 'Username Missing')
else:
t = self.dbu.GetTable()
print (t)
for col in t:
if Question == col[1]:
QtGui.QMessageBox.warning(self, 'Warning', 'Question Taken. :(')#Checks the database and warns if the database does exist
else:
self.dbu.AddEntryToTable (Question, Answer, isMultiChoice)
QtGui.QMessageBox.information(self, 'Success', 'User Added Successfully!')#If everythings is okay it adds the user
self.close()
Here is my database code:
import mysql.connector
from mysql.connector import errorcode
from datetime import datetime
class DatabaseUtility:
def __init__(self, database, Tablename,):
self.db = database
self.Tablename = Tablename
self.cnx = mysql.connector.connect(user = 'root',
password = '',
host = 'localhost')
self.cursor = self.cnx.cursor()
def ConnectToDatabase(self):
try:
self.cnx.database = self.db
except mysql.connector.Error as err:
if err.errno == errorcode.ER_BAD_DB_ERROR:
self.CreateDatabase()
self.cnx.database = self.db
else:
print(err.msg)
def CreateDatabase(self):
try:
self.RunCommand("CREATE DATABASE %s DEFAULT CHARACTER SET 'utf8';" %self.db)
except mysql.connector.Error as err:
print("Failed creating database: {}".format(err))
def GetTable(self):
self.Tablename()
return self.RunCommand("SELECT * FROM %s;" % self.Tablename)
def GetColumns(self):
return self.RunCommand("SHOW COLUMNS FROM %s;" % self.Tablename)
def RunCommand(self, cmd):
print ("RUNNING COMMAND: " + cmd)
try:
self.cursor.execute(cmd)
except mysql.connector.Error as err:
print ('ERROR MESSAGE: ' + str(err.msg))
print ('WITH ' + cmd)
try:
msg = self.cursor.fetchall()
except:
msg = self.cursor.fetchone()
return msg
def AddEntryToTable(self, Question, Answer,IsMultiChoice):
cmd = " INSERT INTO " + self.Tablename + " (Question, Answer, IsMultiChoice)"
cmd += " VALUES ('%s', '%s', '%s');" % (Question, Answer, IsMultiChoice)
self.RunCommand(cmd)
def __del__(self):#Deconstructer class
self.cnx.commit()
self.cursor.close()
self.cnx.close()
if __name__ == '__main__':
db = 'UsernamePassword_DB'
Tablename = 'questiontable'
dbu = DatabaseUtility(db, Tablename)
What am I doing wrong and how can I correct it?
Upvotes: 0
Views: 183
Reputation: 1414
I'd need to see some additional code to be sure (see my comment). But for now my guess is it has to do with how you first instantiate or reference the dbu
variable/attribute. If all you did is import it from another file, because dbu
is created in that file's main()
(in the snippet you provided), that might be the issue. For example, this is incorrect:
from whatever import dbu # wrong
from whatever.main import dbu # Also wrong. In fact I don't think this would even work
Instead, import DatabaseUtility
directly into the script, then instantiate dbu
like :
from whatever import Database Utility
class SomeClass(object):
...
def startDB(self, db, Tablename):
self.dbu = DatabaseUtility(db, Tablename)
The reason I guess this is because the error message says module has no attribute, instead of mentioning the actual class. Here's an example of the error I would expect to see:
In [31]: class MyClass(object):
one = 1
....:
In [32]: myc = MyClass()
In [33]: myc.one
Out[33]: 1
In [34]: myc.two
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-34-4bae87671838> in <module>()
----> 1 myc.two
AttributeError: 'MyClass' object has no attribute 'two'
Notice how it says MyClass is the problem, not "module".
Potentially unrelated, you might want to remove self.Tablename()
from DatabaseUtility.GetTable()
. The way you have your class defined, Table
is an attribute to the DatabaseUtility
class, but by including the parentheses you're trying to call it as if it were a method (like GetTable
is).
Upvotes: 1