I. BACKGROUND
There are 4 tables to insert multiple test data into each table. If there is the same demand, write a script to add the data.
Second, the Code
#--coding:utf8--import pymysqlclass 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))
In the main function, you can quickly insert test data by specifying only the files you want to read.
To facilitate the construction of SQL statements, the data file format is defined as follows:
' SID ', ' CID ', ' degree ' 103 ', ' 3-245 ', ' 86 ' 105 ', ' 3-245 ', ' 75 ' 109 ', ' 3-245 ', ' 68 ' 103 ', ' 3-105 ', ' 92 ' 105 ', ' 3-10 5 ', ' 88 ' 109 ', ' 3-105 ', ' 76 ' 101 ', ' 3-105 ', ' 64 ' 107 ', ' 3-105 ', ' 91 ' ' 108 ', ' 3-105 ', ' 78 ' 101 ', ' 6-166 ', ' 85 ' 10 7 ', ' 6-166 ', ' 79 ' 108 ', ' 6-166 ', ' 81 '
Third, the problems encountered
Unicodedecodeerror: ' GBK ' codec can ' t decode byte 0xbf in position 2:illegal multibyte sequence
Reason: File encoding is not ANSI;
Workaround: Open the file with Notepad, save it, and set the encoding to ANSI format.
Storage of Chinese information garbled, display as '? '
Cause: The charset was not set to UTF8 when the database was created;
WORKAROUND: Rebuild the database, rebuild the table, and the database and table charset are set to UTF8
How to construct the values data of SQL into a ' (), (), (); ' Format
Workaround: Increase the counter when reading to the last row, ending with '; '
How to read a file and get the table name, column name, values
Workaround:
The file name of the data file, defined as the table name;
The titile of the data file, i.e. the first line, is defined as a table column;
The contents of the data file, that is, the contents after the first line, define the data as tables.
This article is from the "Le Gakuen" blog, please be sure to keep this source http://2338053.blog.51cto.com/2328053/1979165
Building test Data using Python+txt