Python MySQL進(jìn)行數(shù)據(jù)庫表變更和查詢
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)行封裝。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- 使用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ù)庫常用命令 簡單超級實(shí)用版
MySQL 數(shù)據(jù)庫常用命令,都是一些比較基礎(chǔ)的東西,更多的命令可以查看相關(guān)文章里面的文字。2010-07-07
CentOS7環(huán)境下安裝MySQL5.5數(shù)據(jù)庫
大家好,本篇文章主要講的是CentOS7環(huán)境下安裝MySQL5.5數(shù)據(jù)庫,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12
解決mysql ERROR 1045 (28000)-- Access denied for user問題
這篇文章主要介紹了mysql ERROR 1045 (28000)-- Access denied for user解決方法,需要的朋友可以參考下2018-03-03
mysql 批處理文件出錯(cuò)后繼續(xù)執(zhí)行的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猰ysql 批處理文件出錯(cuò)后繼續(xù)執(zhí)行的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10
MySQL報(bào)錯(cuò):The?server?quit?without?updating?PID?file的解決思路
最近在學(xué)習(xí)mysql二進(jìn)制的時(shí)候遇到了個(gè)報(bào)錯(cuò),解決分享給大家,這篇文章主要給大家介紹了關(guān)于MySQL報(bào)錯(cuò):The?server?quit?without?updating?PID?file的解決思路與方法,需要的朋友可以參考下2023-02-02
mysql數(shù)據(jù)庫詳解(基于ubuntu 14.0.4 LTS 64位)
這篇文章主要介紹了mysql數(shù)據(jù)庫詳解(基于ubuntu 14.0.4 LTS 64位),具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12
Mysql 取字段值逗號第一個(gè)數(shù)據(jù)的查詢語句
Mysql 取字段值逗號第一個(gè)數(shù)據(jù),在某些情況下還是比較實(shí)用的,主要使用到了left函數(shù)及其他等等,感興趣的朋友可以參考下2013-10-10

