python code performs SQL file (step into)

I. Introduction

Python on how to connect to the database and execute SQL statements, almost all of the Python tutorials will speak, only basic tutorial describes how to execute a single SQL statement, but the actual production process is not just execute a two statements, dozens or even hundreds of Article 100 SQL statement, a .SQL normal SQL file will contain multiple SQL statements, if these SQL statements directly paste into Python code files, and beauty is bound to affect the readability of the code, the code amount of redundant, so Python can not be like the oracle PL / SQL client or navicat as read and execute SQL file would be a very interesting thing.

 

After continuous Baidu and try some elegant Python code was finally knocked out, as a big fan of open source, technology began to share, now Python code to execute SQL file sharing in CSDN, the code example is MySQL database .

Second, the realization of the principle

We know that when the statement is executed mysql, the default is a semicolon. Therefore, as long as the entire sql file is read, a semicolon cut, you can get the sql statement.

sql file, as follows:

USE test;
/*迁移用户信息*/
INSERT INTO `users` (
    `UID`,
    `USERNAME`,
    `PASSWORD`,
    `ROLE`,
    `CREATE_TIME`
) SELECT
    `id`,
    `username`,
    `password`,
    `level`,
    `create_time`
FROM
    tbl_users;

 

Use a semicolon python cut to cut, sql print or many lines. So we need to line breaks, replaced with spaces. Added at the end end with a semicolon

Open with ( 'test.sql', encoding = 'UTF-. 8', MODE = 'R & lt') AS F: 
    # sql read the entire document in order to cut a semicolon. [: - . 1 ] Remove the last element, i.e. the empty string 
    sql_list = reached, f.read (). Split ( ' ; ' ) [: - . 1 ]
     for X in sql_list: 
        # Analyzing contains an empty row 
        IF  ' \ n- '  in X: 
            # 1 replace the empty space acts 
            X = x.replace ( ' \ n- ' , '  ' ) 

        # SQL statement to add a semicolon 
        sql_item = X + ' ; '
        print(sql_item)

 

Perform output:

USE test;
 /*迁移用户信息*/ INSERT INTO `users` (     `UID`,     `USERNAME`,     `PASSWORD`,     `ROLE`,     `CREATE_TIME` ) SELECT     `id`,     `username`,     `password`,     `level`,     `create_time` FROM     tbl_users;

 

Can be found, there will be many blank lines. Analyzing can be coupled with a, replace

Open with ( 'test.sql', encoding = 'UTF-. 8', MODE = 'R & lt') AS F:
     # read the entire file sql, cutting a semicolon. [: -1] Remove the last element, i.e. the empty string 
    sql_list reached, f.read = () Split (. ' ; ' ) [: -. 1 ]
     for X in sql_list:
         # Analyzing contains an empty row 
        IF  ' \ n- '  in X:
             # replace an empty behavior space 
            X = x.replace ( ' \ n- ' , '  ' ) 

        # is determined that a plurality of spaces 
        IF  '     '  in X:
             # replaced with a null 
            x x =.    ' , ' ' ) 

        # SQL statement to add a semicolon 
        sql_item = X + ' ; ' 
        Print (sql_item)

 

Perform output:

USE test;
 /*迁移用户信息*/ INSERT INTO `users` ( `UID`, `USERNAME`, `PASSWORD`, `ROLE`, `CREATE_TIME` ) SELECT `id`, `username`, `password`, `level`, `create_time` FROM tbl_users;

 

Third, the code official

The above steps have been complete sql statement, then you can use pymysql module to perform every word of sql.

 

Environment Description

The original table structure is:

TABLE `tbl_users` the CREATE ( 
  ` id` int ( . 11 ) the AUTO_INCREMENT the NOT NULL, 
  `username` VARCHAR ( 20 is) the NOT NULL the DEFAULT '' the COMMENT ' username ' , 
  ` password` char ( 32) the NOT NULL the DEFAULT '' the COMMENT ' password ' , 
  `level` tinyint ( 10) the NOT NULL the DEFAULT ' 2 ' the COMMENT ' grade ' , 
  ` create_time` the COMMENT datetime the DEFAULT NULL ' created ' ,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT=' User table ' ;

 

Target table structure is:

CREATE TABLE `users` (
  `UID` bigint(20) NOT NULL AUTO_INCREMENT,
  `USERNAME` varchar(20) NOT NULL DEFAULT '' COMMENT '用户名',
  `PASSWORD` char(32) NOT NULL DEFAULT '' COMMENT '密码',
  `ROLE` varchar(20) NOT NULL DEFAULT '' COMMENT '角色',
  `CREATE_TIME` datetime DEFAULT NULL COMMENT '创建时间',
  PRIMARY KEY (`UID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='user table';

 

The original table data needs to be migrated to the target table.

mysql server ip is  192.168.10.104 , user name and password are root , use the test database.

 

The complete code

The complete code is as follows:

# / Usr / bin / the env to python3! 
# Coding: UTF. 8- 
Import pymysql 

the try : 
    DB = pymysql.connect ( " 192.168.10.104 " , " the root " , " the root " , " Test " ) 
    C = db.cursor () 
    Open with ( ' test.sql ' , encoding = ' UTF-. 8 ' , MODE = ' R & lt ' ) AS F:
         # read the entire file sql, cutting a semicolon. [: -1] Removes the last element, which is an empty string
        reached, f.read = sql_list () Split (. ' ; ' ) [: -. 1 ]
         for X in sql_list:
             # Analyzing contains an empty row 
            IF  ' \ n- '  in X:
                 # replace an empty behavior space 
                x = x.replace ( ' \ n- ' , '  ' ) 

            # is determined that a plurality of spaces 
            IF  '     '  in X:
                 # replaced with a null 
                X = x.replace ( '     ' , '' ) 

            # SQL statement to add a semicolon
            = X + sql_item ' ; ' 
            # Print (sql_item) 
            c.execute (sql_item)
             Print ( " successful implementation of SQL:% S " % sql_item)
 the except Exception AS E:
     Print (E)
     Print ( ' failed SQL: S% ' % sql_item)
 the finally :
     # close mysql connection 
    c.close () 
    the db.commit () 
    db.Close ()

 

Perform output:

The successful implementation of sql: USE test; 
executed successfully SQL:   / * migrate user information * / INSERT INTO `users` (` UID`, `USERNAME`,` PASSWORD`, `ROLE`,` CREATE_TIME`) SELECT `id`,` username`, `password`,` level`, `create_time` FROM tbl_users;

 

 

This article reference links:

https://blog.csdn.net/jsyzliuyu/article/details/79632995

 

Guess you like

Origin www.cnblogs.com/xiao987334176/p/11487426.html