English 中文(简体)
三. 关于MySQLdb的最新错误
原标题:Catching update errors on MySQLdb
  • 时间:2009-11-24 05:30:51
  •  标签:
  • python
  • mysql

我的职能是,从CSV档案中更新MySQL表格。 MySQL表载有客户账户号码——这是我与CSV档案相比使用的数字。 在某些时候,一些询问将失败,因为与CSV档案相比的账户数目尚未增加。

我如何从在更新过程中失败的CSV档案中获得记录? 我希望将这些记录存放在一个单独的档案中,然后在成功更新所有记录之前重新阅读。

以下是更新亚洲开发银行的职能。

def updateDatabase(records, options):
    """Update database"""
    import re # Regular expression library
    import MySQLdb

    # establish DB connection
    try:
         db = MySQLdb.connect(host="localhost", user="root", passwd="", db="demo")
    except MySQLdb.Error, e:
         print "Error %d: %s" % (e.args[0], e.args[1])
         sys.exit (1)
    # create cursor
    cursor = db.cursor()
    # tell MySQLdb to turn off auto-commit
    db.autocommit(False) 

    # inform the user that this could take a while
    if len(records) > 499:
        print  This process can take a while. 

    print  Updating the database now... 
    # this is the actual loop
    maxrecords = len(records)
    for record in records:
        account_no, ag_1to15, ag_16to30, ag_31to60, ag_61to90, ag_91to120, beyond_120, total, status, credit_limit = record
        if re.match( 1000 , account_no):
            query = """UPDATE sys_accountscf SET cf_581 = %s, cf_583 = %s, cf_574 = %s, cf_575 = %s, cf_576 = %s, cf_577 = %s, cf_579 = %s, cf_585 =  %s , cf_558 = %s WHERE cf_538 = %s"""
        else:
            query = """UPDATE sys_accountscf SET cf_580 = %s, cf_582 = %s, cf_568 = %s, cf_569 = %s, cf_571 = %s, cf_572 = %s, cf_578 = %s, cf_584 =  %s , cf_555 = %s WHERE cf_535 = %s"""
        cursor.execute(query % (ag_1to15, ag_16to30, ag_31to60, ag_61to90, ag_91to120, beyond_120, total, status, credit_limit, account_no))
    # commit all changes and close database connection      
    try:
        db.commit()
    except:
        db.rollback()
    cursor.close()
    db.close()
最佳回答

An update query returns the number of rows affected. Checking the Cursor.rowcount after you made am execute will give that number. If it is not 1, that that update row failed.

问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签