python使用MySQLdb訪問mysql數(shù)據(jù)庫的方法
本文實(shí)例講述了python使用MySQLdb訪問mysql數(shù)據(jù)庫的方法。分享給大家供大家參考。具體如下:
#!/usr/bin/python
import MySQLdb
def doInsert(cursor,db):
#insert
# Prepare SQL query to INSERT a record into the database.
sql = "UPDATE EMPLOYEE SET AGE = AGE+1 WHERE SEX = '%c'" %('M')
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
def do_query(cursor,db):
sql = "SELECT * FROM EMPLOYEE \
WHERE INCOME > '%d'" % (1000)
try:
# Execute the SQL command
cursor.execute(sql)
# Fetch all the rows in a list of lists.
results = cursor.fetchall()
print 'resuts',cursor.rowcount
for row in results:
fname = row[0]
lname = row[1]
age = row[2]
sex = row[3]
income = row[4]
# Now print fetched result
print "fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \
(fname, lname, age, sex, income )
except:
print "Error: unable to fecth data"
def do_delete(cursor,db):
sql = 'DELETE FROM EMPLOYEE WHERE AGE > {}'.format(20)
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
def do_insert(cursor,db,firstname,lastname,age,sex,income):
sql = "INSERT INTO EMPLOYEE(FIRST_NAME, \
LAST_NAME, AGE, SEX, INCOME) \
VALUES ('%s', '%s', '%d', '%c', '%d' )" % \
(firstname,lastname,age,sex,income)
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
# Open database connection
# change this to your mysql account
#connect(server,username,password,db_name)
db = MySQLdb.connect("localhost","hunter","hunter","pydb" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
do_query(cursor,db)
doInsert(cursor,db)
do_query(cursor,db)
do_delete(cursor,db)
do_query(cursor,db)
do_insert(cursor,db,'hunter','xue',22,'M',2000)
do_insert(cursor,db,'mary','yang',22,'f',5555)
do_insert(cursor,db,'zhang','xue',32,'M',5000)
do_insert(cursor,db,'hunter','xue',22,'M',333)
do_query(cursor,db)
# disconnect from server
db.close()
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
- Python3.7 pyodbc完美配置訪問access數(shù)據(jù)庫
- 詳解js文件通過python訪問數(shù)據(jù)庫方法
- 對(duì)Python通過pypyodbc訪問Access數(shù)據(jù)庫的方法詳解
- Python使用pyodbc訪問數(shù)據(jù)庫操作方法詳解
- Python輕量級(jí)ORM框架Peewee訪問sqlite數(shù)據(jù)庫的方法詳解
- Python的Tornado框架實(shí)現(xiàn)異步非阻塞訪問數(shù)據(jù)庫的示例
- Linux下通過python訪問MySQL、Oracle、SQL Server數(shù)據(jù)庫的方法
- python訪問mysql數(shù)據(jù)庫的實(shí)現(xiàn)方法(2則示例)
- Python訪問純真IP數(shù)據(jù)庫腳本分享
- 在Linux中通過Python腳本訪問mdb數(shù)據(jù)庫的方法
- Shell、Perl、Python、PHP訪問 MySQL 數(shù)據(jù)庫代碼實(shí)例
- python訪問純真IP數(shù)據(jù)庫的代碼
- 使用Python通過oBIX協(xié)議訪問Niagara數(shù)據(jù)的示例
相關(guān)文章
pandas.loc 選取指定列進(jìn)行操作的實(shí)例
今天小編就為大家分享一篇pandas.loc 選取指定列進(jìn)行操作的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05
Python中Matplotlib繪圖保存圖片時(shí)調(diào)節(jié)圖形清晰度或分辨率的方法
有時(shí)我們?cè)谑褂胢atplotlib作圖時(shí),圖片不清晰或者圖片大小不是我們想要的,這篇文章主要給大家介紹了關(guān)于Python中Matplotlib繪圖保存圖片時(shí)調(diào)節(jié)圖形清晰度或分辨率的相關(guān)資料,需要的朋友可以參考下2024-05-05
在Pytorch中使用樣本權(quán)重(sample_weight)的正確方法
今天小編就為大家分享一篇在Pytorch中使用樣本權(quán)重(sample_weight)的正確方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08
Django調(diào)用百度AI接口實(shí)現(xiàn)人臉注冊(cè)登錄代碼實(shí)例
這篇文章主要介紹了Django調(diào)用百度AI接口實(shí)現(xiàn)人臉注冊(cè)登錄,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
python [:3] 實(shí)現(xiàn)提取數(shù)組中的數(shù)
今天小編就為大家分享一篇python [:3] 實(shí)現(xiàn)提取數(shù)組中的數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11
python同時(shí)給兩個(gè)收件人發(fā)送郵件的方法
這篇文章主要介紹了python同時(shí)給兩個(gè)收件人發(fā)送郵件的方法,涉及Python使用smtplib包發(fā)送郵件的相關(guān)技巧,需要的朋友可以參考下2015-04-04
python實(shí)現(xiàn)簡(jiǎn)單的購物程序代碼實(shí)例
這篇文章主要介紹了python實(shí)現(xiàn)簡(jiǎn)單的購物程序代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03

