简体   繁体   English

将python变量插入mysql表的行

[英]Insert python variables into rows of mysql table

I have the following code that parses data from an XML file. 我有以下代码可以解析XML文件中的数据。

import MySQLdb
from xml.dom import minidom

xmldoc = minidom.parse("report.xml")

parking_report = xmldoc.getElementsByTagName("parking_report")[0]

sensors = parking_report.getElementsByTagName("sensor")

for sensor in sensors:
    id = sensor.getElementsByTagName("id")[0].firstChild.data
    date = sensor.getElementsByTagName("date")[0].firstChild.data
    time = sensor.getElementsByTagName("time")[0].firstChild.data
    status = sensor.getElementsByTagName("status")[0].firstChild.data

    db = MySQLdb.connect(host="localhost",user="root",passwd="pass",db="parking_report")
    cur = db.cursor()
    cur.execute('INSERT INTO report_table (id, date, time, status) VALUES (id, date, time, status)')

    print(id, date, time, status)

Right now, it runs without errors and returns the id, date, time and status of each parking sensor. 现在,它可以正常运行,并返回每个停车传感器的ID,日期,时间和状态。 However, my mySQL table (parking_report) also has the columns ID, Date, Time and Status. 但是,我的mySQL表(parking_report)也具有ID,日期,时间和状态列。 I want to insert the data of those variables into my table under those columns. 我想将这些变量的数据插入到这些列下的表中。 (Note there are three separate sensors so I will need three rows of data in the end.) (请注意,有三个单独的传感器,因此最后我将需要三行数据。)

When I run this it does not insert into my table. 当我运行它时,它不会插入到我的表中。 Please Help! 请帮忙! Thank you. 谢谢。

You are not passing any parameters to the query. 您没有将任何参数传递给查询。

Instead of: 代替:

cur.execute('INSERT INTO report_table (id, date, time, status) VALUES (id, date, time, status)')

create a parameterized query: 创建一个参数化查询:

cur.execute("""INSERT INTO 
                   report_table 
                   (id, date, time, status) 
               VALUES
                   (%(id)s, %(date)s, %(time)s, %(status)s)""", 
            {'id': id, 
             'date': date, 
             'time': time, 
             'status': status})

There are other fixes you should apply to the code: 您还应该对代码应用其他修复程序:

  • define db and cursor variables before the loop so that you don't need to connect to the database and open a cursor on every iteration 在循环之前定义dbcursor变量,这样您就不必连接到数据库并在每次迭代时都打开一个游标
  • you should properly close the cursor and connection objects 您应该正确关闭游标和连接对象
  • instead of calling INSERT database query on each iteration step, consider gathering the data into a list and then insert once after the loop INSERT在每个迭代步骤中调用INSERT数据库查询,不如考虑将数据收集到一个列表中,然后在循环后插入一次

Hope that helps. 希望能有所帮助。

Well easiest way to insert python variables into rows of mysql table is: 将python变量插入mysql表行的最简单方法是:

cursor.execute(
   """INSERT INTO menu(col1, col2, col3) VALUES(%s, %s, %s)"""
   %(item1, item2, item3))

In your case: 在您的情况下:

cursor.execute(
   """INSERT INTO report_table(id, date, time, status) VALUES(%s, %s, %s, %s)"""
   %(id, date, time, status))

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

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