python3.7输出语句_python3.4用循环往mysql5.7中写数据并输出的实现方法
如下所示:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = "blzhu"
"""
python study
Date:2017
"""
import pymysql
# import MySQLdb #python2中的产物
try:
# 获取一个数据库连接,注意如果是UTF-8类型的,需要制定数据库
conn = pymysql.connect(host='localhost', user='root', passwd='root', db='zbltest1', port=3306, charset='utf8')
cur = conn.cursor() # 获取一个游标
for i in range(1, 10):
zbl_id = str(i)
zbl_name = 'zbl'+str(i)
zbl_gender = 'man'
# print("%s,%s,%s" % (zbl_id,zbl_name,zbl_gender))
# sql = "insert student VALUES (id='%s',name='%s',gender='%s')" % (zbl_id,zbl_name,zbl_gender)
sql = "insert student VALUES ('%s','%s','%s')" % (zbl_id, zbl_name, zbl_gender)
# print(sql)
cur.execute(sql)
conn.commit()# 将数据写入数据库
# try:
# cur.execute(sql)
# cur.commit()
# except:
# cur.rollback()
#cur.execute("""INSERT INTO 'student' ('id','name','gender') VALUES (%s,%s,%s ,(zbl_id,zbl_name,zbl_gender,))""")
#cur.execute("""INSERT INTO 'student' ('id','name','gender') VALUES (zbl_id,zbl_name,zbl_gender)""")
# cur.execute("INSERT student VALUES (zbl_id,zbl_name,zbl_gender)")
# cur.execute("INSERT student VALUES ('4', 'zbl4', 'man')")# 正确
#cur.execute("INSERT INTO 'student' ('id','name','gender') VALUES ('4', 'zbl4', 'man')")#错误
#cur.execute("INSERT student ('id','name','gender') VALUES ('4', 'zbl4', 'man')")
cur.execute('select * from student')
# data=cur.fetchall()
for d in cur:
# 注意int类型需要使用str函数转义
print("ID: " + str(d[0]) + ' 名字: ' + d[1] + " 性别: " + d[2])
print("row_number:", (cur.rownumber))
# print('hello')
cur.close() # 关闭游标
conn.close() # 释放数据库资源
except Exception:
print("发生异常")
上面代码是对的,但是是曲折的。
下面整理一下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = "blzhu"
"""
python study
Date:2017
"""
import pymysql
try:
# 获取一个数据库连接,注意如果是UTF-8类型的,需要制定数据库
conn = pymysql.connect(host='localhost', user='root', passwd='root', db='zbltest1', port=3306, charset='utf8')
cur = conn.cursor() # 获取一个游标
for i in range(1, 10):
zbl_id = str(i)
zbl_name = 'zbl'+str(i)
zbl_gender = 'man'
# print("%s,%s,%s" % (zbl_id,zbl_name,zbl_gender))
# sql = "insert student VALUES (id='%s',name='%s',gender='%s')" % (zbl_id,zbl_name,zbl_gender)
sql = "insert student VALUES ('%s','%s','%s')" % (zbl_id, zbl_name, zbl_gender)
# print(sql)
cur.execute(sql)
conn.commit()# 将数据写入数据库
cur.execute('select * from student')
# data=cur.fetchall()
for d in cur:
# 注意int类型需要使用str函数转义
print("ID: " + str(d[0]) + ' 名字: ' + d[1] + " 性别: " + d[2])
print("row_number:", (cur.rownumber))
# print('hello')
cur.close() # 关闭游标
conn.close() # 释放数据库资源
except Exception:
print("发生异常")
#!/usr/bin/python3
import pymysql
import types
db=pymysql.connect("localhost","root","123456","python");
cursor=db.cursor()
#创建user表
cursor.execute("drop table if exists user")
sql="""CREATE TABLE IF NOT EXISTS `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`age` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=0"""
cursor.execute(sql)
#user插入数据
sql="""INSERT INTO `user` (`name`, `age`) VALUES
('test1', 1),
('test2', 2),
('test3', 3),
('test4', 4),
('test5', 5),
('test6', 6);"""
try:
# 执行sql语句
cursor.execute(sql)
# 提交到数据库执行
db.commit()
except:
# 如果发生错误则回滚
db.rollback()
#更新
id=1
sql="update user set age=100 where id='%s'" % (id)
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
#删除
id=2
sql="delete from user where id='%s'" % (id)
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
#查询
cursor.execute("select * from user")
results=cursor.fetchall()
for row in results:
name=row[0]
age=row[1]
#print(type(row[1])) #打印变量类型
print ("name=%s,age=%s" % \
(age, name))
以上这篇python3.4用循环往mysql5.7中写数据并输出的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。
本文标题: python3.4用循环往mysql5.7中写数据并输出的实现方法
本文地址: http://www.cppcns.com/shujuku/mysql/194065.html
python3.7输出语句_python3.4用循环往mysql5.7中写数据并输出的实现方法相关推荐
- mysql5.7循环,python3.4用循环往mysql5.7中写数据并输出的实现方法
python3.4用循环往mysql5.7中写数据并输出的实现方法 来源:中文源码网 浏览: 次 日期:2018年9月2日 python3.4用循环往mysql5.7中写数据并输出的实现方 ...
- python3.4用循环往mysql5.7中写数据并输出
#!/usr/bin/env python # -*- coding:utf-8 -*- # __author__ = "blzhu" """ pyt ...
- 在python中数据的输出用哪个函数名_在Python中,数据的输出用哪个函数名
在Python中,数据的输出用哪个函数名 工业机器人按照坐标形式可以分为().A:圆柱坐标机器人B:球坐标机器人C:直角坐标机器人D:关节坐标机器人 生物膜上脂质主要是磷脂.A:对B:错 再结晶退火主 ...
- python3.7输出语句_python3测试工具开发快速入门教程7输入和输出
python有多种输出方式:屏幕打印数据,或者写入文件. 格式化输出 我们有两种大相径庭地输出值方法:表达式语句* 和 print() 函数(第三种方法使用文件对象的write()方法,标准文件输出可 ...
- mysql循环查询一个表中的数据并进行修改_JavaScript学习笔记(二十四)-- MYSQL基础操作...
MYSQL mysql 是一个数据库的名字 和 php 合作的比较好的数据库 之前我们说过一个问题,前端向后端索要数据,后端就是去数据库中查询数据,返回给前端 接下来就聊聊使用 php 操作数据库 M ...
- python3.4使用教程_python3.4使用循环的实例详解
#!/usr/bin/env python# -*- coding:utf-8 -*-# __author__ = "blzhu""""python ...
- 输出26个英文字母c语言,菜鸟求助,写一个随机输出26个英文字母的程序
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 #include #include // 用srand.rand函数了 #include // 用time函数了 #define LEN 32 // 产生 ...
- Python 随机提取Excel中部分数据并输出为新表格
将60000行数据的excel提取其中10%作为模型测试集,并将提取剩下的数据输出为另一个文件 import openpyxl import random from openpyxl import l ...
- R从文件中读取数据,输出文件
看了几天的书,终于到这一步了,说实话,用R来做统计,很少有人手动的去输入那些数字,肯定是从别的地方导入的,我们用来处理就可以了,所以到这里才算是真正的入门,前面都是做基础的练手. 我学习R从<R ...
最新文章
- GOF23设计模式(创建型模式)建造者模式
- Redis做消息队列,香吗?
- 用js实现放大镜的效果
- 宜信开源|详解PaaS平台LAIN的功能和架构
- C语言中变量的链接属性
- Hibernate(三) - hibernate 表操作-多对多配置
- 如何在电脑上截屏_如何在电脑上截图?教你6种常用的截图方法,总有你不知道的...
- 整数数组按绝对值排序
- 台式计算机总是重启,台式电脑经常自动重启怎么修复
- MATLAB simulink 2FSK调制与解调实验,附仿真文件(西电B测)
- querySelector()与querySelectorAll()
- 大气压力换算公式_大气压怎么算
- pytest+seleniumUI自动化框架设计
- 陶哲轩实分析 3.2 节 习题试解
- 用计算机弹出晴空,宠物小精灵之晴空之翼
- TEASLA M40安装问题记录
- 室内定位技术及机场方案建议
- 2022年终总结——工作第五年
- SQL的左连接 ,右连接,内连接和全外连接的4者区别
- 格式工厂3.0.1转PSP2000视频的设置
热门文章
- php将汉字转换为gb2312编码,php实现utf-8和GB2312编码相互转换
- npm vue安装教程
- WPF中如何重新定义Main函数
- [Ubuntu+opencv]Ubuntu18.04安装opencv3.4.3
- oracle:10g下载地址(转载)
- windows下升级nodenpm
- JAVA-5NIO之Selector
- 封装类(Merry May Day to all you who are burried in work ~)---2017-05-01
- eclipse自动补全设置
- Linux 下 Shell 命令的分类及用法