Python操作MySQL簡單實(shí)現(xiàn)方法
更新時(shí)間:2015年01月26日 11:58:58 投稿:shichen2014
這篇文章主要介紹了Python操作MySQL簡單實(shí)現(xiàn)方法,通過一個(gè)簡單的實(shí)例講述了Python針對(duì)mysql數(shù)據(jù)庫的增刪改查技巧,需要的朋友可以參考下
本文實(shí)例講述了Python操作MySQL簡單實(shí)現(xiàn)方法。分享給大家供大家參考。具體分析如下:
一、安裝:
安裝MySQL
安裝MySQL不用多說了,下載下來安裝就是,沒有特別需要注意的地方。
一個(gè)下載地址:點(diǎn)擊打開鏈接
二、示例:
復(fù)制代碼 代碼如下:
# coding=utf-8
import MySQLdb
#查詢數(shù)量
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()
#數(shù)量
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])
import MySQLdb
#查詢數(shù)量
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()
#數(shù)量
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])
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- 詳解使用pymysql在python中對(duì)mysql的增刪改查操作(綜合)
- python使用pymysql實(shí)現(xiàn)操作mysql
- Python中操作mysql的pymysql模塊詳解
- Python的SQLalchemy模塊連接與操作MySQL的基礎(chǔ)示例
- Python2.7簡單連接與操作MySQL的方法
- Python操作MySQL數(shù)據(jù)庫9個(gè)實(shí)用實(shí)例
- 使用Python操作MySQL的一些基本方法
- 在Python程序中操作MySQL的基本方法
- Python讀取ini文件、操作mysql、發(fā)送郵件實(shí)例
- Python操作MySQL數(shù)據(jù)庫的方法
相關(guān)文章
python去除字符strip方法的實(shí)現(xiàn)
Python中strip()方法用于去除字符串首尾的空白字符,包括空格、制表符和換行符,可以確保字符串沒有多余的空白字符,感興趣的可以了解一下2024-11-11
Python利用zhconv模塊進(jìn)行簡繁體字轉(zhuǎn)換的案例演示
zhconv是一個(gè)Python庫,提供了簡體字和繁體字之間的轉(zhuǎn)換功能,本教程將向你展示如何使用zhconv模塊來實(shí)現(xiàn)簡繁體字的互轉(zhuǎn),并附帶一個(gè)案例演示,感興趣的朋友可以參考一下2024-05-05
分享7個(gè) Python 實(shí)戰(zhàn)項(xiàng)目練習(xí)
這篇文章主要介紹了分享7個(gè) Python 實(shí)戰(zhàn)項(xiàng)目代碼,經(jīng)過Python3.6.4調(diào)試通過的代碼,就具一點(diǎn)的參考價(jià)值,需要的小伙伴可以參考一下2022-03-03
python中對(duì)開區(qū)間和閉區(qū)間的理解
這篇文章主要介紹了python中對(duì)開區(qū)間和閉區(qū)間的理解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07

