python - sqlite InterfaceError: Error binding parameter 0 - probably unsupported type

烈酒焚心 提交于 2021-01-28 07:22:27

问题


i am using sqlite3 to store values into database, My problem is i have created lineedits through which i extract values and store it to variables and pass this variables to insert query but i am getting above error at line values(?,?,?,?,?)......if i hard quote the values directly to variables it gets saved into database what is the mistake am i doing? please help.......here is my code

            self.uname = self.le1.text()
            self.passwd = self.le2.text()
            self.permssn = self.le3.text()
            self.queryCurs.execute('''CREATE TABLE IF NOT EXISTS USER
            (USERNAME TEXT NOT NULL, PASSWORD TEXT NOT NULL, PERMISSION TEXT NOT NULL)''')
            self.queryCurs.execute('''INSERT INTO USER(USERNAME, PASSWORD, PERMISSION) 
            VALUES(?,?,?)''',(self.uname,self.passwd,self.permssn))
            print ('inserted row')
            self.createDb.commit()

where am i going wrong?


回答1:


The method from where you're getting the text "le1.text()", doesn't stores the value in string format. I was getting the same error, sorted out that my data type was mismatched. I converted this value to string and it worked fine.

Also don't miss to add comma in the end because the execute query takes tulip.

self.queryCurs.execute('''INSERT INTO USER(USERNAME, PASSWORD, PERMISSION) 
        VALUES(?,?,?)''',(str(self.uname),str(self.passwd),str(self.permssn),))



回答2:


Values should be in touple (square brackets), like this:

self.queryCurs.execute("INSERT INTO USER(USERNAME, PASSWORD, PERMISSION) VALUES(?,?,?)",[self.uname, self.passwd, self.permssn])

Since you add values at the same order as columns in your table are, you don't really need them in your query, so this will also work:

self.queryCurs.execute("INSERT INTO USER VALUES(?,?,?)",[self.uname, self.passwd, self.permssn])


来源:https://stackoverflow.com/questions/21231264/python-sqlite-interfaceerror-error-binding-parameter-0-probably-unsupported

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!