Tag:mysql python practice
# -*-coding:utf-8-*-__author__ = ' Administrator ' Import mysqldbimport pprintimport sysreload (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 ') # the database connection object help information (s) # selected database 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_versionprint ' port: ', s.portprint ' open?: ', s.openprint ' client_flag: ', s.client_flagprint ' converter: ', s.converterprint ' server_capabilities: ', s.server_capabilitiesprint ' ===================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 () # The ======================== method 1 sets the SQL statement =======================================s.query (' select * from scanner_vulnerability ') # gets the SQL execution result, which is obtained using Store_result () or Use_result (), gets, and then uses the two functions again, Cannot successfully get Result = s.store_result () print ' affect rows: ', s.affected_rows () # Help for SQL execution results print ' ============= result data get =================== ' Tup = result.fetch_row (1) print ' Get first line: ', tupprint ' field flags: ', result.field_ Flags () print ' number of rows: ', result.num_rows () print ' Number of columns: ', result.num_fields () # == ================= Method Two : cursor Access =======================cur = s.cursor () # help (cur) cur.execute (' select * from scanner_vulnerability ') # select Fortunately, if an update is inserted, only the above execute is the local set, and a commit is required to actually insert the data. # commit () Submit # rollback () rollback s.commit () Ree = cur.fetchall () # help (ree) print ' Remove 2nd data 2nd field: ', ree[1][1]cur.close () S.close ()
This article is from the "Magicpwn" blog, make sure to keep this source http://magicpwn.blog.51cto.com/10497784/1686366
Python MySQL database access