sqlite3 error: You did not supply a value for binding 1

后端 未结 3 1945
别那么骄傲
别那么骄傲 2020-12-21 11:24
def save():
    global editor
    conn = sqlite3.connect(\'address_book.db\')

    c = conn.cursor()

    recordID = delete_bo         


        
3条回答
  •  悲&欢浪女
    2020-12-21 12:25

    To avoid the situation reported as https://bugs.python.org/issue41638 i am using a wrapper. See also python sqlite insert named parameters or null for the solution of the more general problem on how to store dicts with different sets of keys which need to be mapped to different colums.

    def testBindingError(self):
            '''
            test list of Records with incomplete record leading to
            "You did not supply a value for binding 2"
            see https://bugs.python.org/issue41638
            '''
            listOfRecords=[{'name':'Pikachu', 'type':'Electric'},{'name':'Raichu' }]
            for executeMany in [True,False]:
                try:
                    self.checkListOfRecords(listOfRecords,'Pokemon','name',executeMany=executeMany)
                    self.fail("There should be an exception")
                except Exception as ex:
                    if self.debug:
                        print(str(ex))
                    self.assertTrue('no value supplied for column' in str(ex)) 
    

    which leads to:

    executeMany:

    INSERT INTO Pokemon (name,type) values (:name,:type)
    failed: no value supplied for column 'type'
    

    no executeMany:

    INSERT INTO Pokemon (name,type) values (:name,:type)
    failed: no value supplied for column 'type'
    record  #2={'name': 'Raichu'}
    

    The wrapper code is:

    def store(self,listOfRecords,entityInfo,executeMany=False):
            '''
            store the given list of records based on the given entityInfo
            
            Args:
              
               listOfRecords(list): the list of Dicts to be stored
               entityInfo(EntityInfo): the meta data to be used for storing
            '''
            insertCmd=entityInfo.insertCmd
            try:
                if executeMany:
                    self.c.executemany(insertCmd,listOfRecords)
                else:
                    index=0
                    for record in listOfRecords:
                        index+=1
                        self.c.execute(insertCmd,record)
                self.c.commit()
            except sqlite3.ProgrammingError as pe:
                msg=pe.args[0]
                if "You did not supply a value for binding" in msg:
                    columnIndex=int(re.findall(r'\d+',msg)[0])
                    columnName=list(entityInfo.typeMap.keys())[columnIndex-1]
                    debugInfo=""
                    if not executeMany:
                        if self.errorDebug:
                            debugInfo="\nrecord  #%d=%s" % (index,repr(record))
                    raise Exception("%s\nfailed: no value supplied for column '%s'%s" % (insertCmd,columnName,debugInfo))
                else:
                    raise pe
    

    for more details see storage/sql.py and tests/testSqlite3.py

提交回复
热议问题