欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

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

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

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

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

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

sudo apt-get install python-mysql

假設有一數據庫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對象:用于建立與數據庫的連接
        創(chuàng )建對象:調用connect()方法
conn=connect(參數列表)
    參數host:連接的mysql主機,如果本機是'localhost'
    參數port:連接的mysql主機的端口,默認是3306
    參數db:數據庫的名稱(chēng)
    參數user:連接的用戶(hù)名
    參數password:連接的密碼
    參數charset:通信采用的編碼方式,默認是'gb2312',要求與數據庫創(chuàng )建時(shí)指定的編碼一致,否則中文會(huì )亂碼

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

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

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

參數化:插入一條數據

# -*- 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("請輸入產(chǎn)品名稱(chēng):")    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

查詢(xún)一條

# -*- 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

查詢(xún)多條

# -*- 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

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

# -*- 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文件。

調用類(lèi)添加

# -*- coding: utf-8 -*-from MysqlHelper import *sql='insert intoproducts(prod_name,price) values(%s,%s)'prod_name=raw_input("請輸入產(chǎn)品名稱(chēng):")price=raw_input("請輸入單價(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'

調用類(lèi)查詢(xún)查詢(xún)

# -*- 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
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
python實(shí)現的MySQL增刪改查操作實(shí)例小結
Python數據庫連接池相關(guān)示例詳細介紹
Python操作Mysql - 課程 - 從此學(xué)習網(wǎng)
pymysql 庫的正確打開(kāi)姿勢
Python-Mysql在測試過(guò)程中的應用
Python接口測試之對MySQL的增、刪、改、查操作(五)
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久