gpt4 book ai didi

python - 如何使用python插入mysql数据库

转载 作者:行者123 更新时间:2023-11-29 22:45:04 24 4
gpt4 key购买 nike

我正在编写一个Python脚本,允许插入数据库mysql。它没有给我任何错误,并且“select *”Python代码给了我我发布的列表,但是当我在终端 mysql -u root -pselect * 中写入时,它给我仅从终端放入数据库的行 - 我从不存在的代码中放入的行。

请你帮帮我。谢谢。

cur.execute("INSERT INTO ALL_DEVICES(HOST_NAME,ADDRESS_MAC,ADDRESS_IP,TIME_SCAN,DATE,ETAT) VALUES(%s,%s,%s,%s,%s,%s)",(host_name1, address_mac1,address_ip1,time_scan1,date1,etat))   

cur.execute("SELECT * FROM ALL_DEVICES")

rows = cur.fetchall()

print rows

结果是:

((2L, 'LKK', 'LLKK', 'KLK', 'KK', 'KJH', 'KLO'), (4L, 'LKK', 'LLKK', 'KLK', 'KK', 'KJH', 'KLO'), (5L, 'host_name1', 'address_mac1', 'address_ip1', 'tim', 'da', 'etat'), (9L, 'host_name1', 'address_mac1', 'address_ip1', 'tim', 'da', 'etat'), (19L, '', 'D8:3C:69:AA:02:E9', '192.168.1.3', '14:07', '21/03/15', 'Interdit'), (20L, '', 'D8:3C:69:AA:02:E9', '192.168.1.2', '4:07', '21/03/15', 'Interdit'), (21L, '', '2C:E4:12:54:7D:4F', '192.168.1.1', '14:07', '21/03/15', 'Interdit'))

并在终端中选择 * 给我:

mysql> select * from ALL_DEVICES;

| Id | HOST_NAME | ADDRESS_MAC | ADDRESS_IP | TIME_SCAN | DATE | ETAT |
+----+------------+--------------+-------------+-----------+------+------+
| 2 | LKK | LLKK | KLK | KK | KJH | KLO |
| 4 | LKK | LLKK | KLK | KK | KJH | KLO |
| 5 | host_name1 | address_mac1 | address_ip1 | tim | da | etat |
| 9 | host_name1 | address_mac1 | address_ip1 | tim | da | etat |
+----+------------+--------------+-------------+-----------+------+------+
4 rows in set (0,00 sec)

最佳答案

您必须使用.commit()

conn = connect("database")
cur = conn.cursor()
cur.execute("INSERT into ...") #insert query

conn.commit() #you missed it

cur.execute("SELECT ...") #select query

关于python - 如何使用python插入mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29183207/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com