python連接mysql數(shù)據(jù)庫示例(做增刪改操作)
更新時間:2013年12月31日 14:34:01 作者:
python連接mysql數(shù)據(jù)庫示例,提供創(chuàng)建表,刪除表,數(shù)據(jù)增、刪、改,批量插入操作,大家參考使用吧
一、相關(guān)代碼
數(shù)據(jù)庫配置類 MysqlDBConn.py
復(fù)制代碼 代碼如下:
#encoding=utf-8
'''
Created on 2012-11-12
Mysql Conn連接類
'''
import MySQLdb
class DBConn:
conn = None
#建立和數(shù)據(jù)庫系統(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類
復(fù)制代碼 代碼如下:
#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', '測試1',26), ('steven2', '測試2',25))
executemany(sql, tmp)
def updateData():
sql = "update lifeba_users set realname = '%s' where name ='steven1'"%("測試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","測試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è)置日志級別、如何記錄異常信息等。通過本文的指南,讀者可以快速學(xué)會如何使用logging模塊進(jìn)行調(diào)試,并保留有用的日志信息,便于后續(xù)排查問題和優(yōu)化代碼2023-04-04python GUI庫圖形界面開發(fā)之PyQt5單行文本框控件QLineEdit詳細(xì)使用方法與實例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5單行文本框控件QLineEdit詳細(xì)使用方法與實例,需要的朋友可以參考下2020-02-02使用Python代碼實現(xiàn)Linux中的ls遍歷目錄命令的實例代碼
這次我就要試著用 Python 來實現(xiàn)一下 Linux 中的 ls 命令, 小小地證明下 Python 的不簡單,需要的朋友可以參考下2019-09-09Python?reversed函數(shù)用法小結(jié)
reversed函數(shù)是Python中的內(nèi)置函數(shù)之一,是對給定的序列返回一個逆序序列的迭代器,需要通過遍歷/list/next()等方法獲取作用后的值,本文給大家介紹Python?reversed函數(shù)及用法,感興趣的朋友一起看看吧2023-10-10