python中mysql基本操作
来源:互联网 发布:淘宝led散热片 编辑:程序博客网 时间:2024/06/05 05:13
#! /usr/bin/env python# -*- coding:utf8 -*-import MySQLdbimport sysreload(sys)sys.setdefaultencoding('utf8')# 打开数据库连接mysqlConn = MySQLdb.connect(host="127.0.0.1",port=3306,user="admin",passwd="123456",db="school",charset="utf8")def mysqlRead(): # 获取操作游标 cursor = mysqlConn.cursor() # sql语句 sql="select * from school.stu" # 执行sql cursor.execute(sql) # # 接收返回的一行数据 # row = cursor.fetchone() # print row # 接收返回的未读取所有行 rs = cursor.fetchall() for row in rs: print row cursor.close()def mysqlInsert(): # 获取操作游标 cursor = mysqlConn.cursor() # sql语句 sql="insert into school.stu(no,name) VALUES ('602','apple2')" try: # 执行sql cursor.execute(sql) # 提交到数据库 mysqlConn.commit() # 操作影响的行数 rowcount = cursor.rowcount print "imfact count:%s" % rowcount except: # 出现异常则回退 mysqlConn.rollback() cursor.close()def main(): mysqlRead() # mysqlInsert() # 关闭连接 mysqlConn.close()if __name__ == "__main__": main()
阅读全文
0 0
- python中mysql基本操作
- python mysql基本操作
- Python mysql 基本操作
- python ---mysql基本操作
- python---mysql基本操作3
- ubunta 中mysql基本操作
- 【python学习.Mysql的基本操作使用】
- python操作mysql数据库基本类封装
- python mysql数据库连接及基本操作
- 数据库之python+MySQL基本操作
- Python 中操作 MySQL 步骤
- Python中矩阵库Numpy基本操作
- Python中字符串、列表的基本操作
- python中excel基本操作实例
- Python中函数定义及基本操作
- python中列表的基本操作
- python 中string的基本操作
- Python中常用的其它基本操作
- python之Flask(一)
- 【LeetCode】572. Subtree of Another Tree
- HDU
- 【腾讯笔试】满二叉排序树中查找三个节点的最小子树的根节点
- linux命令-sed命令使用(1)
- python中mysql基本操作
- MongoDB C++开发环境搭建
- 分布式消息系统:Kafka
- 使用Maven运行Java main的3种方式
- 数据控制、定义、操作
- leetcode : subtree of another tree
- 问题寻找之表格内标签定位行数
- Safari浏览器无法播放视频资源(Java后台SSM架构)的解决方法
- Apache ZooKeeper简介