2016-06-10 48 views
0

我想插入,我用下面的代碼創建了一個數據庫表中的一些值:插入vriable到表QGIS

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 

的使用問題,在過去cur.execute來源於%S ,這個函數最多需要2個參數,但有3個因爲%s使用。那麼你有解決方案或想法來幫助我嗎?我想用用戶給出的名字填充我的列表,並用來自表格的值填充它。

感謝您關注此事!

回答

0

我會寫這樣的而不是:

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]))