我正在尝试将一个查询的输出用于另一个查询,但是当我选择一个元素时,它会将输出作为,
[(8, u'O', 2, 16)]
[(8,)]
[ ]
所以第三个查询无法正常工作。我认为问题是我打印一个数字,但输出是数字列表,我试图解决它但仍然困在这里。
def OnClick(self, event):
name = event.GetEventObject().GetLabelText()
cursor= self.conn.execute("SELECT * FROM ELEMENT where SYMBOL=='%s'"%(name))
elements = cursor.fetchall()
print elements
cursor= self.conn.execute("SELECT ATOMIC_NUMBER FROM ELEMENT where SYMBOL IN ('%s')"%(name))
numbers = cursor.fetchall()
print numbers
cursor= self.conn.execute("SELECT * FROM LINK where ELEMENT_NUMBER = 'numbers'")
mnumbers = cursor.fetchall()
print mnumbers
#cursor= self.conn.execute("SELECT * FROM LINK where ELEMENT_NUMBER IN (SELECT ATOMIC_NUMBER FROM ELEMENT where SYMBOL IN ('%s')%(SYMBOL))")
#cursor= self.conn.execute("SELECT * FROM MOLECULE where MOL_NUMBER=='%d'"%(Mnumbers))
#molecules = cursor.fetchall()
#print molecules
答案 0 :(得分:0)
Pythons sqlite3
documentation在顶部说你不应该用你的值进行%s
次替换。
将变量值引入查询的正确方法是使用SQL参数:
...execute("SELECT * FROM ELEMENT where SYMBOL==?", [name])
...execute("SELECT ATOMIC_NUMBER FROM ELEMENT where SYMBOL = ?", [name])
atomicnumber = numbers[0][0] # extract value in first column of first row
...execute("SELECT * FROM LINK where ELEMENT_NUMBER = ?", [atomicnumber])