By the Python Excel spreadsheet information into the database

Foreword

    The company uses the original Excel spreadsheet recorded the information server assets, with the increase in business, the corresponding hardware assets also increased, while the physical machine virtual multiple virtual machines, there is a messy table management, change asset information can not be timely synchronized with each other, in order to keeping pace with the times, the boss staged a Django-based asset management platform framework for recording asset information;

    And I am responsible for the server information and sort out their information entered into the asset management platform, based on the collated Excel, individual preliminary understanding at Django, that page manually entered by operating information will be stored in MySQL database specified specified table in order to efficiently input information, I will use Python to import asset information in the Excel spreadsheet to the database!

surroundings

1) determining the structure of the database to be imported into the correspondence table, as follows:

 

2) The table structure corresponding to the collated Excel spreadsheet, as follows:

script

# cat insert.py

 1 # -*- coding: utf-8 -*-
 2 # Author: kazihuo
 3 
 4 import pymysql
 5 import xlrd
 6 
 7 # 连接数据库
 8 try:
 9     db = pymysql.connect(host="10.2.5.200", user="root",
10                          passwd="123456",
11                          db="OPSINFO",
12                          charset=' UTF8 ' )
 13 is  the except :
 14      Print ( " Could Not MySQL Connect to Server " )
 15  
16  
. 17  DEF open_excel ():
 18 is      the try :
 . 19          Book = xlrd.open_workbook ( " test.xlsx " )
 20 is          # test.xlsx is a table file, when it is not with this script in the same directory, you need to write the absolute path 
21      the except :
 22          Print ( " ! Open Excel file failed the " )
 23      the try :
 24-         sheet = book.sheet_by_name("ops-info")
25         # ops-info是表sheet名称,不理解的可看上图;
26         return sheet
27     except:
28         print("locate worksheet in excel failed!")
29 
30 
31 def insert_deta():
32     sheet = open_excel()
33     cursor = db.cursor()
34     row_num = sheet.nrows
35     for i in range(1, row_num):
36     #The first line is the title name, the field names corresponding to the table so that the second line should start from the computer to start the count 0, the value is. 1 
37 [          ROW_DATA = sheet.row_values (I)
 38 is          value = (ROW_DATA [0], ROW_DATA [. 1], ROW_DATA [2], ROW_DATA [. 3], ROW_DATA [. 4], ROW_DATA [. 5 ])
 39          # value representative of the data in Excel table each row 
40          Print (I)
 41 is          SQL = ' the INSERT the INTO CN2Info_machins () the VALUES (% S,% S,% S,% S,% S,% S) ' 
42 is          # SQL =' the INSERT the INTO CN2Info_machins (the Host, the IP, IdracIP, the Position, Rack, ST) the VALUES (% S,% S,% S,% S,% S,% S) ' 
43 is          the cursor.execute (sql, value)   # execute sql statement 
44 is          the db.commit ()
 45     cursor.close ()   # close the connection 
46 is  
47  
48  open_excel ()
 49 insert_deta ()

 

### after running the script, you can see the corresponding information in the database, as follows:

 

### pages also appeared correspondence information, as follows:

 

### script reference links https://www.cnblogs.com/longbigbeard/p/9309180.html

 

### Note

    Because the table id is auto-increment field, so the author in a Python script when inserting data, just write field corresponding to the information to be inserted!

An error

I have some trouble during the operation, given as follows:

pymysql.err.InternalError: (1136, "Column count doesn't match value count at row 1")

 

# Solution

    When an error because the data is inserted, the insertion of the information fields and database fields are inconsistent, the cause may be the "Excel spreadsheet and database table structure does not correspond to" or "the presence of flaws in the script insert statements", after carefully check the field information again operation, can be solved!

Guess you like

Origin www.cnblogs.com/kazihuo/p/11128248.html