myaql機能パッケージコードおよびパッケージ後のテスト

13515 ワード

          ,          ,         ,              ,    !
mysql Python
1
#-*- coding: utf-8 -*- 2 ''' 3 4 : mysql 5 :1.0 6 :2019-10-01 7 ''' 8 9 # 10 import pymysql 11 12 # MySQL 13 class MySQL(): 14 15 ''' ''' 16 def __init__(self, host, user, passwd, db_name): 17 self.host = host 18 self.user = user 19 self.passwd = passwd 20 self.db_name = db_name 21 22 ''' ''' 23 def connect(self): 24 # host,user,passwords database_name 25 self.db = pymysql.connect(self.host, self.user, self.passwd, self.db_name) 26 # 27 self.cursor = self.db.cursor() 28 29 ''' ''' 30 def close(self): 31 self.cursor.close() 32 self.db.close() 33 34 ''' ''' 35 def get_one(self, sql): 36 result = None 37 try: 38 self.connect() 39 self.cursor.execute(sql) 40 result = self.cursor.fetchone() 41 self.close() 42 except: 43 print(' ') 44 45 return result 46 47 ''' ''' 48 def get_all(self, sql): 49 result = () 50 try: 51 self.connect() 52 self.cursor.execute(sql) 53 result = self.cursor.fetchall() 54 self.close() 55 except: 56 print(' ') 57 58 return result 59 60 ''' ''' 61 def insert(self, sql): 62 63 return self.__edit(sql) 64 65 ''' ''' 66 def update(self, sql): 67 68 return self.__edit(sql) 69 70 ''' ''' 71 def delete(self,sql): 72 73 return self.__edit(sql) 74 75 def __edit(self, sql): 76 count = 0 77 try: 78 self.connect() 79 count = self.cursor.execute(sql) 80 self.db.commit() 81 self.close() 82 except: 83 print(' ') 84 self.db.rollback()

#-*- coding: utf-8 -*-
'''
  :   
  :     MySQL     
  :
  :2019-10-01
'''

#    
from MySQL import MySQL

'''     '''
s = MySQL('localhost', 'root', '123456', 'test')

'''    '''
# sql = 'select * from bankcard where money > 400'

'''    '''
sql_insert = 'insert into bankcard values (0,100),(0, 2000),(0, 1500)'
s.insert(sql_insert)

'''    '''
sql_check = 'select * from bankcard'
res = s.get_all(sql_check)
for row in res:
    print('%d -- %d' %(row[0], row[1]))