python連接mysql數(shù)據(jù)庫(kù)示例(做增刪改操作)
一、相關(guān)代碼
數(shù)據(jù)庫(kù)配置類 MysqlDBConn.py
#encoding=utf-8
'''
Created on 2012-11-12
Mysql Conn連接類
'''
import MySQLdb
class DBConn:
conn = None
#建立和數(shù)據(jù)庫(kù)系統(tǒng)的連接
def connect(self):
self.conn = MySQLdb.connect(host="localhost",port=3306,user="house", passwd="house" ,db="house",charset="utf8")
#獲取操作游標(biāo)
def cursor(self):
try:
return self.conn.cursor()
except (AttributeError, MySQLdb.OperationalError):
self.connect()
return self.conn.cursor()
def commit(self):
return self.conn.commit()
#關(guān)閉連接
def close(self):
return self.conn.close()
MysqlDemo.py類
#encoding=utf-8
'''
Created on 2012-11-12
@author: Steven
Mysql操作Demo
Done:創(chuàng)建表,刪除表,數(shù)據(jù)增、刪、改,批量插入
'''
import MysqlDBConn
dbconn = MysqlDBConn.DBConn()
def process():
#建立連接
dbconn.connect()
#刪除表
dropTable()
#創(chuàng)建表
createTable()
#批量插入數(shù)據(jù)
insertDatas()
#單條插入
insertData()
#更新數(shù)據(jù)
updateData()
#刪除數(shù)據(jù)
deleteData()
#查詢數(shù)據(jù)
queryData()
#釋放連接
dbconn.close()
def insertDatas():
sql = "insert into lifeba_users(name, realname, age) values(%s, %s, %s)"
tmp = (('steven1', '測(cè)試1',26), ('steven2', '測(cè)試2',25))
executemany(sql, tmp)
def updateData():
sql = "update lifeba_users set realname = '%s' where name ='steven1'"%("測(cè)試1修改")
execute(sql)
def deleteData():
sql = "delete from lifeba_users where id=2"
execute(sql)
def queryData():
sql = "select * from lifeba_users"
rows = query(sql)
printResult(rows)
def insertData():
sql = "insert into lifeba_users(name, realname, age) values('%s', '%s', %s)"%("steven3","測(cè)試3","26")
print sql
execute(sql)
def executemany(sql, tmp):
'''插入多條數(shù)據(jù)'''
conn=dbconn.cursor()
conn.executemany(sql, tmp)
def execute(sql):
'''執(zhí)行sql'''
conn=dbconn.cursor()
conn.execute(sql)
def query(sql):
'''查詢sql'''
conn=dbconn.cursor()
conn.execute(sql)
rows = conn.fetchmany(10)
return rows
def createTable():
'''創(chuàng)建表'''
conn=dbconn.cursor()
conn.execute('''
CREATE TABLE `lifeba_users` (
`ID` int(11) NOT NULL auto_increment,
`name` varchar(50) default NULL,
`realName` varchar(50) default NULL,
`age` int(11) default NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
''')
# dbconn.commit()
def dropTable():
'''刪除表'''
conn=dbconn.cursor()
conn.execute('''
DROP TABLE IF EXISTS `lifeba_users`
''')
# dbconn.commit()
def printResult(rows):
for row in rows:
for i in range(0,len(row)):#遍歷數(shù)組
print row[i], #加, 不換行打印
print ''
if __name__ == '__main__':
process()
相關(guān)文章
Python使用日志模塊快速調(diào)試代碼并記錄異常信息
本文詳細(xì)介紹了Python logging日志模塊的使用方法,包括如何在代碼中使用logging記錄調(diào)試信息、如何設(shè)置日志級(jí)別、如何記錄異常信息等。通過(guò)本文的指南,讀者可以快速學(xué)會(huì)如何使用logging模塊進(jìn)行調(diào)試,并保留有用的日志信息,便于后續(xù)排查問(wèn)題和優(yōu)化代碼2023-04-04Python數(shù)據(jù)處理的三個(gè)實(shí)用技巧分享
數(shù)據(jù)處理無(wú)所不在,掌握常用技巧,事半功倍。這篇文章將使用Pandas開(kāi)展數(shù)據(jù)處理分析,總結(jié)其中常用、好用的數(shù)據(jù)分析技巧,感興趣的可以學(xué)習(xí)一下2022-04-04對(duì)dataframe進(jìn)行列相加,行相加的實(shí)例
今天小編就為大家分享一篇對(duì)dataframe進(jìn)行列相加,行相加的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06python GUI庫(kù)圖形界面開(kāi)發(fā)之PyQt5單行文本框控件QLineEdit詳細(xì)使用方法與實(shí)例
這篇文章主要介紹了python GUI庫(kù)圖形界面開(kāi)發(fā)之PyQt5單行文本框控件QLineEdit詳細(xì)使用方法與實(shí)例,需要的朋友可以參考下2020-02-02Python實(shí)現(xiàn)爬取馬云的微博功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)爬取馬云的微博功能,結(jié)合實(shí)例形式較為詳細(xì)的分析了Python模擬ajax請(qǐng)求爬取馬云微博的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-02-02使用Python代碼實(shí)現(xiàn)Linux中的ls遍歷目錄命令的實(shí)例代碼
這次我就要試著用 Python 來(lái)實(shí)現(xiàn)一下 Linux 中的 ls 命令, 小小地證明下 Python 的不簡(jiǎn)單,需要的朋友可以參考下2019-09-09熵值法原理及Python實(shí)現(xiàn)的示例詳解
熵值法也稱熵權(quán)法,是學(xué)術(shù)研究及實(shí)際應(yīng)用中的一種常用且有效的編制指標(biāo)的方法。本文就來(lái)和大家聊聊熵值法原理及Python實(shí)現(xiàn),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-02-02Python?reversed函數(shù)用法小結(jié)
reversed函數(shù)是Python中的內(nèi)置函數(shù)之一,是對(duì)給定的序列返回一個(gè)逆序序列的迭代器,需要通過(guò)遍歷/list/next()等方法獲取作用后的值,本文給大家介紹Python?reversed函數(shù)及用法,感興趣的朋友一起看看吧2023-10-10