gpt4 book ai didi

python - 如何将 lineedit 中的内容插入到 MySQL 中?

转载 作者:行者123 更新时间:2023-11-29 12:21:25 27 4
gpt4 key购买 nike

我正在制作一个表单,允许我向数据库添加问题。我已经编写了代码,但是当我尝试它时,我收到此错误('AttributeError:'module'对象没有属性'GetTable')

错误在于我的代码的这一部分'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()

这是我的数据库代码:

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)

我做错了什么以及如何纠正?

最佳答案

我需要查看一些额外的代码才能确定(请参阅我的评论)。但目前我的猜测是,这与您首次实例化或引用 dbu 变量/属性的方式有关。如果您所做的只是从另一个文件导入它,因为 dbu 是在该文件的 main() 中创建的(在您提供的代码片段中),这可能就是问题所在。例如,这是不正确:

from whatever import dbu  # wrong
from whatever.main import dbu # Also wrong. In fact I don't think this would even work

相反,直接将 DatabaseUtility 导入到脚本中,然后实例化 dbu,如下所示:

from whatever import Database Utility
class SomeClass(object):
...
def startDB(self, db, Tablename):
self.dbu = DatabaseUtility(db, Tablename)

我猜这是因为错误消息说模块没有属性,而不是提及实际的类。以下是我期望看到的错误示例:

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'

注意它是如何说MyClass是问题的,而不是“模块”。

可能不相关,您可能需要从 DatabaseUtility.GetTable() 中删除 self.Tablename()。按照您定义类的方式,TableDatabaseUtility 类的属性,但通过包含括号,您可以尝试调用它就好像它是一个方法(就像GetTable一样)。

关于python - 如何将 lineedit 中的内容插入到 MySQL 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28949294/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com