前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python2.7 MySQLdb i

python2.7 MySQLdb i

作者头像
py3study
发布2020-01-14 16:17:21
5820
发布2020-01-14 16:17:21
举报
文章被收录于专栏:python3python3

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

代码语言:javascript
复制
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
代码语言:javascript
复制
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

代码语言:javascript
复制
192.1.1.121 tomcat
192.1.1.122 redis,mongodb,tomcat
代码语言:javascript
复制
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)

# ####
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-07-06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档