Why am I getting this error IndexError: tuple index out of range

Why am I getting this error IndexError: tuple index out of range

我正在尝试从数据库中提取行。它拉出前两行,但不会拉出第 6 行和第 7 行,它说这个错误:tuple index out of range.

db = sqlite3.connect('da_destination_history')
cursor = db.cursor()
cursor.execute('''SELECT time, dest_lat, dest_lng, source_lat, source_lng FROM destination_history''')
rows = cursor.fetchall()

for row in rows:
    print('{0} : {1}, {2}'.format(row[0], float(row[1])/1000000, float(row[2])/1000000))
    print('{0} : {6}, {7}'.format(row[0], float(row[6])/1000000, float(row[7])/1000000))
    ttime = row[0]
    dlong = float(row[1])/1000000
    dlat = float(row[2])/1000000

kml = simplekml.Kml()
kml.newpoint(name="andriod2", coords=[((float(row[2])/1000000),(float(row[1])/1000000))])
kml.newpoint(name="andriod", coords=[((float(row[7])/1000000),(float(row[6])/1000000))])
kml.save("andriod.kml")

正如 Martijn 所指出的,row[6] 的索引看起来很可疑,因为您只有 select 5 列。

字符串上的这个format函数也有问题:

'{0} : {6}, {7}'.format(row[0], float(row[6])/1000000, float(row[7])/1000000)

as 67 between {} 表示 format() 的参数,您只提供 3 个参数(索引为 {0}{1} 对应 {2}

如果您从某些包含更多列的示例中复制此代码 selected,您应该确保更新所有索引,包括行中的以及格式说明中的索引。