Using prepared statements with mysql in python

后端 未结 3 1836
醉酒成梦
醉酒成梦 2020-12-03 15:42

I am trying to use SQL with prepared statements in Python. Python doesn\'t have its own mechanism for this so I try to use SQL directly:

sql = \"PREPARE stm         


        
3条回答
  •  醉酒成梦
    2020-12-03 16:14

            import mysql.connector
            db_con=mysql.connector.connect(host='',
                database='',
                user='',
                password='')
    
            cursor = db_con.cursor(prepared=True,)
            #cursor = db_con.cursor(prepared=True)#IT MAY HAVE PROBLEM
    
            sql = """INSERT INTO table (xy,zy) VALUES (%s, %s)"""
            input=(1,2)
            cursor.execute(sql , input)
            db_con.commit()
    

    SELECT STMT

            sql = """SELECT * FROM TABLE WHERE XY=%s ORDER BY id DESC LIMIT 1 """
            ID=1
    
            input=(ID,)
            #input=(ID)# IT MAY HAS PROBLEM
            cursor.execute(sql, input)
            data = cursor.fetchall()
            rowsNumber=cursor.rowcount
    

提交回复
热议问题