MySQLdb模塊如何操作MySQL數(shù)據(jù)庫-創(chuàng)新互聯(lián)

下面一起來了解下MySQLdb模塊如何操作MySQL數(shù)據(jù)庫,相信大家看完肯定會受益匪淺,文字在精不在多,希望MySQLdb模塊如何操作MySQL數(shù)據(jù)庫這篇短內(nèi)容是你想要的。

創(chuàng)新互聯(lián)公司是一家集成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、網(wǎng)站頁面設(shè)計(jì)、網(wǎng)站優(yōu)化SEO優(yōu)化為一體的專業(yè)的建站公司,已為成都等多地近百家企業(yè)提供網(wǎng)站建設(shè)服務(wù)。追求良好的瀏覽體驗(yàn),以探求精品塑造與理念升華,設(shè)計(jì)最適合用戶的網(wǎng)站頁面。 合作只是第一步,服務(wù)才是根本,我們始終堅(jiān)持講誠信,負(fù)責(zé)任的原則,為您進(jìn)行細(xì)心、貼心、認(rèn)真的服務(wù),與眾多客戶在蓬勃發(fā)展的市場環(huán)境中,互促共生。

1. python連接mysql的connector有很多,我們選擇MySQLdb

2. 讓python支持MySQLdb模塊

#pip2.7 install MySQL-python

3. 查看python2.7可使用的模塊是否存在MySQLdb

# ipython WARNING: IPython History requires SQLite, your history will not be saved Python 2.7.11 (default, Mar 10 2016, 09:45:30)  Type "copyright", "credits" or "license" for more information. IPython 4.1.2 -- An enhanced Interactive Python. ?         -> Introduction and overview of IPython's features. %quickref -> Quick reference. help      -> Python's own help system. object?   -> Details about 'object', use 'object??' for extra details. In [1]: help('modules') Please wait a moment while I gather a list of all available modules... MySQLdb             calendar            marshal

4. 簡單封裝的模塊,測試沒問題

script-name:mysql-conn.py

#!/usr/bin/python27 #-*-coding:utf-8-*- #導(dǎo)入MySQL驅(qū)動原生模塊 import MySQLdb   class mysqldb():         #構(gòu)造器設(shè)定初始值 def __init__(self,host='192.168.17.1',port=4306,user='root',passwd='zcy'):   self.host = host self.port = port self.user = user self.passwd = passwd #實(shí)例一開始就具備了連接和游標(biāo)屬性 self.conn1 = MySQLdb.connect(host=self.host,port=self.port,user=self.user,passwd=self.passwd)   self.cur1 = self.conn1.cursor()                    #定義mysql中的Select查詢語句 def find(self,field,db,table):   """ field -> query field db -> query database table -> query table """ self.field = field self.db = db self.table = table self.cur1.execute('select ' + self.field + ' from ' + self.db + '.' + self.table) return self.cur1.fetchall() #建庫 def createdb(self,db):   self.cur1.execute('create database if not exists ' + db)                 #建表 def createtable(self,db,table):    self.conn1.select_db(db) self.cur1.execute( '''     create table ''' + table + ''' ( id int(5) not null primary key auto_increment, name char(10) not null, phone varchar(12) not null, class char(20) );  ''')                                  #插入數(shù)據(jù) def insert(self,db,table,*l):  self.cur1.execute('insert into '+ db + '.' + table +  ' values(null,%s,%s,%s)' , *l) self.conn1.commit()

看完MySQLdb模塊如何操作MySQL數(shù)據(jù)庫這篇文章后,很多讀者朋友肯定會想要了解更多的相關(guān)內(nèi)容,如需獲取更多的行業(yè)信息,可以關(guān)注我們的行業(yè)資訊欄目。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

網(wǎng)站題目:MySQLdb模塊如何操作MySQL數(shù)據(jù)庫-創(chuàng)新互聯(lián)
標(biāo)題URL:http://bm7419.com/article38/dpoesp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、品牌網(wǎng)站制作、App開發(fā)、軟件開發(fā)、自適應(yīng)網(wǎng)站、云服務(wù)器

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)