Python操作MySQL簡(jiǎn)單實(shí)現(xiàn)方法
更新時(shí)間:2015年01月26日 11:58:58 投稿:shichen2014
這篇文章主要介紹了Python操作MySQL簡(jiǎn)單實(shí)現(xiàn)方法,通過一個(gè)簡(jiǎn)單的實(shí)例講述了Python針對(duì)mysql數(shù)據(jù)庫(kù)的增刪改查技巧,需要的朋友可以參考下
本文實(shí)例講述了Python操作MySQL簡(jiǎn)單實(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簡(jiǎn)單連接與操作MySQL的方法
- Python操作MySQL數(shù)據(jù)庫(kù)9個(gè)實(shí)用實(shí)例
- 使用Python操作MySQL的一些基本方法
- 在Python程序中操作MySQL的基本方法
- Python讀取ini文件、操作mysql、發(fā)送郵件實(shí)例
- Python操作MySQL數(shù)據(jù)庫(kù)的方法
相關(guān)文章
Python實(shí)現(xiàn)提取和去除數(shù)據(jù)中包含關(guān)鍵詞的行
這篇文章主要介紹了Python如何提取數(shù)據(jù)中包含關(guān)鍵詞的行已經(jīng)如何去除數(shù)據(jù)中包含關(guān)鍵詞的行,文中的示例代碼講解詳細(xì),需要的可以參考一下2023-08-08Python數(shù)據(jù)可視化:餅狀圖的實(shí)例講解
今天小編就為大家分享一篇Python數(shù)據(jù)可視化:餅狀圖的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12Python日期時(shí)間處理庫(kù)dateutil詳解
dateutil 為 Python 標(biāo)準(zhǔn)庫(kù) datetime 提供了強(qiáng)大的擴(kuò)展,這篇文章主要介紹了Python日期時(shí)間處理庫(kù)dateutil,需要的朋友可以參考下2022-09-09