[python]Python操作MySQL

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介:

【安装】

安装MySQL

安装MySQL不用多说了,下载下来安装就是,没有特别需要注意的地方。

一个下载地址:点击打开链接

【样例】

# coding=utf-8
import MySQLdb

#查询数量
def Count(cur):
   count=cur.execute('select * from Student')
   print 'there has %s rows record' % count
   
#插入
def Insert(cur):
   sql = "insert into Student(ID,Name,Age,Sex)values(%s,%s,%s,%s)"
   param = (2,'xiaoming',24,'boy')
   cur.execute(sql,param)

#查询 
def  Select(cur):  
   n = cur.execute("select * from Student")    
   print "------"
   for row in cur.fetchall():    
      for r in row:    
         print r
      print "------"	
#更新
def Update(cur):
   sql = "update Student set Name = %s where ID = 2"   
   param = ("xiaoxue")    
   count = cur.execute(sql,param)

#删除
def Delete(cur):    
   sql = "delete from Student where Name = %s"   
   param =("xiaoxue")    
   n = cur.execute(sql,param)   
 
try:
   conn=MySQLdb.connect(host='localhost',user='root',passwd='123456',db='python',port=3306)
   cur=conn.cursor()
   #数量
   Count(cur)
   #查询
   Select(cur)
   #插入
   Insert(cur)
   print "插入之后"
   #查询
   Select(cur)
   #更新
   Update(cur)
   print "更新之后"
   #查询
   Select(cur)
   #删除
   Delete(cur)
   print "删除之后"
   #查询
   Select(cur)
   
   cur.close()
   conn.close()
   
except MySQLdb.Error,e:
   print "Mysql Error %d: %s" % (e.args[0], e.args[1])








相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
1天前
|
Python
【Python操作基础】——帮助文档
【Python操作基础】——帮助文档
|
1天前
|
Python
【Python操作基础】——包
【Python操作基础】——包
|
1天前
|
Python
【Python操作基础】——函数
【Python操作基础】——函数
|
1天前
|
Python
【Python操作基础】——字典,迭代器和生成器
【Python操作基础】——字典,迭代器和生成器
|
1天前
|
Python
【Python操作基础】——集合
【Python操作基础】——集合
|
1天前
|
索引 Python
【Python操作基础】——序列
【Python操作基础】——序列
|
1天前
|
Python
【Python操作基础】——字符串
【Python操作基础】——字符串
|
1天前
|
Python
【Python操作基础】——元组
【Python操作基础】——元组
|
1天前
|
Python
【Python操作基础】——列表操作
【Python操作基础】——列表操作
|
1天前
|
Python
【Python操作基础】——while语句用法和pass语句
【Python操作基础】——while语句用法和pass语句