python 从表中选择列,其中列 = self.variable

标签 python oop sqlite python-3.6

我正在尝试根据给定的参数选择列中的数据。 但出现错误提供的绑定(bind)数量不正确

class TheClass():

    def __init__(self):
        self.con = connect('mydb.db')
        self.cur = self.con.cursor()

    def getonedata(self, text):
        self.variable = text
        self.cur.execute("""SELECT username, age, password FROM usertbl WHERE username =?""",(self.variable))
        self.result = self.cur.fetchone()
        return self.result

    def getoneresult(self, text):

        for row in self.getonedata(text):
            self.row1 = row[0]
            self.row2 = row[1]
            self.row3 = row[2]

            print("{} {} {}".format(self.row1, self.row2, self.row3))
app = TheClass()
app.getonedata('IgiveUP')

最佳答案

您必须将参数作为元组提供给 execute 方法,并且在 Python 中,必须使用附加逗号指定单项元组:

self.cur.execute("""SELECT username, age, password FROM usertbl WHERE username =?""",(self.variable,))

关于python 从表中选择列,其中列 = self.variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51571191/

相关文章:

javascript - 无法在 setTimeout() 函数调用的对象函数中获取具有 "this"属性的对象变量

ios sqlite 未使用 Xcode 打开(但从 cli) - ionic

c++ - 使用 TCL 编写嵌入式 SQLite 脚本

python - 使用 python 将 utf-16 编码的 rss 流存储到 sqlite3 中的正确过程是什么

Python:用于元组的 Pandas DataFrame

python - Flask 无法导入枚举?未定义错误 : 'enumerate' is undefined

python 字符串语法错误 += 运算符

c++ - 将所有函数转换为类

c++ - Arduino类初始化执行

python - 如何从代码中读取失败队列(rq)的回溯?