简体   繁体   English

无法使用python将存储在变量中的数据插入到MYSQL数据库表中

[英]unable to insert data that are stored in variables in to an MYSQL database table using python

am trying to insert the data entered into the web form into database table,i am passing the data to the function to insert the data,but it was not successful below is my code 我正在尝试将输入到Web表单中的数据插入数据库表中,我正在将数据传递给函数以插入数据,但是下面的操作不成功,这是我的代码

def addnew_to_database(tid,pid,usid,address,status,phno,email,ord_date,del_date):
    connection = mysql.connector.connect(user='admin_operations', password='mypassword',host='127.0.0.1',database='tracking_system')
    try:
        print tid,pid,usid,address,status,phno,email,ord_date,del_date
        cursor = connection.cursor()
        cursor.execute("insert into track_table (tid,pid,usid,address,status,phno,email,ord_date,del_date) values(tid,pid,usid,address,status,phno,email,ord_date,del_date)")
        cursor.execute("insert into user_table (tid,usid) values(tid,usid)")
    finally:
        connection.commit()
        connection.close()

You should pass the variables as an argument to .execute instead of putting them in the actual query. 您应该将变量作为.execute的参数.execute而不是将其放入实际查询中。 Eg: 例如:

cursor.execute("""insert into track_table 
                    (tid,pid,usid,address,status,phno,email,ord_date,del_date)
                  values (%s,%s,%s,%s,%s,%s,%s,%s,%s)""", 
               (tid,pid,usid,address,status,phno,email,ord_date,del_date))
cursor.execute("""insert into user_table 
                    (tid,usid) 
                  values (%s,%s)""",(tid,usid))

You should tell us what API you are using and what the error code is. 您应该告诉我们您使用的是什么API,错误代码是什么。 You should define the values within the execution, right now within the sql statement as a string they are not referencing anything. 您应该在执行中定义值,现在在sql语句中将它们定义为不引用任何内容的字符串。

Typically when you use a variable name inside of a sql statement this way, you need to indicate that it is a variable you are binding data to. 通常,当您以这种方式在sql语句中使用变量名时,需要指出它是您要将数据绑定到的变量。 This might be replacing it with (1,2,3,4..) or (%s,%s,...) that corresponds to an ordered list or using variable names (:tid,:pid,...) that you then define the values of with a dictionary as the second argument of execute(). 这可能是用(1,2,3,4 ..)或(%s,%s,...)替换了有序列表或使用了变量名(:tid,:pid,...)然后使用字典定义execute的第二个参数的值。

Like this: 像这样:

track_table_data = [tid,pid,usid,address,status,phno,email,ord_date,del_date]
user_table_data = [tid,usid]
cursor.execute("insert into track_table (tid,pid,usid,address,status,phno,email,ord_date,del_date) values(1,2,3,4,5,6,7,8,9)", track_table_data)
cursor.execute("insert into user_table (tid,usid) values(1,2)",user_table_data)

or 要么

cursor.execute("insert into track_table (tid,pid,usid,address,status,phno,email,ord_date,del_date) values(:tid,:pid,:usid,:address,:status,:phno,:email,:ord_date,:del_date))", {'tid':tid,'pid':pid,'usid':usid,'address':address,'status':status,'phno':status,'email':email,'ord_date':ord_date,'del_date':del_date})
cursor.execute("insert into user_table (tid,usid) values(:tid,:usid)",{'tid':tid,'usid':usid})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM