Python operation MySQL (add and revise)

Source: Internet
Author: User

#!/usr/bin/env python#coding:utf-8import mysqldbtry:         #连接mysql的方法: Connect (' IP ', ' user ', ' password ', ' dbname ')          #conn = MySQLdb.connect (host= ' localhost ', user= ' root ', passwd= ' 123456 ', db= ' test ')     conn = MySQLdb.connect (' 127.0.0.1 ', ' root ', ' 123456 ',charset =  ' gb2312 ')     conn.select_ db (' Python ')     cur=conn.cursor ()     sql1 =  ' drop  Database python '   #删除数据库      sql2 =  ' create database if  not exists python '   #若不存在, create database     sql3 =  ' create  Database python '     sql4 =  ' Create table module (m_id int  not null,m_name varchar (+), m_size int) ' #创建表     sql5 =  ' Create table if not&nBsp;exists demo (D_id int not null,d_name varchar), M_size int default  0) '     values=[]    for i in range (1):          values.append ((i, ' MySQL ', i+1)       sql6 =  ' Insert into module values (%s,%s,%s) '      #cur. Executemany ( sql6,values)     #批量插入     values = [1, ' MySQLdb ',5]     sql6 =  "insert into module values ('%d ', '%s ', '%d ')"% (2, ' MySQLdb ', 7)  # Insert      #sql6  =  insert into module (m_id,m_name,m_size)  values ('%d ' , '%s ', '%d ') "% (2, ' MySQLdb ', 7)      #sql6  = " Insert into module (m_id,m_ name,m_size)  values ('%d ', '%s ', '%d ') "% (Values[0],values[1],values[2])     sql7 =   "update module Set m_name= ' MySql '  where m_id=0 and m_size=0 '   #修改     sql8  =  "delete from module where m_id=1 and m_size=0"   #删除      sql9 =  "Select * from module where m_id=1"      cur.execute (SQL9)     count = cur.execute (sql9)    #查询结果数量      print u ' query result number: ', Count    result = cur.fetchone ()       print u ' Single query Result: ', Result    result = cur.fetchmany (2 )     print u ' Multiple query results: ', Result    result = cur.fetchall ()     print u ' All different query results: ',result    for data in  Result:        print data    conn.commit ()     cur.Close ()     conn.close () except mysqldb.error,e:    print  " mysql error %d: %s " %  (E.args[0], e.args[1])


Python operation MySQL (add and revise)

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.