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

