自己玩一玩封裝的mysql

來源:互聯網
上載者:User

標籤:dmi   str   append   語句   imp   rgs   .com   date   mysqldb   

#coding:utf-8import MySQLdbimport sysreload(sys)sys.setdefaultencoding(‘utf-8‘)class MysqldbHelper:    #擷取資料庫連接    def getCon(self):        try:            conn=MySQLdb.connect(host=‘localhost‘,                                 user=‘admin‘,                                 passwd=‘admin‘,                                 db=‘test‘,                                 port=3306,                                 charset=‘utf8‘)            return conn        except MySQLdb.Error,e:            print "Mysqldb Error:%s" %e    #查詢方法,使用con.cursor(MySQLdb.cursors.DictCursor),返回結果為字典    def input_select(self,sql):#手動輸入sql語句        try:            con=self.getCon()            cur=con.cursor(MySQLdb.cursors.DictCursor)            cur.execute(sql)            fc=cur.fetchall()            return fc        except MySQLdb.Error,e:            print "Mysqldb Error:%s" %e        finally:            cur.close()    def parameter_select(self,sql,params):        try:            con = self.getCon()            cur = con.cursor(MySQLdb.cursors.DictCursor)            cur.execute(sql,params)            fc = cur.fetchall()            return fc        except MySQLdb.Error,e:            print "Mysql Error %s" %e        finally:            cur.close()    #帶參數的更新方法,eg:sql=‘insert into pythontest values(%s,%s,%s,now()‘,params=(6,‘C#‘,‘good book‘)    def updateByParam(self,sql,params):        ‘‘‘cursor.execute("SELECT * FROM t1 WHERE id = %s", (5,))‘‘‘        try:            con=self.getCon()            cur=con.cursor(MySQLdb.cursors.DictCursor)            count=cur.execute(sql,params)            con.commit()            return count        except MySQLdb.Error,e:            con.rollback()            print "Mysqldb Error:%s" %e        finally:            cur.close()            con.close()    #不帶參數的更新方法    def update(self,sql):        try:            con=self.getCon()            cur=con.cursor()            count=cur.execute(sql)            con.commit()            return count        except MySQLdb.Error,e:            con.rollback()            print "Mysqldb Error:%s" %e        finally:            cur.close()            con.close()#刪除資料    def delete_information(self,sql):        try:            con = self.getCon()            cur = con.cursor()            cur.execute(sql)            cur.commit()        except MySQLdb.Error,e:            cur.rollback()            print "Mysql Error %s" %e        finally:            cur.close()    def delete_infor_param(self,sql,params):        try:            con = self.getCon()            cur = con.cursor()            cur.execute(sql,params)            cur.commit()        except MySQLdb.Error,e:            cur.rollback()            print "Mysql Error %s" %e        finally:            cur.close()
  
def circulation_update(self,sql,params,params1):
‘‘‘多參數sql語句
data = [
(‘Jane‘,‘555-001‘),
(‘Joe‘, ‘555-001‘),
(‘John‘, ‘555-003‘)
]
stmt = "INSERT INTO employees (name, phone) VALUES (‘%s‘,‘%s‘)"
cursor.executemany(stmt, data)
‘‘‘
try:
con = self.getCon()
cur = con.cursor()
values = []
for i in range(1):
values.append(params)
values.append(params1)
cur.executemany(sql,tuple(values))
cur.commit()
except MySQLdb.Error,e:
cur.rollback()
print "Mysql Error %d :%s" %(e.args[0],e.args[1])
if __name__ == "__main__": 
a
= MysqldbHelper() sql="select * from phoneuser WHERE account = %s" # sql="select * from phoneuser WHERE account = ‘[email protected]‘"
par = ‘[email protected]‘
# fd=a.select(sql)
fd = a.updateByParam(sql,par)

print fd

# for row in fd:

# print row #迴圈插入的例子

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

# cur.execute(‘update test set info="I am rollen" where id=3‘)

# conn.commit()

# cur.close()

# conn.close()

有不對的地方,可以指出來;謝啦

自己玩一玩封裝的mysql

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.