python2.7 MySQLdb insert

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
云数据库 MongoDB,通用型 2核4GB
简介:

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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

1
2
192.1 . 1.121  tomcat
192.1 . 1.122  redis,mongodb,tomcat



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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  in  range ( len ( apply )):
                 applylist  =  [ip,  apply [i]]
                 applylast.append(applylist)
     return  applylast
 
 
sql_lines  =  read_file_2( '2.txt' )
test1 = test(sql_lines)
 
# ####




本文转自 liqius 51CTO博客,原文链接:http://blog.51cto.com/szgb17/1971040,如需转载请自行联系原作者
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
SQL 数据库 Python
Python 数据库Insert语句脚本生成工具(SQL Server)
Python 数据库Insert语句脚本生成工具(SQL Server)
320 0
Python 数据库Insert语句脚本生成工具(SQL Server)
|
SQL 关系型数据库 MySQL
|
1天前
|
数据采集 算法 Python
2024年Python最全python基础入门:高阶函数,小米面试编程题
2024年Python最全python基础入门:高阶函数,小米面试编程题
|
1天前
|
数据采集 人工智能 前端开发
干货满满,转行逆袭,0编程基础学Python拿高薪offer如何做?都在这里!
干货满满,转行逆袭,0编程基础学Python拿高薪offer如何做?都在这里!
|
3天前
|
Python
10个python入门小游戏,零基础打通关,就能掌握编程基础_python编写的入门简单小游戏
10个python入门小游戏,零基础打通关,就能掌握编程基础_python编写的入门简单小游戏
|
5天前
|
网络协议 Unix Python
Python编程-----网络通信
Python编程-----网络通信
9 1
|
6天前
|
JSON 数据格式 开发者
pip和requests在Python编程中各自扮演着不同的角色
【5月更文挑战第9天】`pip`是Python的包管理器,用于安装、升级和管理PyPI上的包;`requests`是一个HTTP库,简化了HTTP通信,支持各种HTTP请求类型及数据交互。两者在Python环境中分别负责包管理和网络请求。
33 5
|
6天前
|
存储 Python 容器
Python高级编程
Python集合包括可变的set和不可变的frozenset,用于存储无序、不重复的哈希元素。创建集合可使用{}或set(),如`my_set = {1, 2, 3, 4, 5}`。通过add()添加元素,remove()或discard()删除元素,如`my_set.remove(3)`。
17 0
|
6天前
|
测试技术 Python
Python模块化方式编程实践
【5月更文挑战第5天】Python模块化编程提升代码质量,包括:定义专注单一任务的模块;使用`import`导入模块;封装函数和类,明确命名便于重用;避免全局变量降低耦合;使用文档字符串增强可读性;为每个模块写单元测试确保正确性;重用模块作为库;定期维护更新以适应Python新版本。遵循这些实践,可提高代码可读性、重用性和可维护性。
44 2