溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用Python操縱mysql數(shù)據(jù)庫(kù)的具體方法

發(fā)布時(shí)間:2020-05-27 10:25:18 來源:網(wǎng)絡(luò) 閱讀:232 作者:三月 欄目:數(shù)據(jù)庫(kù)

本文主要給大家介紹使用Python操縱mysql數(shù)據(jù)庫(kù)的具體方法,文章內(nèi)容都是筆者用心摘選和編輯的,具有一定的針對(duì)性,對(duì)大家的參考意義還是比較大的,下面跟筆者一起了解下使用Python操縱mysql數(shù)據(jù)庫(kù)的具體方法吧。 

#!/usr/bin/python
# -*- coding: utf-8 -*-
import MySQLdb

class mysql:
    def __init__(self, sql, host='127.0.0.1', username='root', password='root', dbname='dbname'):
        self.username = username
        self.password = password
        self.dbname = dbname
        self.sql = sql
        self.mysqldb = MySQLdb.connect(host, self.username, self.password, self.dbname, charset="utf8")

    # 查詢操作
    def query(self):
        try:
            cursor = self.mysqldb.cursor()
            cursor.execute(self.sql)
            data = cursor.fetchall()
            return data
            
        except Exception as e:
            print e

    # 插入操作
    def insert(self):
        try:
            cursor = self.mysqldb.cursor()
            cursor.execute(self.sql)
            self.mysqldb.commit()
            self.mysqldb.close()
            return 'ok'
        except Exception as e:
            print e

    # 刪除操作
    def delete(self):
        try:
            cursor = self.mysqldb.cursor()
            cursor.execute(self.sql)
            self.mysqldb.commit()
            self.mysqldb.close()
        except Exception as e:
            print e

    # 修改操作
    def update(self):
        try:
            cursor = self.mysqldb.cursor()
            cursor.execute(self.sql)
            self.mysqldb.commit()
            self.mysqldb.close()
        except Exception as e:
            print e

if __name__=="__main__":
	pass
#!/usr/bin/python
# -*- coding: utf-8 -*-

__author__ = 'gaogd'

import  MySQLdb

try:
    conn = MySQLdb.connect(host='localhost', user='root', passwd='root', port=3306)
    cur = conn.cursor()
    cur.execute('create database if not exists python')
    conn.select_db('python')
    cur.execute('create table test(id int,info varchar(20))')

    value = [1, 'hi rollen']
    cur.execute('insert into test values(%s,%s)', value)

    values = []
    for i in range(20):
        values.append((i, 'hi rollen' + str(i)))

    cur.executemany('insert into test values(%s,%s)', values)
    ## 重點(diǎn):這個(gè)  cur.executemany 可以一次性插入多個(gè)值  
    cur.execute('update test set info="I am rollen" where id=3')

    conn.commit()
    cur.close()
    conn.close()

except MySQLdb.Error, e:
    print "Mysql Error %d: %s" % (e.args[0], e.args[1])

常用函數(shù):

然后,這個(gè)連接對(duì)象也提供了對(duì)事務(wù)操作的支持,標(biāo)準(zhǔn)的方法

commit()提交

rollback()回滾

cursor用來執(zhí)行命令的方法:

callproc(self,procname,args):用來執(zhí)行存儲(chǔ)過程,接收的參數(shù)為存儲(chǔ)過程名和參數(shù)列表,返回值為受影響的行數(shù)

execute(self, query, args):執(zhí)行單條sql語句,接收的參數(shù)為sql語句本身和使用的參數(shù)列表,返回值為受影響的行數(shù)

executemany(self, query, args):執(zhí)行單挑sql語句,但是重復(fù)執(zhí)行參數(shù)列表里的參數(shù),返回值為受影響的行數(shù)

nextset(self):移動(dòng)到下一個(gè)結(jié)果集

cursor用來接收返回值的方法:

fetchall(self):接收全部的返回結(jié)果行.

fetchmany(self, size=None):接收size條返回結(jié)果行.如果size的值大于返回的結(jié)果行的數(shù)量,則會(huì)返回cursor.arraysize條數(shù)據(jù).

fetchone(self):返回一條結(jié)果行.

scroll(self, value, mode='relative'):移動(dòng)指針到某一行.如果mode='relative',則表示從當(dāng)前所在行移動(dòng)value條,如果 mode='absolute',則表示從結(jié)果集的第一行移動(dòng)value條.

看完以上關(guān)于使用Python操縱mysql數(shù)據(jù)庫(kù)的具體方法,很多讀者朋友肯定多少有一定的了解,如需獲取更多的行業(yè)知識(shí)信息 ,可以持續(xù)關(guān)注我們的行業(yè)資訊欄目的。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI