发布时间:2019-07-06 10:45:21编辑:auto阅读(1961)
CREATE TABLE `a` (
`id` int(15) NOT NULL AUTO_INCREMENT,
`ip` varchar(20) NOT NULL,
`apply` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
cat 1.txt
tomcat 192.1.1.121 redis 192.1.1.122 mongodb 192.1.1.122 tomcat 192.1.1.122 tomcat 192.1.1.123 redis 192.1.1.124 mongodb 192.1.1.124 tomcat 192.1.1.124 other 192.1.1.125 tomcat 192.1.1.126 fastdfs 192.1.1.127 fastdfs 192.1.1.128 fastdfs 192.1.1.129 other 192.1.1.130 other 192.1.1.131 fastdfs 192.1.1.132 fastdfs 192.1.1.133
python 1.py #!/usr/bin/env python # -*- coding: utf-8 -*- import sys #import pymysql #pymysql.install_as_MySQLdb() import MySQLdb as mdb con = mdb.connect('127.0.0.1', 'root', '123456', 'db03') def test(param): with con: cur = con.cursor() # cur.execute("CREATE TABLE IF NOT EXISTS \ # Writers(Id INT PRIMARY KEY AUTO_INCREMENT, Name VARCHAR(25))";"INSERT INTO Writers(Name) VALUES('Jack London')") # cur.execute("INSERT INTO Writers(Name) VALUES('Jack London'),INSERT INTO Writers(Name) VALUES('Honore de Balzac')") # sql = 'INSERT INTO Writers(Name) VALUES(%s)' # param = ('Jack London', 'Honore de Balzac') # cur.executemany(sql, param) # sql="insert a(ip,yy) values(['tomcat', '192.1.1.121']); insert a(ip,yy) values(['redis', '192.1.1.122']);" # cur.execute(sql) sql = 'INSERT INTO a(apply,ip) VALUES(%s,%s)' #param = [['tomcat', '192.1.1.121'], ['redis', '192.1.1.122'], ['mongodb', '192.1.1.122']] #param = ((username1, salt1, pwd1), (username2, salt2, pwd2), (username3, salt3, pwd3)) cur.executemany(sql, param) ''' sql_lines = [] with open('1.txt', 'r') as file: for lines in file.readlines(): line = lines.strip('\n').split() sql = 'insert a(ip,yy) values({0});'.format(line) sql_lines.append(sql) sql_last = '\r\n'.join(sql_lines) cur.execute(sql_last) # cur.execute("INSERT INTO Writers(Name) VALUES('Honore de Balzac')") # cur.execute("INSERT INTO Writers(Name) VALUES('Lion Feuchtwanger')") # cur.execute("INSERT INTO Writers(Name) VALUES('Emile Zola')") # cur.execute("INSERT INTO Writers(Name) VALUES('Truman Capote')") ''' def db_execute(sql): cursor = con.cursor() cursor.execute(sql) cursor.close() def read_file(file_path): sql_lines = [] with open(file_path, 'r') as file: for lines in file.readlines(): line = lines.strip('\n').split() # sql = 'insert table(field) values({0});'.format(line) sql_lines.append(line) print sql_lines return sql_lines # print '\r\n'.join(str(sql_lines)) python3用的 sql_lines = read_file('1.txt') # db_execute(sql_lines) test1=test(sql_lines) # test1=test()
cat 2.txt
192.1.1.121 tomcat 192.1.1.122 redis,mongodb,tomcat
python 2.py #!/usr/bin/env python # -*- coding: utf-8 -*- import sys #import pymysql #pymysql.install_as_MySQLdb() import MySQLdb as mdb con = mdb.connect('127.0.0.1', 'root', '123456', 'db03') def test(param): with con: cur = con.cursor() sql = 'INSERT INTO a(ip,apply) VALUES(%s,%s)' cur.executemany(sql, param) def db_execute(sql): cursor = con.cursor() cursor.execute(sql) cursor.close() def read_file(file_path): sql_lines = [] with open(file_path, 'r') as file: for lines in file.readlines(): line = lines.strip('\n').split() sql_lines.append(line) print sql_lines return sql_lines def read_file_2(file_path): applylast = [] with open(file_path, 'r') as file: for lines in file.readlines(): line = lines.strip('\n').split() ip = line[0] apply = line[1].split(',') for i in range(len(apply)): applylist = [ip, apply[i]] applylast.append(applylist) return applylast sql_lines = read_file_2('2.txt') test1=test(sql_lines) # ####
上一篇: 关于python的doc string
下一篇: Python脚本sendmail
48564
47536
38340
35574
30033
26742
25733
20646
20375
18777
130°
172°
180°
181°
191°
236°
261°
383°
363°
330°