一、背景

有4張表,每張表要插入多條測試數(shù)據(jù)。如若還有同種需求,于是寫了一個腳本,來添加數(shù)據(jù)。
二、代碼
#--coding:utf8--
import pymysql
class InsertTestData(object):
STUDENT_FILE = 'Student.txt'
COURSE_FILE = 'Course.txt'
TEACHER_FILE = 'Teacher.txt'
SCORE_FILE = 'Score.txt'
def __init__(self):
self.connect = pymysql.Connect(
host = 'localhost',
port = 3306,
user = 'root',
# passwd = ' ',
charset = 'utf8'
)
self.database = 'execersise_test'
def read_lines(self, filename):
dict = {}
file_name = filename.split('.')[0]
dict['file_name'] = file_name
with open(filename) as f:
lines = f.readlines()
dict['file_content'] = lines
return dict
def connect_mysql(self):
cursor = self.connect.cursor()
return cursor
def close_mysql(self):
self.connect.close()
def close_curser(self):
self.connect_mysql().close()
def add_test_datas(self, file_obj):
file = file_obj
file_name = file['file_name']
title = file['file_content'][0].strip()
datas = file['file_content'][1:]
content_len = len(datas)
data = ''
counter = 1
for tmpdata in datas:
if counter == content_len:
data += '(' + tmpdata.strip() + ');'
else:
data += '(' + tmpdata.strip() + '),'
counter += 1
sql = 'insert into ' + self.database + '.' + file_name + ' (' + title + ') values '+ data
try:
# self.connect_mysql().executemany(sql)
self.connect_mysql().execute(sql)
except Exception as e:
print('add_' + file_name + ' error: ', e)
self.connect.commit()
self.close_curser()
if __name__ == '__main__':
testdata = InsertTestData()
testdata.add_test_datas(testdata.read_lines(InsertTestData.STUDENT_FILE))
testdata.add_test_datas(testdata.read_lines(InsertTestData.TEACHER_FILE))
testdata.add_test_datas(testdata.read_lines(InsertTestData.COURSE_FILE))
testdata.add_test_datas(testdata.read_lines(InsertTestData.SCORE_FILE))在main函數(shù)中,只需要指定要讀取的文件,即可快速插入測試數(shù)據(jù)。
為了便于構(gòu)造sql語句,定義的數(shù)據(jù)文件格式如下:
`SID`,`CID`,`Degree` '103' , '3-245' , '86' '105' , '3-245' , '75' '109' , '3-245' , '68' '103' , '3-105' , '92' '105' , '3-105' , '88' '109' , '3-105' , '76' '101' , '3-105' , '64' '107' , '3-105' , '91' '108' , '3-105' , '78' '101' , '6-166' , '85' '107' , '6-166' , '79' '108' , '6-166' , '81'
三、遇到的問題
UnicodeDecodeError: 'gbk' codec can't decode byte 0xbf in position 2: illegal multibyte sequence
原因:文件編碼不是ANSI;
解決方法:將文件用notepad打開,另存,編碼設(shè)置為ANSI格式。
入庫的中文信息亂碼,顯示為'?'
原因:在創(chuàng)建數(shù)據(jù)庫時,未將charset設(shè)置為utf8;
解決方法:重建數(shù)據(jù)庫,重建表,數(shù)據(jù)庫和表的charset都設(shè)置為utf8
如何將sql的values數(shù)據(jù)構(gòu)造成‘(),(),();‘的格式
解決方法:增加計數(shù)器,當(dāng)讀取到最后一行時,以‘;’結(jié)尾
如何通過讀取一個文件,同時獲取要操作的表名稱、列名、values
解決方法:
數(shù)據(jù)文件的文件名稱,定義成表名;
數(shù)據(jù)文件的titile,即第一行內(nèi)容,定義成表的列;
數(shù)據(jù)文件的內(nèi)容,即第一行之后的內(nèi)容,定義成表的數(shù)據(jù)。
網(wǎng)站標(biāo)題:使用python+txt構(gòu)建測試數(shù)據(jù)-創(chuàng)新互聯(lián)
文章出自:http://chinadenli.net/article10/ceeido.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、定制開發(fā)、網(wǎng)站改版、外貿(mào)建站、網(wǎng)站內(nèi)鏈、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容