"Database" Added Million Test Data Part Two-Programming Language + Import Function

Still in the same scenario, let's talk about the second method of adding millions of data.

Scenes

The App of Lemon Class is online, and now it needs the information of one million trainees for the stress test, so where does the data come from? Ask the developers for help directly? May be despised! Go to insert one by one? Well, it will take about 20 years after all!

Don't be afraid, Teacher Happy will teach you 100 ways to create 1 million data efficiently!

Below is a student transcript in the Lemon Class APP,

"Database" Added Million Test Data Part Two-Programming Language + Import Function

The table structure is very simple, id is the primary key, self-growth uniquely identifies a student, sName is the nickname of each student in the lemon class, phone is the mobile phone number, and there is a score field that we love and hate. Attached table SQL:

drop table if exists tb_lemon_student_score;create table tb_lemon_student_score(
id int primary key auto_increment,
sName varchar(50),
phone varchar(11),
score tinyint(1));

The second method: program language to generate data, use tools to import

Our idea is to generate millions of data with agreed rules through the programming language, save them in a text file, and then use the powerful import function of Navicat For MySQL to import millions of data into the database.

1: JAVA programming: write an application that generates data

Open Eclipse and write the following code:

import java.util.Random;public class BatchInsertTest { 
public static void main(String[] args) { 
Random random = new Random(); 
for (int i = 1; i <= 1000000; i++) { 
// student id 
int id = i; 
// the name of the student 
String sName = "s_" + i; 
// mobile phone number 
long mobile = 13000000000L + i; 
// score 
int score = random.nextInt(100); 
System.out.println(id + "|" + sName + "|" + mobile + "|" + score); 
} 
}}
Program description: The program uses a for loop to output 1 million pieces of student information to the console. Each line represents one student information. The rule is: id from 1 to 100w, sName is s_ spliced ​​with id value, mobile phone number is 13000000000+id value , A random integer from 0 to 100, each field is separated by a separator "|" (this separator is useful when importing data to the database).

Run the JAVA application, as shown in the figure:

"Database" Added Million Test Data Part Two-Programming Language + Import Function

The program can normally output the data we require to the console, but the number of lines is limited. The default Eclipse console shows the number of lines is limited. This number of lines can be adjusted, but the copy of millions of lines of text data is very expensive Resources, so introduce another more efficient method, you can directly save all the output of our console to a specified file.

2: Save the program output to the specified file

1) Right-click the JAVA class -> select Run As -> select Run Configurations

"Database" Added Million Test Data Part Two-Programming Language + Import Function

2) Select the Common item on the Run Configurations interface -> check Output File -> select File System to set the location to save the file, here is set to the desktop data.txt file: C:\Users\tommy\Desktop\data.txt

"Database" Added Million Test Data Part Two-Programming Language + Import Function

3) Run the program again, and you can see that a data.txt file is generated on the desktop. The content of the opened file is as follows, a total of 1 million lines:

"Database" Added Million Test Data Part Two-Programming Language + Import Function

3: Navicat For MySQL import text data

Right-click the data table, select Import Wizard, select the import type as txt, and after a series of import settings, start importing:

"Database" Added Million Test Data Part Two-Programming Language + Import Function

Note: Pay special attention to setting the separator in the middle to the vertical bar "|". After about one minute, the import ends.

Finally, select spot check, the data has met our requirements

"Database" Added Million Test Data Part Two-Programming Language + Import Function

"Database" Added Million Test Data Part Two-Programming Language + Import Function

Above are some videos and interview questions I collected.

For software testing friends, it should be the most comprehensive and complete interview preparation warehouse. In order to better organize each module, I also refer to many high-quality blog posts and projects on the Internet, and strive not to miss every knowledge point. Friends relied on these contents to review, and got offers from big factories such as BATJ. This warehouse has also helped many software test learners, and I hope it can also help you.

Follow my WeChat public account [Programmer Erhei] Get it for free
 

Guess you like

Origin blog.csdn.net/weixin_54696666/article/details/115016471