Python operations on the MySQL database

Source: Internet
Author: User



Python operations on the MySQL database

#!/usr/bin/python#-*- coding: utf-8 -*-import mysqldbclass mysql:     def __init__ (self,sql,host= ' 127.0.0.1 ', username= ' root ', password= ' root ', dbname= ' dbname '):         self.username=username         self.password=password        self.dbname=dbname         self.sql=sql        mysql.db= MySQLdb.connect (self.host,self.username,self.password,self.dbname,charset= "UTF8")      #查询操作     def query (self):        try:             cursor=mysql.db.cursor ()              cursor.execute (Self.sql)              d Ata=cursor.fetchall ()             return  Data            mysql.db.close ()          except Exception as e:             print e     #插入操作     def insert (self):        try:             cursor=mysql.db.cursor ()              cursor.execute (Self.sql)              Mysql.db.commit ()             mysql.db.close () return  ' OK '         except Exception as e:             print e     #删除操作     def  delete (self):        try:             cursor=mysql.db.cursor ()              cursor.execute (Self.sql)              mysql.db.commit ()              Mysql.db.close ()         except Exception as e:             print e     #修改操作     def update (self):        try:             cursor=mysql.db.cursor ()            &nbSp; cursor.execute (Self.sql)              Mysql.db.commit ()             mysql.db.close ()         except Exception as e:             print eif __name__== "__main__":p


This article is from the "Struggle Bar" blog, please be sure to keep this source http://lvnian.blog.51cto.com/7155281/1845184

Python operations on the MySQL database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.