将vriable插入表QGI

2024-10-01 22:36:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在使用以下代码创建的数据库表中插入一些值:

conn = db.connect('test.sqlite')
    cur = conn.cursor()
    sql= 'SELECT InitSpatialMetadata()'
    cur.execute(sql)
    conn.commit()
#connection to the DB

qid=QInputDialog()    
tablefacette,ok=QInputDialog.getText(qid,u'Table name',None,QLineEdit.Normal,None)
tablefacette=str(tablefacette)
#asking user for a table name

cur.execute('DROP TABLE IF EXISTS %s' %tablefacette)
    cur.execute('CREATE TABLE %s (IDface INTEGER NOT NULL PRIMARY KEY, Xp1 FLOAT NOT NULL, \
    Yp1 FLOAT NOT NULL, Zp1 FLOAT NOT NULL, Xp2 FLOAT NOT NULL, Yp2 FLOAT NOT NULL, \
    Zp2 FLOAT NOT NULL, Xp3 FLOAT NOT NULL, Yp3 FLOAT NOT NULL, Zp3 FLOAT NOT NULL)' %tablefacette)
#creation of the table

    for i in range(0,len(IDface)):
        cur.execute("INSERT INTO %s" %tablefacette, "(IDface, Xp1, Yp1, Zp1, Xp2, Yp2, Zp2, Xp3, Yp3, Zp3) VALUES \
        (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (IDface[i], Xp1[i], Yp1[i], Zp1[i],  Xp2[i], Yp2[i], Zp2[i],  Xp3[i], Yp3[i], Zp3[i]))
        conn.commit()
#filling the table with values Xp[i] etc which come from a list that I created

问题来自于上一次使用的%s当前执行,此函数最多接受2个参数,但由于使用了%s,因此有3个参数。那么你有什么解决办法或者想法来帮助我吗?我想用用户给出的名称填充我的列表,并用来自表的值填充它。在

感谢您对此事的关注!在


Tags: theexecutetablenotfloatconnnullcur
1条回答
网友
1楼 · 发布于 2024-10-01 22:36:31

我改成这样写:

cur.execute("INSERT INTO %s (IDface, Xp1, Yp1, Zp1, Xp2, Yp2, Zp2, Xp3, Yp3, Zp3) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", %(table_name, Xp1[i], Yp1[i], Zp1[i],  Xp2[i], Yp2[i], Zp2[i],  Xp3[i], Yp3[i], Zp3[i]))

相关问题 更多 >

    热门问题