One script per day-two

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.