国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開(kāi)通VIP
MySQL與Python交互

關(guān)于MySQL推薦一本書MySQL必知必會(huì)

首先安裝第三方模塊(ubuntu下Python2)

sudo apt-get install python-mysql

假設(shè)有一數(shù)據(jù)庫(kù)test1,里面有一張產(chǎn)品信息表products,向其中插入一條產(chǎn)品信息,程序如下:

# -*- coding: utf-8 -*-import MySQLdbtry:    conn=MySQLdb.connect(host='localhost',port=3306,db='test1',user='root',passwd='mysql',charset='utf8')    cs1=conn.cursor()    count=cs1.execute("insert into products(prod_name) values('iphone')")    print count    conn.commit()    cs1.close()    conn.close()except Exception,e:    print e.message

Connection對(duì)象:用于建立與數(shù)據(jù)庫(kù)的連接
        創(chuàng)建對(duì)象:調(diào)用connect()方法
conn=connect(參數(shù)列表)
    參數(shù)host:連接的mysql主機(jī),如果本機(jī)是'localhost'
    參數(shù)port:連接的mysql主機(jī)的端口,默認(rèn)是3306
    參數(shù)db:數(shù)據(jù)庫(kù)的名稱
    參數(shù)user:連接的用戶名
    參數(shù)password:連接的密碼
    參數(shù)charset:通信采用的編碼方式,默認(rèn)是'gb2312',要求與數(shù)據(jù)庫(kù)創(chuàng)建時(shí)指定的編碼一致,否則中文會(huì)亂碼

對(duì)象的方法
    close()關(guān)閉連接
    commit()事務(wù),所以需要提交才會(huì)生效
    rollback()事務(wù),放棄之前的操作
    cursor()返回Cursor對(duì)象,用于執(zhí)行sql語(yǔ)句并獲得結(jié)果

Cursor對(duì)象:執(zhí)行sql語(yǔ)句
    創(chuàng)建對(duì)象:調(diào)用Connection對(duì)象的cursor()方法
    cursor1=conn.cursor()

對(duì)象的方法
    close()關(guān)閉
    execute(operation [, parameters ])執(zhí)行語(yǔ)句,返回受影響的行數(shù)
    fetchone()執(zhí)行查詢語(yǔ)句時(shí),獲取查詢結(jié)果集的第一個(gè)行數(shù)據(jù),返回一個(gè)元組
    next()執(zhí)行查詢語(yǔ)句時(shí),獲取當(dāng)前行的下一行
    fetchall()執(zhí)行查詢時(shí),獲取結(jié)果集的所有行,一行構(gòu)成一個(gè)元組,再將這些元組裝入一個(gè)元組返回
    scroll(value[,mode])將行指針移動(dòng)到某個(gè)位置
        mode表示移動(dòng)的方式
        mode的默認(rèn)值為relative,表示基于當(dāng)前行移動(dòng)到value,value為正則向下移動(dòng),value為負(fù)則向上移動(dòng)
        mode的值為absolute,表示基于第一條數(shù)據(jù)的位置,第一條數(shù)據(jù)的位置為0

修改/刪除:

# -*- coding: utf-8 -*-import MySQLdbtry:    conn=MySQLdb.connect(host='localhost',port=3306,db='test1',user='root',passwd='mysql',charset='utf8')    cs1=conn.cursor()    # 修改    count=cs1.execute("update products set prod_name='xiaomi' where id=6")   # 刪除  count=cs1.execute("delete from products where id=6")  print count     conn.commit()     cs1.close()     conn.close()except Exception,e:    print e.message

參數(shù)化:插入一條數(shù)據(jù)

# -*- coding: utf-8 -*-import MySQLdbtry:    conn=MySQLdb.connect(host='localhost',port=3306,db='test1',user='root',passwd='mysql',charset='utf8')    cs1=conn.cursor()    prod_name=raw_input("請(qǐng)輸入產(chǎn)品名稱:")    params=[prod_name]    count=cs1.execute('insert into products(sname) values(%s)',params)    print count    conn.commit()    cs1.close()    conn.close()except Exception,e:    print e.message

查詢一條

# -*- coding: utf-8 -*-import MySQLdbtry:    conn=MySQLdb.connect(host='localhost',port=3306,db='test1',user='root',passwd='mysql',charset='utf8')    cs1=conn.cursor()    cur.execute('select * from products where id=2')    result=cur.fetchone()    print result    conn.commit()     cs1.close()     conn.close() except Exception,e:     print e.message

查詢多條

# -*- coding: utf-8 -*-import MySQLdbtry:    conn=MySQLdb.connect(host='localhost',port=3306,db='test1',user='root',passwd='mysql',charset='utf8')    cs1=conn.cursor()    cur.execute('select * from prod_name')    result=cur.fetchall()    print result    conn.commit()     cs1.close()     conn.close() except Exception,e:     print e.message

封裝:觀察前面的程序發(fā)現(xiàn),除了sql語(yǔ)句及參數(shù)不同,其它語(yǔ)句都是一樣的,可以進(jìn)行封裝然后調(diào)用

# -*- coding: utf-8 -*-import MySQLdbclass MysqlHelper():    def __init__(self,host,port,db,user,passwd,charset='utf8'):        self.host=host        self.port=port        self.db=db        self.user=user        self.passwd=passwd        self.charset=charset    def connect(self):        self.conn=MySQLdb.connect(host=self.host,port=self.port,db=self.db,user=self.user,passwd=self.passwd,charset=self.charset)        self.cursor=self.conn.cursor()    def close(self):        self.cursor.close()        self.conn.close()    def get_one(self,sql,params=()):        result=None        try:            self.connect()            self.cursor.execute(sql, params)            result = self.cursor.fetchone()            self.close()        except Exception, e:            print e.message        return result    def get_all(self,sql,params=()):        list=()        try:            self.connect()            self.cursor.execute(sql,params)            list=self.cursor.fetchall()            self.close()        except Exception,e:            print e.message        return list    def insert(self,sql,params=()):        return self.__edit(sql,params)    def update(self, sql, params=()):        return self.__edit(sql, params)    def delete(self, sql, params=()):        return self.__edit(sql, params)    def __edit(self,sql,params):        count=0        try:            self.connect()            count=self.cursor.execute(sql,params)            self.conn.commit()            self.close()        except Exception,e:            print e.message        return count

保存為MysqlHelper.py文件。

調(diào)用類添加

# -*- coding: utf-8 -*-from MysqlHelper import *sql='insert intoproducts(prod_name,price) values(%s,%s)'prod_name=raw_input("請(qǐng)輸入產(chǎn)品名稱:")price=raw_input("請(qǐng)輸入單價(jià):")params=[prod_name,price]mysqlHelper=MysqlHelper('localhost',3306,'test1','root','mysql')count=mysqlHelper.insert(sql,params)if count==1:    print 'ok'else:    print 'error'

調(diào)用類查詢查詢

# -*- coding: utf-8 -*-from MysqlHelper import *sql='select prod_name,price from products order by id 'helper=MysqlHelper('localhost',3306,'test1','root','mysql')one=helper.get_one(sql)print one
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
python實(shí)現(xiàn)的MySQL增刪改查操作實(shí)例小結(jié)
Python數(shù)據(jù)庫(kù)連接池相關(guān)示例詳細(xì)介紹
Python操作Mysql - 課程 - 從此學(xué)習(xí)網(wǎng)
pymysql 庫(kù)的正確打開(kāi)姿勢(shì)
Python-Mysql在測(cè)試過(guò)程中的應(yīng)用
Python接口測(cè)試之對(duì)MySQL的增、刪、改、查操作(五)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服