Python:MySQL 操纵类
直接看代码吧:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
#!/usr/bin/python # -*- coding: UTF-8 -*- # ######################################## # import # -- os import sys # -- Command Options #import getopt import argparse # -- MySQL import pymysql # ######################################## # variable # -- Meta:关于本脚本的调用与控制 # 该脚本将以什么方式运行 # 对数据的增加,ADD # 对数据的删除,DELETE # 对数据的修改,MODIFY meta_python_options_OpsMode="" # -- MySQL mysql_host="***你不需要知道的主机地址***" mysql_user="***你不需要知道的用户名***" mysql_passwd="***你不需要知道的口令***" mysql_database="***你不需要知道的数据库名***" # ######################################## # function # init def __init__(self): pass # -- Show message out # 直接输出信息 # 目标:统一输出 def show_message(message_string): # do - display print("================") print(message_string) print("") # -- MySQL # =================================== # 数据库:连接阶段 # 数据库:连接方法 # 根据变量区域的变量,返回一个数据库的连接对象 def mysql_return_connect(): # variable db = pymysql.connect(mysql_host,mysql_user,mysql_passwd,mysql_database) # return return db # 根据数据库连接对象,返回一个数据库游标对象 def mysql_return_cursor(db_connect): # variable cursor = db_connect.cursor(pymysql.cursors.DictCursor) # return return cursor # =================================== # 数据库:使用阶段 # --------------- # 查 # 查询方法 # 最简单的查询,并返回MySQL的查询结果 # 1. 执行SQL # 2. 选择结果集的返回模式 # all # one # many,3 - 返回三行 def mysql_do_select(db_connect,sql_command_string,result_set_mode,result_set_lines=0): # variable db_cursor=mysql_return_cursor(db_connect) # 结果集 result_set="" # do try: # 执行SQL的命令语句 db_cursor.execute(sql_command_string) # 获取SQL的执行结果 #result_set=db_cursor.fetchall() if (result_set_mode=="all"): result_set = db_cursor.fetchall() elif (result_set_mode=="one"): result_set = db_cursor.fetchone() elif (result_set_mode=="many"): result_set = db_cursor.fetchmany(result_set_lines) else: show_message("需要指定结果集的模式") except: # variable error_info = sys.exc_info() # display show_message("Error: unable to fetch data from MySQL.") print( error_info[0], ":", error_info[1]) finally: # 清理数据库连接资源 mysql_do_cleanup(db_connect) # display #print(result_set) # return return result_set # 判断SQL的查询是否有结果 def mysql_do_select_isNull(mysql_result_set): # variable result=False # do if mysql_result_set: result=True else: result = False # return return result # --------------- # 删 # --------------- # 改 # --------------- # 增 # 对数据库做增(Insert)删(Delete)改(Update)查操作 # 并返回是否执行成功 def mysql_do_IDU(db_connect,sql_command_string): # variable # -- return result=False # -- MySQL db_cursor=mysql_return_cursor(db_connect) # do try: # DB Execute SQL db_cursor.execute(sql_command_string) # DB Commit db_connect.commit() # change status result=True except: # variable error_info = sys.exc_info() # display show_message("Error: unable to execute SQL") print(error_info[0], ":", error_info[1]) # MySQL - Roll back db_connect.rollback() # change status result = False finally: # 清理数据库连接资源 mysql_do_cleanup(db_connect) # return return result # =================================== # 数据库:关闭阶段 # 数据库:关闭阶段 - 清理方法 # 传入,数据库连接对象 def mysql_do_cleanup(db_connect): # display #show_message("数据库 - 关闭阶段:清理") # do # 关闭数据库连接 db_connect.close() # ######################################## # do # Print Hello world #print("Hello world.") # db - mysql # 查询 #mysql_do_select(mysql_return_cursor(mysql_return_connect()),"select count(*) from person;") #db = mysql_return_connect() #cursor = mysql_return_cursor(db) #print(mysql_do_select(cursor,"select name,email from person","one")) #print(mysql_do_select(mysql_return_cursor(mysql_return_connect()),"select name,email from person","all")) #print(mysql_do_select(mysql_return_cursor(mysql_return_connect()),"select name,email from person","many",2)) #result=mysql_do_select(mysql_return_cursor(mysql_return_connect()),"select name,email from person where name='adamhuan'","many",2) #if result: # print("查得出内容") #else: # print("查不出内容") #result=mysql_do_select(mysql_return_cursor(mysql_return_connect()),"select name,email from person where name='adamhuan'","many",2) #if mysql_do_select_isNull(result): # print("YES") #else: # print("NO") #result=mysql_do_select(mysql_return_connect(),"select name,email from person","many",1) #done=mysql_do_select_isNull(result) #show_message(result) #show_message(done) # 执行SQL #result = mysql_do_IDU(mysql_return_connect(),"drop database abc;") #show_message(result) if __name__ == "__main__": # 命令行参数 parser = argparse.ArgumentParser(description='How to use This Python Script') # -- options parser.add_argument('--OpsMode', type=str, default=None) # -- analyze args = parser.parse_args() # -- store to variable meta_python_options_OpsMode=args.OpsMode # display #print(meta_python_options_OpsMode) # done |
——————————————————————
Done。
1 thought on “Python:MySQL 操纵类”