# -*-coding:utf-8-*- __author__ = ‘Administrator‘ import MySQLdb import pprint import sys reload(sys) sys.setdefaultencoding(‘utf8‘) # help(MySQLdb) s = MySQLdb.connect(host=‘192.168.2.19‘, user=‘scafane‘, passwd=‘vesill‘, db=‘scppn‘, port=3306, use_unicode=True, charset=‘utf8‘) # 数据库连接对象的帮助信息 help(s) # 选定数据库 s.select_db(‘scanner‘) print ‘========== A Connect Obj Func ============‘ print ‘Host info: ‘, s.get_host_info() print ‘con obj info: ‘, s.info() print ‘server_info: ‘, s.get_server_info() print ‘============DATA descripters=============‘ print ‘server_version: ‘, s._server_version print ‘PORT: ‘, s.port print ‘open?: ‘, s.open print ‘client_flag: ‘, s.client_flag print ‘converter: ‘, s.converter print ‘server_capabilities: ‘, s.server_capabilities print ‘===================Methods=====================‘ print ‘affected_rows: ‘, s.affected_rows() print ‘use_result: ‘, s.use_result() print ‘thread_id: ‘, s.thread_id() print ‘state: ‘, s.stat() print ‘sql_state: ‘, s.sqlstate() # ========================方法1 设置sql语句======================================= s.query(‘select * from scanner_vulnerability‘) # 获取sql执行结果,用store_result()或use_result()可获取,获取后,再次使用这两个函数,不能再次成功获取 result = s.store_result() print ‘affect rows: ‘, s.affected_rows() # sql执行结果的帮助 help(result) print ‘============= result DATA 获取 ===================‘ tup = result.fetch_row(1) print ‘获取第一行:‘, tup print ‘字段flags: ‘, result.field_flags() print ‘行数: ‘, result.num_rows() print ‘列数: ‘, result.num_fields() # ===================方法二 : cursor 访问======================= cur = s.cursor() # help(cur) cur.execute(‘select * from scanner_vulnerability‘) # select 还好,如果是更新插入,只上面的execute只是操作本地的set,需要commit才能真正插入数据。 # commit() 提交 # rollback()回滚 s.commit() ree = cur.fetchall() # help(ree) print ‘取出第2条数据第2个字段: ‘, ree[1][1] cur.close() s.close()
本文出自 “magicpwn” 博客,请务必保留此出处http://magicpwn.blog.51cto.com/10497784/1686366
原文地址:http://magicpwn.blog.51cto.com/10497784/1686366