Python operation MySQL

Source: Internet
Author: User

Query (two methods):

import mysqldb    conn = mysqldb.connect  (host =  "localhost" , user =  "root", passwd =  "root", db =  "fish")   cursor  = conn.cursor  ()   cursor.execute  ("Select * from polls_poll") Rows = cursor.fetchall ()      #获取所有结果集for  row in rows:              #从结果集中一条一条读出来     print  "% d, %s, %s " %  (row[0], row[1], row[2])   print " number of  rows returned: %d " % cursor.rowcount  cursor.execute  (" select *  from polls_poll ")   while  (True):      row =  Cursor.fetchone ()      #从结果集的开始, get a record,  move a downstream label until the end returns NONE    IF  row == noNe:          break      print   "%d, %s, %s"  %  (row[0], row[1], row[2])        print  "number of rows returned: %d"  % cursor.rowcount   cursor.close  ()   conn.close  ()

Insert:

Cursor.execute ("INSERT into (name, age) VALUES (' Bill ')") #返回受影响的行数, execution error return exception

Update:

Cursor.execute ("UPDATE both SET name = ' Jack ', age = ' WHERE name = ' Bill ') ' #返回受影响的行数, execution error returned exception





Python operation MySQL

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.