gpt4 book ai didi

python - MySQLdb 没有用数据填充数据库中的行,但是 feildes 是自动递增的

转载 作者:行者123 更新时间:2023-11-30 22:16:52 24 4
gpt4 key购买 nike

我正在使用 MySQlDb 连接并使用 Python 脚本填充我的数据库。来自 BGP 流(转储)的数据正在进入数据库。但是当我尝试在代码底部的第 65 行使用 SQL 执行(插入数据),数据库不受影响,除了该行在其中一个字段上自动递增。这是编码问题吗?我在 python 和 utf-8 中使用 utf-8,MySQL 中的 utf8_swedish_ci。我使用的代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from _pybgpstream import BGPStream, BGPRecord, BGPElem
from collections import defaultdict
import time
import datetime
import os
import MySQLdb

db = MySQLdb.connect(user="bgpstream", host="localhost", passwd="Bgpstream9", db="bgpstream_copy")
db_cursor = db.cursor()

# Create a new bgpstream instance and a reusable bgprecord instance
stream = BGPStream()
rec = BGPRecord()

# Consider Route Views origon only
collector_name = 'rrc11'
stream.add_filter('collector',collector_name) #maybe we want route-views4?

t_end = int(time.time()) #current time now
t_start = t_end-3600 #the time interval (duration) we are getting from collecor, e.i 60*60 = 3600s = 1 hour
stream.add_interval_filter(t_start,t_end)
print "Total duration " + str(t_end-t_start) + " sec"

# Start the stream
stream.start()

### Insert loop ###
# This loop insert new records and tries not to over count the records
# Get next record:
while(stream.get_next_record(rec)):
# Print the record information only if it is not a valid record
if rec.status != "valid":
print rec.project, rec.collector, rec.type, rec.time, rec.status
else:

# Skip if rib
if rec.type == "rib":
continue

#get affected rows from insert
affected_rows = db.affected_rows()

# Skip if dulpicate record
if affected_rows <= 0:
continue

# Extract insert id of last inserted bgp record
last_record_id = db.insert_id()

print last_record_id

# Traverse elements
elem = rec.get_next_elem()
while(elem):

print last_record_id

## Dette bør kaste en exeption
if elem == None:
continue

# Insert element
db_cursor.execute(
"""INSERT INTO bgp_elements
(record_id_owner, element_time, peer_address, peer_asn)
VALUES
(
'"""+str(last_record_id)+"""',
'"""+str(elem.time)+"""',
'"""+str(elem.peer_address)+"""',
'"""+str(elem.peer_asn)+"""'
)
""")
elem = rec.get_next_elem()

最佳答案

我没有使用 python 中的数据库的经验,但是否有必要在运行 cursor.execute() 函数后运行 db.commit() 。

关于python - MySQLdb 没有用数据填充数据库中的行,但是 feildes 是自动递增的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37964915/

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