Python: Database Operations

Source: Internet
Author: User

#! /Usr/bin/Python
#-*-Coding: UTF-8 -*-

    • Add header file

Import mysqldb as MDB
Import sys, time, datetime

Class operatedbtool (object ):

    • Connect to database

@ Staticmethod
Def connecttodb (host_info ):
Con = none
Host_info_list = host_info.split (',')
If Len (host_info_list) = 5:
Con = MDB. Connect (host = host_info_list [0]. Strip (), Port = int (host_info_list [1]. Strip ()),\
User = host_info_list [2]. Strip (),\
Passwd = host_info_list [3]. Strip (), DB = host_info_list [4]. Strip ())
Return con

    • Perform query operations

@ Staticmethod
Def executivedb (con, SQL ):
RT = none
If con! = None:
Cur = con. cursor ()
Cur.exe cute (SQL)
Data = cur. fetchone ()
# Print "data:", Data
Return data
Failed t MDB. error, E:
# Print "MDB error: % d: % s" % (E. ARGs [0], E. ARGs [1])
If con:
Con. Close ()

    • Call

@ Staticmethod
Def checkbuyofferrunning (host_info ):
Con = operatedbtool. connecttodb (host_info)
SQL = 'select * From table_name where e_info )'
Data = operatedbtool. executivedb (con, SQL)

Related Article

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: 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.