Python mysqldb basic operation method for MySQL

Source: Internet
Author: User

1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 ImportMySQLdb4 5conn = MySQLdb.connect (host='192.168.1.101', user='Root', passwd='123', db='Host')6cur = conn.cursor (cursorclass=MySQLdb.cursors.DictCursor)7Recout = Cur.execute ('Select Ip,name from host,user where user.name = "Alex" and User.id=host.id')8Nret =Cur.fetchall ()9 Conn.commit ()Ten cur.close () One conn.close () A PrintRecout - PrintNret -  forIinchNret: the     Printi['name'],i['IP'] -  - """ - #修改 + conn = MySQLdb.connect (host= ' 192.168.1.101 ', user= ' root ', passwd= ' 123 ', db= ' host ') - cur = conn.cursor () + recout = Cur.execute (' Update host set id=%s ', (1,)) A Conn.commit () at cur.close () - conn.close () - Print Recout - """ - """ - #删除 in conn = MySQLdb.connect (host= ' 192.168.1.101 ', user= ' root ', passwd= ' 123 ', db= ' host ') - cur = conn.cursor () to recout = cur.execute (' Delete from host ') + Conn.commit () - cur.close () the conn.close () * Print Recout $ """Panax Notoginseng """ - L = [ the (' 192.168.1.107 ', ' 2 '), + (' 192.168.1.108 ', ' 2 '), A (' 192.168.1.109 ', ' 2 '), the (' 192.168.1.177 ', ' 2 '), + ] - #插入多条数据 $ conn = MySQLdb.connect (host= ' 192.168.1.101 ', user= ' root ', passwd= ' 123 ', db= ' host ') $ cur = conn.cursor () - recout = Cur.executemany (' INSERT into host (Ip,id) VALUES (%s,%s) ', L) -  the Conn.commit () - cur.close ()Wuyi conn.close () the Print Recout - """ Wu """ - #插入单条数据 About conn = MySQLdb.connect (host= ' 192.168.1.101 ', user= ' root ', passwd= ' 123 ', db= ' host ') $ cur = conn.cursor () - recout = Cur.execute (' INSERT into host (Ip,id) VALUES (%s,%s) ', (' 192.168.1.1 ', ' 1 ')) -  - Conn.commit () A cur.close () + conn.close () the Print Recout - """

Python mysqldb basic operation method for MySQL

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.