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:
Try:
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])
Pass
Finally:
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

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.