Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Python编写的CRUD。。。索引器错误:元组索引超出范围_Python_Crud - Fatal编程技术网

用Python编写的CRUD。。。索引器错误:元组索引超出范围

用Python编写的CRUD。。。索引器错误:元组索引超出范围,python,crud,Python,Crud,我必须使用Python进行一些选择和更新。这是一门全新的语言,在执行以下(简单)查询时,我在语法方面遇到了一些问题: 以下是我的方法 def retrieve(self, key): cursor = self._db.execute('select from {}'.format(self._table)) return dict(cursor.fetchone()) def update(self, row): self._

我必须使用Python进行一些选择和更新。这是一门全新的语言,在执行以下(简单)查询时,我在语法方面遇到了一些问题:

以下是我的方法

    def retrieve(self, key):
        cursor = self._db.execute('select  from {}'.format(self._table))
        return dict(cursor.fetchone())

    def update(self, row):
        self._db.execute(
            'update {} set BASE_PRICE = ? where {}'.format(self._table), 
            (row['BASE_PRICE'], row['where']))
        self._db.commit()

    def disp_rows(self):
        cursor = self._db.execute('select IDENT, CONTRACT_CLASS, BASE_PRICE from {} order 
        by BASE_PRICE'.format(self._table))
        for row in cursor:
            print('  {}: {}: {}'.format(row['IDENT'], row['CONTRACT_CLASS'], 
            row['BASE_PRICE']))

    def __iter__(self):
        cursor = self._db.execute('select * from {} order by  
        BASE_PRICE'.format(self._table))
        for row in cursor:
            yield dict(row)


    def main():
    db_cities = database(filename = 'insurance.sqlite', table = 'CITIES')
    db= database(filename = 'insurance.sqlite', table = 'CONTRACTS')
    db_cars = database(filename = 'insurance.sqlite', table = 'CARS')

    print('Retrieve rows')
    for row in db: print(row)

    print('Update rows')
    db.update({'where': 'CONTRACT_CLASS' >= 10', 'BASE_PRICE'= 1000})
    for row in db: print(row)

    print('Retrieve rows after update')
    for row in db: print(row)

Thanks in advance for your help!

您在
self.\u表
后面放错了括号

'UPDATE{table\u name}SET BASE\u PRICE={BASE\u PRICE}其中{condition}'。格式(
表\u name=self.\u表,
基准价格=行[“基准价格”],
条件=行['where']
)
我还做了一些代码清理


另一个问题:为什么不使用ORM?

您现在了解了
format()
的用法了吗?事实上,我在大括号中使用名称的方式更加灵活,没有大括号也可以工作,但可读性不是很好。当我必须进行SQL联接时,如何在db.execute中引用多个表?def retrieve(self,key):cursor=self.\u db.execute('select*from{}.format(self.\u table))返回dict(cursor.fetchone())'def retrieveSmCityCust(self,key):cursor=self.\u db.execute('SELECT CONTRACTS.CUSTOMER_NAME,CONTRACTS.CUSTOMER.CITY,CITIES.POPULATION FROM CONTRACTS JOIN CITIES ON CONTRACTS.CUSTOMER_CITY=CITIES.IDENT where{condition}.format(self_table),condition=row['where'])return dict(cursor.fetchall())抛出错误。format(self_table),condition=row['where']NameError:全局名称“self_table”未定义RM=对象关系映射器;这意味着您不需要再编写普通SQL。请看一看或仅举一个例子。在上面的代码中,您忘记了
self
后面的点(应该是
self.\u table
)。您还将括号再次放错了
self.\u table
。您的错误并不都是python特有的。您必须小心设置括号、大括号、圆点等。
>'update {} set BASE_PRICE = ? where {}'.format(self._table), (row['BASE_PRICE'], >row['where']))
>IndexError: tuple index out of range
    def retrieve(self, key):
        cursor = self._db.execute('select  from {}'.format(self._table))
        return dict(cursor.fetchone())

    def update(self, row):
        self._db.execute(
            'update {} set BASE_PRICE = ? where {}'.format(self._table), 
            (row['BASE_PRICE'], row['where']))
        self._db.commit()

    def disp_rows(self):
        cursor = self._db.execute('select IDENT, CONTRACT_CLASS, BASE_PRICE from {} order 
        by BASE_PRICE'.format(self._table))
        for row in cursor:
            print('  {}: {}: {}'.format(row['IDENT'], row['CONTRACT_CLASS'], 
            row['BASE_PRICE']))

    def __iter__(self):
        cursor = self._db.execute('select * from {} order by  
        BASE_PRICE'.format(self._table))
        for row in cursor:
            yield dict(row)


    def main():
    db_cities = database(filename = 'insurance.sqlite', table = 'CITIES')
    db= database(filename = 'insurance.sqlite', table = 'CONTRACTS')
    db_cars = database(filename = 'insurance.sqlite', table = 'CARS')

    print('Retrieve rows')
    for row in db: print(row)

    print('Update rows')
    db.update({'where': 'CONTRACT_CLASS' >= 10', 'BASE_PRICE'= 1000})
    for row in db: print(row)

    print('Retrieve rows after update')
    for row in db: print(row)

Thanks in advance for your help!