Python MySQL進(jìn)行數(shù)據(jù)庫表變更和查詢
更新時(shí)間:2017年05月04日 14:55:54 投稿:lqh
這篇文章主要介紹了Python MySQL進(jìn)行數(shù)據(jù)庫表變更和查詢的相關(guān)資料,需要的朋友可以參考下
Python連接MySQL,進(jìn)行數(shù)據(jù)庫表變更和查詢:
python mysql insert delete query:
#!/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","root","root","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()
之后可以在此基礎(chǔ)上根據(jù)需要進(jìn)行封裝。
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
您可能感興趣的文章:
- 使用Python實(shí)現(xiàn)將多表分批次從數(shù)據(jù)庫導(dǎo)出到Excel
- python如何解析復(fù)雜sql,實(shí)現(xiàn)數(shù)據(jù)庫和表的提取的實(shí)例剖析
- python的mysql數(shù)據(jù)庫建立表與插入數(shù)據(jù)操作示例
- python 獲取sqlite3數(shù)據(jù)庫的表名和表字段名的實(shí)例
- Python獲取數(shù)據(jù)庫數(shù)據(jù)并保存在excel表格中的方法
- Python實(shí)現(xiàn)將MySQL數(shù)據(jù)庫表中的數(shù)據(jù)導(dǎo)出生成csv格式文件的方法
- Python實(shí)現(xiàn)mysql數(shù)據(jù)庫更新表數(shù)據(jù)接口的功能
- Python實(shí)現(xiàn)將sqlite數(shù)據(jù)庫導(dǎo)出轉(zhuǎn)成Excel(xls)表的方法
- Python如何讀取MySQL數(shù)據(jù)庫表數(shù)據(jù)
- python數(shù)據(jù)庫操作常用功能使用詳解(創(chuàng)建表/插入數(shù)據(jù)/獲取數(shù)據(jù))
- Python 如何實(shí)現(xiàn)數(shù)據(jù)庫表結(jié)構(gòu)同步
相關(guān)文章
mysql數(shù)據(jù)庫開發(fā)規(guī)范【推薦】
這篇文章主要介紹了mysql數(shù)據(jù)庫開發(fā)規(guī)范的相關(guān)內(nèi)容,還是十分不錯(cuò)的,這里給大家分享下,需要的朋友可以參考。2017-10-10
Windows10系統(tǒng)下MySQL(8.0.37)安裝與配置教程
相信很多人都遇到過安裝Mysql的時(shí)候出現(xiàn)各種各樣的問題,下面這篇文章主要給大家介紹了關(guān)于Windows10系統(tǒng)下MySQL(8.0.37)安裝與配置的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07
MySQL關(guān)于sql_mode解析與設(shè)置講解
今天小編就為大家分享一篇關(guān)于MySQL關(guān)于sql_mode解析與設(shè)置講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03
Mysql存儲(chǔ)過程如何實(shí)現(xiàn)歷史數(shù)據(jù)遷移
這篇文章主要介紹了Mysql存儲(chǔ)過程如何實(shí)現(xiàn)歷史數(shù)據(jù)遷移,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
MySQL數(shù)據(jù)庫char與varchar的區(qū)別分析及使用建議
本文主要介紹了mysql中VARCHAR與CHAR字符型數(shù)據(jù)的差異以及這兩種字符型數(shù)據(jù)在項(xiàng)目中的使用建議,真心不錯(cuò)。值得一看。小編有種受益匪淺的感覺。2014-09-09

