【问题标题】:Batch SQL INSERT in Django App在 Django App 中批量 SQL INSERT
【发布时间】:2018-08-18 11:27:40
【问题描述】:

我正在关注this example 将记录批量插入表中,但会对其进行修改以适合我的具体示例

sql='INSERT INTO CypressApp_grammatrix (name, row_num, col_num, gram_amount) VALUES {}'.format(', '.join(['(%s, %s, %s, %s)']*len(gram_matrix)),)
    #print sql

    params=[]
    for gram in gram_matrix:
        col_num=1
        for g in gram:            
            params.extend([(matrix_name, row_num, col_num, g)])
            col_num += 1
        row_num += 1
    print params

    with closing(connection.cursor()) as cursor:
        cursor.execute(sql, params)

但是,在这样做时,我收到此错误

return cursor._last_executed.decode('utf-8')
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 150, in __getattr__
return getattr(self.cursor, attr)
AttributeError: 'Cursor' object has no attribute '_last_executed'

我想知道我收到此错误的原因以及我可以做些什么来修复它,尽管我觉得问题可能出在我没有编写的与 MySQL 一起使用的代码上

def last_executed_query(self, cursor, sql, params):
    # With MySQLdb, cursor objects have an (undocumented) "_last_executed"
    # attribute where the exact query sent to the database is saved.
    # See MySQLdb/cursors.py in the source distribution.
    return cursor._last_executed.decode('utf-8')

【问题讨论】:

    标签: python-2.7 mysql-python django-1.5


    【解决方案1】:

    所以我不知道我是否只是拥有 MySQLdb 的旧副本或什么,但问题似乎出在 cursors.py 上。该文件中唯一可以找到_last_executed 的地方就是这里

    def _do_query(self, q):
        db = self._get_db()
        self._last_executed = q
        db.query(q)
        self._do_get_result()
        return self.rowcount
    

    但是,__init__ 没有将此变量设置为实例属性。它完全消失了。所以我冒昧地自己添加它并将其初始化为一些查询字符串。我认为任何人都会这样做,所以我只是添加了

    class BaseCursor(object):
    
    """A base for Cursor classes. Useful attributes:
    
    description
        A tuple of DB API 7-tuples describing the columns in
        the last executed query; see PEP-249 for details.
    
    description_flags
        Tuple of column flags for last query, one entry per column
        in the result set. Values correspond to those in
        MySQLdb.constants.FLAG. See MySQL documentation (C API)
        for more information. Non-standard extension.
    
    arraysize
        default number of rows fetchmany() will fetch
    
    """
    
    from _mysql_exceptions import MySQLError, Warning, Error, InterfaceError, \
         DatabaseError, DataError, OperationalError, IntegrityError, \
         InternalError, ProgrammingError, NotSupportedError
    
    def __init__(self, connection):
        from weakref import ref    
        ...
        self._last_executed ="SELECT * FROM T"
    
        ...
    

    现在游标对象确实有属性_last_executed,当这个函数出现时

    def last_executed_query(self, cursor, sql, params):
        # With MySQLdb, cursor objects have an (undocumented) "_last_executed"
        # attribute where the exact query sent to the database is saved.
        # See MySQLdb/cursors.py in the source distribution.
        return cursor._last_executed.decode('utf-8')
    

    base.py被调用,属性确实存在所以这个错误

    return cursor._last_executed.decode('utf-8')
    File "/usr/local/lib/python2.7/dist-
    packages/django/db/backends/mysql/base.py", line 150, in __getattr__
    return getattr(self.cursor, attr)
    AttributeError: 'Cursor' object has no attribute '_last_executed'     
    

    不会遇到。至少我认为它是这样工作的。无论如何,它为我解决了问题。

    【讨论】:

      猜你喜欢
      • 2010-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-06
      相关资源
      最近更新 更多