Python 3和SQLite3 -为什么这个函数是未定义的?

pes8fvy9  于 2023-10-21  发布在  Python
关注(0)|答案(1)|浏览(102)
import sqlite3 as sq3

class DataModule:
    
    def del_tbl(tbl_Name):
        sq3.Connection("csm").cursor().execute("DROP TABLE IF EXSISTS '{}'".format(tbl_Name))
        print("Table [{}] has beeen deleted".format(tbl))
    
    def createTable(tableName,cmd):
        del_tbl("tbl_login")
        del_tbl("tbl_address")
        cmd=str.join(", ",cmd)
        sq3.Connection("csm").cursor().execute("CREATE TABLE IF NOT EXISTS {} ({}); ".format(tableName, cmd))

输出现在说:

Error: ("name 'del_tbl' is not defined",)
Error: ("name 'del_tbl' is not defined",)
Press any key to continue . . .

我肯定我错过了一些很简单的东西

p5fdfcr1

p5fdfcr11#

这不是一个函数,而是一个类方法,你必须用self调用它。prefix(并将其添加到方法定义中):

import sqlite3 as sq3

class DataModule:
    
    def del_tbl(self, tbl_Name):
        sq3.Connection("csm").cursor().execute("DROP TABLE IF EXSISTS '{}'".format(tbl_Name))
        print("Table [{}] has beeen deleted".format(tbl))
    
    def createTable(self, tableName,cmd):
        self.del_tbl("tbl_login")
        self.del_tbl("tbl_address")
        cmd=str.join(", ",cmd)
        sq3.Connection("csm").cursor().execute("CREATE TABLE IF NOT EXISTS {} ({}); "

相关问题