mysql匯入資料丟失 資料庫插入頻繁導致資料丟失

2021-10-22 07:08:04 字數 1800 閱讀 1589

插入語句有兩條,迴圈插入這兩條

只是簡單寫了下插入語句,沒有捕捉到異常

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了一下,沒少。

怎麼解決這個問題?

oracle資料庫匯入資料丟失問題

1 原因 檢視匯入過程中的log,發現是由於字符集不同導致資料長度太長導致資料丟失 本地是utf 8型別,伺服器上是zhs16gbk 2 處理方法,修改字符集和伺服器一致,然後重新匯入資料 進入dos視窗 輸入 sqlplus as sysdba 若此時資料庫伺服器已啟動,則先執行shutdown ...

mysql資料庫匯出匯入 mysql資料庫匯入匯出

匯出 mysqldump u 使用者名稱 p 資料庫名 匯出的檔名 匯出整個資料庫 mysqldump u dbuser p dbname dbname.sql all databases 備份伺服器上所有資料庫 mysqldump u dbuser p all databases allbacku...

Mysql匯入資料庫

網上查的都是用命令列,匯入資料的,自己發現用mysql的workbench匯入非常簡單 1.在server administration中選取manage import export 2.選擇data import restore,options選取import from self containe...