Python MySQLdb没有用数据填充数据库中的行,但feildes是自动递增的
我正在使用MySQlDb连接数据库,并使用python脚本填充数据库。 来自BGP流(转储)的数据将进入数据库。但是当我试着 使用SQL在代码底部的第65行执行(插入数据),DB不受影响,除了行在它的一个字段上自动递增之外。这是编码问题吗?我在python和utf-8中使用utf-8, MySQL中的utf8_瑞典语_ci。我使用的代码:Python MySQLdb没有用数据填充数据库中的行,但feildes是自动递增的,python,mysql,mysql-python,Python,Mysql,Mysql Python,我正在使用MySQlDb连接数据库,并使用python脚本填充数据库。 来自BGP流(转储)的数据将进入数据库。但是当我试着 使用SQL在代码底部的第65行执行(插入数据),DB不受影响,除了行在它的一个字段上自动递增之外。这是编码问题吗?我在python和utf-8中使用utf-8, MySQL中的utf8_瑞典语_ci。我使用的代码: #!/usr/bin/env python # -*- coding: utf-8 -*- from _pybgpstream import BGPStrea
#!/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()
#/usr/bin/env python
#-*-编码:utf-8-*-
从_pybgpstream导入BGPStream、bgprecard、bgplem
从集合导入defaultdict
导入时间
导入日期时间
导入操作系统
导入MySQLdb
db=MySQLdb.connect(user=“bgpstream”,host=“localhost”,passwd=“Bgpstream9”,db=“bgpstream\u copy”)
db_cursor=db.cursor()
#创建新的bgpstream实例和可重用的bgprecord实例
流=BGPStream()
rec=bgprecrd()
仅考虑路由视图
收集器名称='rrc11'
stream.add_filter('collector',collector_name)#也许我们想要路线视图4?
t_end=int(time.time())#现在是当前时间
t_start=t_end-3600#我们从collecor获得的时间间隔(持续时间),即60*60=3600s=1小时
stream.add\u interval\u过滤器(t\u start,t\u end)
打印“总持续时间”+str(t_结束-t_开始)+“秒”
#开源节流
stream.start()
###插入环###
#此循环将插入新记录,并尝试不过度计数记录
#获取下一条记录:
while(stream.get_next_record(rec)):
#仅当记录信息不是有效记录时才打印该记录信息
如果记录状态!=“有效”:
打印rec.project、rec.collector、rec.type、rec.time、rec.status
其他:
#跳过如果肋骨
如果rec.type==“肋骨”:
持续
#从insert获取受影响的行
受影响的_行=db.受影响的_行()
#如果没有记录,则跳过
如果受影响的\u行检查最后一个\u记录\u id的有效性,该id可能作为mysql将拒绝的非数字传递,相同的sa元素\u时间 检查最后一个\u记录\u id的有效性,该id可能作为mysql将拒绝的非数字传递,相同的sa元素\u时间 我对python中的DBs没有经验,但可能是在运行cursor.execute()函数之后需要运行db.commit() 我对python中的DBs没有经验,但可能是在运行cursor.execute()函数之后需要运行db.commit() 我正在第52行打印最后的记录id。这是我在第52行打印的最后一条记录id的递增数字(从数据库自动递增)。这是我更改为包的递增数字(DB的自动递增);mysql.connector。现在,数据库按其应该的方式填充。但是,在执行该命令之后,有必要对DB对象进行提交。谢谢。我已经换了包裹;mysql.connector。现在,数据库按其应该的方式填充。但是,在执行该命令之后,有必要对DB对象进行提交。谢谢