简体   繁体   English

2 Try / Except语句不能按正常顺序工作,但是当代码被翻转时可以工作

[英]2 Try / Except statement doesn't work in normal order but works when codes are flipped

Good day guys, I hope to get a little advice on this. 大家好,我希望对此有所建议。 I can't seem to get this 2 TRY/EXCEPT statement to run in the order I want them to. 我似乎无法让这2个TRY / EXCEPT语句按我想要的顺序运行。 They however, work great if I put STEP2 first then STEP1. 但是,如果我先放置STEP2,然后再放置STEP1,它们将非常有用。

This current code prints out only. 当前代码仅打印出来。

Transferred: x rows.

If flipped, they print both. 如果翻转,它们将同时打印。

Unfetched: x rows.
Transferred: x rows.

I tried: 我试过了:

  • Assigning individual cur.close() and db.commit() as per the examples here , didn't work either. 按照此处的示例分配单独的cur.close()和db.commit()也不起作用。 (Side question: Should I be closing/committing them individually nevertheless? Is that a general good practice or context-based?) (旁边的问题:尽管如此,我还是应该单独关闭/提交它们吗?这是一般的良好做法还是基于上下文?)

  • Using a cur.rowcount method for Step 2 as well as I thought maybe the problem was on the SQL side but, the problem still persists. 在第2步中使用cur.rowcount方法,我认为问题可能出在SQL端,但是问题仍然存在。

  • Did a search on SO and couldn't find any similar case. 在SO上进行了搜索,找不到类似的情况。

Running on Python 2.7. 在Python 2.7上运行。 Code: 码:

import MySQLdb
import os

#Initiate connection to database.
db = MySQLdb.connect(host="localhost",user="AAA",passwd="LETMEINYO",db="sandbox")
cur = db.cursor()

#Declare variables.
viewvalue = "1"

mainreplace =   (
                    "INSERT INTO datalog "
                    "SELECT * FROM cachelog WHERE viewcount = %s; "
                    "DELETE FROM cachelog WHERE viewcount = %s; "
                    % (viewvalue, viewvalue)
                )

balance =   (
                "SELECT COUNT(*) FROM cachelog "
                "WHERE viewcount > 1"
            )

#STEP 1: Copy and delete old data then print results.
try:
    cur.execute(mainreplace)
    transferred = cur.rowcount
    print "Transferred: %s rows." %(transferred)
except:
    pass

#STEP 2: Check for unfetched data and print results.
try:
    cur.execute(balance)
    unfetched = cur.fetchone()
    print "Unfetched: %s rows." % (unfetched)
except:
    pass

#Confirm and close connection.
cur.close()
db.commit()
db.close()

Pardon any of my un-Pythonic ways as I am still very much a beginner. 请原谅我的任何非Python方式,因为我仍然还是个初学者。 Any advice is much appreciated, thank you! 任何建议都非常感谢,谢谢!

You have two blaring un-Pythonic bits of code: the use of a bare except: without saying which exception you want to catch, and using pass in that except block so the exception is entirely ignored! 您有两个非Python的代码段:使用裸except:不说您想捕获哪个异常,以及在该except块中使用pass ,这样该异常将被完全忽略!

The problem with code like that is that if something goes wrong, you'll never see the error message, so you can't find out what's wrong. 此类代码的问题在于,如果出现问题,您将永远不会看到错误消息,因此无法找出问题所在。

The problem is perhaps that your "mainreplace" query deletes everything from the "cachelog" table, so the "balance" query after it has no rows, so fetchone() fails, throws an exception and the line after it is never executed. 问题可能是您的“ mainreplace”查询从“ cachelog”表中删除了所有内容,因此“ balance”查询没有行,因此fetchone()失败,引发异常,并且从不执​​行该行。 Or maybe something completely different, hard to tell from here. 或者也许是完全不同的东西,从这里很难说出来。

If you didn't have that try/except there, you would have had a nice error message and you wouldn't have had to ask this question. 如果您没有该try / except,那么您将收到一条不错的错误消息,而不必提出这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM