插入语句有两条,循环插入这两条

只是简单写了下插入语句,没有捕捉到异常

def process_item(self, item, spider):

#print(item)

try:

with self.connection.cursor() as cursor:

#Create a new record

sql1 = "INSERT INTO staff (XNXQ, \

department, \

teacher, \

gender, \

title, \

note1, \

note2) VALUES (%s, %s, %s, %s, %s, %s, %s)"

cursor.execute(sql1, (item['first']['XNXQ'],

item['first']['department'],

item['first']['teacher'],

item['first']['gender'],

item['first']['title'],

item['first']['note1'],

item['first']['note2']))

self.connection.commit()

#Create a new record

cursor.execute("select max(id) from staff")

teacherId = cursor.fetchone()['max(id)']

print('teacherId:' + str(teacherId))

print(item['second'])

sql2 = "INSERT INTO staffCourse (teacherId, \

snum, \

course, \

credit, \

teachWay, \

courseType, \

classNum, \

className, \

stuNum, \

week, \

section, \

location) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"

cursor.execute(sql2, (teacherId,

item['second']['snum'],

item['second']['course'],

item['second']['credit'],

item['second']['teachWay'],

item['second']['courseType'],

item['second']['classNum'],

item['second']['className'],

item['second']['stuNum'],

item['second']['week'],

item['second']['section'],

item['second']['location']))

self.connection.commit()

except Exception as e:

print('------------------------------------------')

print(e)

查看数据库时,发现少了很多,我猜应该是频繁插入导致数据丢失的,因为我在插入数据库之前把数据print了一下,没少。

怎么解决这个问题?

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐