Python MySQL database access

Source: Internet
Author: User
Tags python mysql

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

Related Article

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.