Python事务中的几个查询

时间:2014-06-17 23:19:48

标签: python mysql transactions

我正在将脚本从另一种语言迁移到Python。我对数据库调用的具体细节进行了淡化......但这就是文件的样子。我故意使一些查询失败,因为我正在测试事务并且它没有回滚()在强制错误之前执行的查询。我对如何处理Python的事务感到有些困惑,我遵循的例子是this one,它是一个循环,其中有几个查询嵌套在事务中,所以我根据我的理解调整了代码。

    #!/usr/bin/python

import MySQLdb
import thread
import os

# Open database connection
# added local_infile=1 to allow the import to work, otherwise you get an error
db = MySQLdb.connect(CONNECTION ARGS...)

# define our function that will be called from our thread
def import_queued_file(conn,distid):

    # prepare a cursor object using cursor() method
    cursor = conn.cursor()

    # total lines imported for all files for a distributor
    total_lines_imported = 0

    # current lines imported for each file on each iteration
    current_lines_imported = 0

    # default this to 0, this will have the total lines for our imports on each iteration
    previous_lines_imported = 0

    # initialize the file exists flag to 0
    file_exists = 0

    # sql statement to retrieve the file(s) for a specific distributor
    sql = """
    SELECT 
        ...
    FROM ...
    WHERE ...
    """

    # execute the sql statement
    cursor.execute(sql)

    # if we have records, execute the code below
    if (cursor.rowcount > 0):

        # set the records to the files variable
        files = cursor.fetchall()

        # set a variable to count iterations
        # we'll use this to determine if we need to drop the table
        cnt = 0

        # keep track of the total number of lines imported per distributor (may be multiple files)
        lines_imported = 0

        # loop the recordset
        for col in files:

            # increment the cnt variable
            cnt += 1

            # set file_exists to 0 at the beginning of the iteration
            file_exists = 0

            # set some variables to be used in our sql load data statement
            var1 = col[1]
            var2 = col[2]
            ....


            # this is the path of our file that we will be using for MySQL LOAD DATA also
            # TODO: REFACTOR SO THAT THE /home/webex/backup/ IS NOT HARD CODED
            inventoryfile = "/path/to/file/%s" % (filepath)

            # check to see if we have a file
            if (os.path.exists(inventoryfile)):
                try:

                    # set file exists to true
                    file_exists = 1

                    # if cnt > 1, it means we have more than 1 file for this distributor
                    # only drop the table if this is the first iteration
                    if (cnt == 1):

                        # drop table sql statement
                        sql = "DROP TABLE IF EXISTS %s" % (temptable)

                        # execute the sql command
                        cur = conn.cursor()
                        cur.execute(sql)
                        cur.close()

                    # assign the create table statement to the sql variable
                    sql = """
                    CREATE TABLE IF NOT EXISTS 
                    .......
                    .......
                    ) ENGINE=MyISAM DEFAULT CHARSET=utf8
                    """ % (temptable)

                    # execute the sql statement
                    cur = conn.cursor()
                    cur.execute(sql)
                    cur.close()

                    # query the temptable to see if we have any records
                    sql = "SELECT COUNT(0) AS total FROM %s" % (temptable)
                    cur = conn.cursor()
                    cur.execute(sql)
                    cur.close()

                    # get the count of how many records exist in the database
                    number_of_line_items = cur.fetchall()
                    previous_lines_imported = number_of_line_items[0][0]

                    # load data local infile sql statement
                    sql = """
                    LOAD DATA LOCAL INFILE ...
                    """

                    # execute the load data infile sql statement
                    cur = conn.cursor()
                    cur.execute(sql)
                    cur.close()

                    # clean up the table by removing...
                    # rows that don't have a part_number,
                    # rows that have part_number's less than 3 characters
                    sql = """
                    DELETE FROM ...
                    """ % (temptable)

                    # execute the delete query
                    cur = conn.cursor()
                    cur.execute(sql)
                    cur.close()

                    # query the temptable to see if we have any records after the import
                    sql = "SELECT COUNT(0) AS total FROM %s" % (temptable)

                    # execute the count query
                    cur = conn.cursor()
                    cur.execute(sql)
                    cur.close()

                    # get the count of how many records exist in the database after the import
                    number_of_line_items = cur.fetchall()

                    # get the current lines imported
                    current_lines_imported = number_of_line_items[0][0] - previous_lines_imported

                    # add the current lines imported to the total lines imported
                    total_lines_imported += current_lines_imported

                    # update distributor_file_settings table last_updated_on field
                    sql = """
                    UPDATE ...
                    """ % (file_id,distributor__id)

                    print sql

                    # execute the update query
                    cur = conn.cursor()
                    cur.execute(sql)
                    cur.close()

                    # close cursor
                    conn.commit()
                except:
                    conn.rollback()

    # no records exists for this distributor                
    else:
        print "dist doesn't exist"
    cursor.close()

import_queued_file(db,42)

# prepare a cursor object using cursor() method
cursor = db.cursor()

# select distinct file settings
sql = """
SELECT ...
"""

# disconnect from server
db.close()

1 个答案:

答案 0 :(得分:1)

一次又一次地检查代码后,问题恰好是表类型。将其更改为INNODB后,它按预期工作。