在使用Python(Python-2.7)时,我的数据库(MySQLdb)出现问题。我有3列的表,id,message1,message2。例如
我希望我的程序搜索列message1以找到“Hi”,如果该消息不存在,则搜索列message2以找到它,如果它存在,则发送“Hello:)”如果消息在两列中都不存在,我将随机发送消息。但这并不像我想的那么明显。这是我的密码
if is_send==0:
try:
cursor.execute("""SELECT message1 FROM Messages WHERE message2 LIKE (%s) limit 1""",('%' +text+'%',))
data=cursor.fetchall()
time.sleep(3)
send(data)
is_send=1
print data
print" - sent message 2"
except Exception as sending_message:
print sending_message
if is_send==0:
try:
cursor.execute("""SELECT message2 FROM messages WHERE message1 LIKE (%s) limit 1""",('%' +text+'%',))
data=cursor.fetchall()
time.sleep(3)
send(data)
is_send=1
print data
print " - sent message 3"
except Exception as sending_message:
print sending_message
if is_send==0:
cursor.execute("""SELECT tresc FROM Wiadomosci.tabwiadprzych ORDER BY rand() limit 1""")
data=cursor.fetchall()
time.sleep(3)
send(data)
print data
print " - sent message 4"
当我试图搜索列message1时,元组索引超出范围,而且“1064”,您的SQL语法有错误 在《选择》中,我试图写作
^{pr2}$或者
("""SELECT message1 FROM messages WHERE message2 LIKE (%s) limit 1""", text)
或者
("""SELECT message1 FROM messages WHERE message2 LIKE (%%s%%) limit 1""", [text])
甚至是
query=("SELECT message1 FROM messages WHERE message2 LIKE (%s) limit 1")
cursor.execute(query,(tekst,))
它不会起作用的。我不知道在这种情况下我能做什么。也许尝试有问题:
发送随机消息(消息4)正常工作,但程序总是尝试发送消息2,甚至不检查第二个或第三个if
目前没有回答
相关问题 更多 >
编程相关推荐