Script requirements:
The company has a MySQL database, which is downloaded from an FTP server every day *. DAT file, and then use MySQL. before importing the DAT file to the database, you need to determine whether the record exists in the database table. If this record does not exist, the record is imported. If there are already some records, the record is not imported.
Script implementation:
General idea: Use the mysql-e command to query the specific column guild_id (unique value) in the table record, redirect it to the table. In file, and write it to list l through the for loop traversal;
Read CRM. the DAT file also reads the first record in the file to the list record through the for loop, and compares whether the value in record [2] is in list L. If not, redirect L to the file table. will. Use mysql-e to import the table. Will file to MySQL.
Script Name: import. Sh
#! /Usr/bin/ENV Python
Import OS
Command1 = '(dbuser = root; dbpwd = 123456; dbname = test; mysql-U $ dbuser-p $ dbpwd $ dbname-e "select guild_id from troopsranks"> table. in )'
Command2 = "" (dbuser = root; dbpwd = 123456; dbname = test; mysql-U $ dbuser-p $ dbpwd $ dbname-e "load data local infile '/root/table. will 'ignore into Table troopsranks Character Set utf8 (area_id, group_id, guild_id, guild_name, grand_parentid, total_troopsrecord, total_memberlevel );")"""
OS. System (command1)
L = []
Record = []
F1 = open ('table. in ')
Lines = f1.readlines ()
F1.close ()
F2 = open ('crm. dat ')
Records = f2.readlines ()
F2.close ()
For line in lines:
Line = line. Strip ('\ n ')
L. append (STR (line ))
For record in records:
Record = record. Split ()
If record [2] Not in L:
Print record
F = open ('table. will', 'w ')
F. Write (record [0])
F. Write ('\ t ')
F. Write (record [1])
F. Write ('\ t ')
F. Write (record [2])
F. Write ('\ t ')
F. Write (record [3])
F. Write ('\ t ')
F. Write (record [4])
F. Write ('\ t ')
F. Write (record [5])
F. Write ('\ t ')
F. Write (record [6])
F. Close ()
OS. System (command2)
One script per day-two