【问题标题】:Python Error connecting with MySQL与 MySQL 连接的 Python 错误
【发布时间】:2013-03-05 08:13:37
【问题描述】:
  1. 当我有 con.close()- mysql.connector.errors.OperationalError: [Errno 9] Bad file descriptor
  2. 时收到错误消息
  3. 我在删除 con.close() 时收到错误消息 - mysql.connector.errors.InterfaceError: 2055: Lost connection to MySQL server at 'localhost:3306', system error: 10054

有什么建议吗?谢谢
以下是我的查询:

import mysql.connector
class databaseConnection:
    def __init__ (self,settings ): 
        cur=settings.cursor()

        selectstmt=("SELECT qSQL FROM TBLTEST WHERE id = 4")
        cur.execute(selectstmt)
        res=cur.fetchone()
        qSQL=res[0]
        cur.execute(qSQL)
        qSQLresults=cur.fetchall()
        for row in qSQLresults:
            id= row[0]
            city= row[1]
            state=row[2]
            insertstmt=("""INSERT into FACTRESTTBL (id, city, state) 
                           values (%d, '%s', '%s')""" % (id, city, state))
            cur.execute(insertstmt)
            con.commit() 
        con.close() # 

print 'done'

con=mysql.connector.connect(host="localhost", 
                            user="root", 
                            password="test", 
                            database="test")
databaseobject = databaseConnection(con)

--

【问题讨论】:

  • 不应该是settings.close()吗?
  • 旁白:您应该将参数作为第二个参数传递给.execute(),以防止 SQL 注入攻击。
  • 我做了类似于:stackoverflow.com/questions/14863692/… 所以我使用了 con.close() 你如何“将参数作为第二个参数传递给 .execute() 以防止 SQL 注入攻击”跨度>
  • 像这样:cur.execute(insertstmt, (id, city, state))。您会将所有占位符更改为您正在使用的模块的适当占位符。有些使用?,有些使用%s
  • 无论哪种情况,您的连接似乎都被提前终止了。您可能可以将配置设置为更长的超时时间。

标签: python mysql


【解决方案1】:

当我在服务器上的 Vim 上运行脚本时,它可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 2016-02-12
    • 2020-02-01
    • 2012-07-25
    • 2018-08-19
    相关资源
    最近更新 更多