python3.4用函數(shù)操作mysql5.7數(shù)據(jù)庫
更新時間:2017年06月23日 11:01:10 作者:豬冰龍
這篇文章主要為大家詳細介紹了python3.4用函數(shù)操作mysql5.7數(shù)據(jù)庫,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python3.4函數(shù)操作mysql數(shù)據(jù)庫的具體代碼,供大家參考,具體內(nèi)容如下
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = "blzhu"
"""
python study
Date:2017
"""
# -*- coding: utf-8 -*-
__author__ = 'djstava@gmail.com'
import logging
import pymysql
class MySQLCommand(object):
def __init__(self, host, port, user, passwd, db, table, charset):
self.host = host
self.port = port
self.user = user
self.password = passwd
self.db = db
self.table = table
self.charset = charset
def connectMysql(self):
try:
self.conn = pymysql.connect(host=self.host, port=self.port, user=self.user, passwd=self.password,
db=self.db, charset=self.charset)
self.cursor = self.conn.cursor()
print('connect ' + self.table + ' correctly!')
except:
print('connect mysql error.')
def queryMysql(self):
sql = "SELECT * FROM " + self.table
try:
print("query Mysql:")
self.cursor.execute(sql)
#row = self.cursor.fetchone()
for d in self.cursor:
print(str(d[0]), str(d[1]), str(d[2]))
# print(row)
except:
print(sql + ' execute failed.')
def insertMysql(self, id, name, sex):
sql = "INSERT INTO " + self.table + " VALUES(" + id + "," + "'" + name + "'," + "'" + sex + "')"
try:
print("insert Mysql:")
self.cursor.execute(sql)
print(sql)
except:
print("insert failed.")
def updateMysqlSN(self, name, sex):
sql = "UPDATE " + self.table + " SET sex='" + sex + "'" + " WHERE name='" + name + "'"
print("update sn:" + sql)
try:
self.cursor.execute(sql)
self.conn.commit()
except:
self.conn.rollback()
def deleteMysql(self, id): # 刪除
sql = "DELETE FROM %s WHERE id='%s'" % (self.table,id)
#"delete from student where zid='%s'" % (id)
try:
self.cursor.execute(sql)
print(sql)
self.conn.commit()
print("delete the " + id + "th row successfully!")
except:
print("delete failed!")
self.conn.rollback()
def closeMysql(self):
self.conn.commit() # 不執(zhí)行此句,所作的操作不會寫入到數(shù)據(jù)庫中
self.cursor.close()
self.conn.close()
if __name__ == '__main__':
zblmysql = MySQLCommand(host='localhost', user='root', passwd='root', db='zbltest1', port=3306, table='student2',
charset='utf8')
zblmysql.connectMysql()
zblmysql.queryMysql()
zblmysql.insertMysql('5', 'zbl5', 'man')
zblmysql.queryMysql()
zblmysql.deleteMysql(id=2)
zblmysql.queryMysql()
zblmysql.updateMysqlSN(name='zbl5',sex='woman')
zblmysql.queryMysql()
zblmysql.closeMysql()
參考:python3操作mysql數(shù)據(jù)庫的方法
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Python3實現(xiàn)將本地JSON大數(shù)據(jù)文件寫入MySQL數(shù)據(jù)庫的方法
- Python3實現(xiàn)的爬蟲爬取數(shù)據(jù)并存入mysql數(shù)據(jù)庫操作示例
- Python3實現(xiàn)的Mysql數(shù)據(jù)庫操作封裝類
- python3連接MySQL數(shù)據(jù)庫實例詳解
- Python3.6簡單操作Mysql數(shù)據(jù)庫
- 在python3環(huán)境下的Django中使用MySQL數(shù)據(jù)庫的實例
- python3操作mysql數(shù)據(jù)庫的方法
- python3使用PyMysql連接mysql數(shù)據(jù)庫實例
- linux下python3連接mysql數(shù)據(jù)庫問題
- python3對接mysql數(shù)據(jù)庫實例詳解
相關(guān)文章
Python Pandas實現(xiàn)數(shù)據(jù)分組求平均值并填充nan的示例
今天小編就為大家分享一篇Python Pandas實現(xiàn)數(shù)據(jù)分組求平均值并填充nan的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
python+selenium 簡易地疫情信息自動打卡簽到功能的實現(xiàn)代碼
這篇文章主要介紹了python+selenium 簡易地疫情信息自動打卡簽到功能的實現(xiàn)代碼,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
Tensorflow tensor 數(shù)學運算和邏輯運算方式
這篇文章主要介紹了Tensorflow tensor 數(shù)學運算和邏輯運算方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06

