相关疑难解决方法(0)

从Django QuerySet获取SQL

如何从QuerySet对象获取Django将在数据库中使用的SQL?我正在尝试调试一些奇怪的行为,但我不确定哪些查询会进入数据库.谢谢你的帮助.

python sql django django-queryset

279
推荐指数
5
解决办法
14万
查看次数

查询期间与MySQL服务器的连接丢失

我有一个巨大的表,我需要处理其中的所有行.我总是得到这个丢失的连接消息,我无法重新连接并将光标恢复到它的最后位置.这基本上就是我在这里的代码:

#
import MySQLdb

class DB:
  conn = None

  def connect(self):
    self.conn = MySQLdb.connect('hostname', 'user', '*****', 'some_table', cursorclass=MySQLdb.cursors.SSCursor)

  def query(self, sql):
    try:
     cursor = self.conn.cursor()
     cursor.execute(sql)
   except (AttributeError, MySQLdb.OperationalError):
     self.connect()
     cursor = self.conn.cursor()
     cursor.execute(sql)
   return cursor
#

#
db = DB()
sql = "SELECT bla FROM foo"
data = db.query(sql)

for row in data:
    do_something(row)
#
Run Code Online (Sandbox Code Playgroud)

但我总是得到这个:

#
Traceback (most recent call last):
  File "teste.py", line 124, in <module>
   run()
 File "teste.py", line 109, in run
   for row in data: …
Run Code Online (Sandbox Code Playgroud)

python mysql

53
推荐指数
6
解决办法
11万
查看次数

标签 统计

python ×2

django ×1

django-queryset ×1

mysql ×1

sql ×1