所以我正在使用Python MySQL连接器,我正在尝试编写一些代码来选择一些行,具体取决于该行是否符合我提供的条件。该表有一个化学品专栏和一个加州(及其他)县的专栏
以下所有代码行都通过MySQL控制台完美运行:
SELECT wellName, approximateLatitude, approximateLongitude, chemical, result, units, date, county FROM chemicaldata WHERE (county='YOLO') AND (chemical='CR6')
SELECT wellName, approximateLatitude, approximateLongitude, chemical, result, units, date, county FROM chemicaldata WHERE (chemical='CR6')
SELECT wellName, approximateLatitude, approximateLongitude, chemical, result, units, date, county FROM chemicaldata WHERE (chemical='CR')
SELECT wellName, approximateLatitude, approximateLongitude, chemical, result, units, date, county FROM chemicaldata WHERE (county='YOLO') AND (chemical='CR')
SELECT wellName, approximateLatitude, approximateLongitude, chemical, result, units, date, county FROM chemicaldata WHERE (county='YOLO') AND (chemical='NO3')
对于上面的每个语句,我将语句放在引号中并通过python执行它。然后我尝试运行一个简单的脚本。例如
query = "SELECT wellName, approximateLatitude, approximateLongitude, chemical, result, units, date, county FROM chemicaldata WHERE (county='YOLO') AND (chemical='CR')"
cursor.execute(query)
i = 0
for (wellName,approximateLatitude,approximateLongitude,chemical,result,units,date,county) in cursor:
print i
i = i+1
你们能猜出Python脚本中的上述5个语句中的哪一个?只有第三和第五......这对我来说没有意义,因为它们几乎是同样的事情,接受我改变了化学物质,它们都在MySQL命令行上完美运行。更具体地说,当我说命令对Python不起作用时,我在循环中的计数器只是在中间冻结并停止做任何事情。对于第三个和第五个语句,循环最终退出并在循环后开始打印出来。任何人都可以帮我确定可能出错的地方或我调试代码的不同方法吗?我很感激帮助!
答案 0 :(得分:0)
尝试在查询中使用斜杠。
query = "SELECT wellName, approximateLatitude, approximateLongitude, chemical, result, units, date, county FROM chemicaldata WHERE (county=\'YOLO\') AND (chemical=\'CR\')"
cursor.execute(query)
i = 0
for (wellName,approximateLatitude,approximateLongitude,chemical,result,units,date,county) in cursor:
print i
i = i+1
告诉我它是否有效。