python mysqldb insert_python2.7 MySQLdb insert

该博客介绍了如何使用Python通过pymysql模块连接MySQL数据库,展示了如何创建表并使用多行插入语句处理来自文件的IP应用数据。重点在于`cat1.txt`和`cat2.txt`文件的数据导入操作。
摘要由CSDN通过智能技术生成

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.txttomcat  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.133python 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.txt192.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)

# ####

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值