好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

python对mysql数据库操作的实例详解

这篇文章主要介绍了python3操作mysql数据库的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

# -*- coding: utf-8 -*-
__author__ = 'djstava@gmail测试数据'

import logging
import pymysql

class MySQLCommand(object):
  def __init__(self,host,port,user,passwd,db,table):
    self.host = host
    self.port = port
    self.user = user
    self.password = passwd
    self.db = db
    self.table = table

  def connectMysql(self):
    try:
      self.conn = pymysql.connect(host=self.host,port=self.port,user=self.user,passwd=self.password,db=self.db,charset='utf8')
      self.cursor = self.conn.cursor()
    except:
      print('connect mysql error.')

  def queryMysql(self):
    sql = "SELECT * FROM " + self.table

    try:
      self.cursor.execute(sql)
      row = self.cursor.fetchone()
      print(row)

    except:
      print(sql + ' execute failed.')

  def insertMysql(self,id,name,sex):
    sql = "INSERT INTO " + self.table + " VALUES(" + id + "," + "'" + name + "'," + "'" + sex + "')"
    try:
      self.cursor.execute(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测试数据mit()
    except:
      self.conn.rollback()


  def closeMysql(self):
    self.cursor.close()
    self.conn.close() 

以上就是python对mysql数据库操作的实例详解的详细内容,更多请关注Gxl网其它相关文章!

查看更多关于python对mysql数据库操作的实例详解的详细内容...

  阅读:43次