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

MySQLdb is not filling rows in DB with data, but feildes are auto-incremented

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

#!/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()

检查可能作为非数字传递的 last_record_id 的有效性,mysql 将拒绝,与 element_time 相同。

我对 python 中的 DB 没有经验,但是否有必要 运行 db.commit() 在你 运行 cursor.execute() 函数。