Python operation MySQL database (to be perfected)

Source: Internet
Author: User

One

Simple to delete and change the search

# coding=utf-8 "Created on May 12, 2015 @author:administrator" import mysqldbtry: #创建MySQL数据库连接, create cursor via connection conn = MYSQ Ldb.connect (host= ' 10.20.102.186 ', user= ' T8 ', passwd= ' 0889781fd27ff33096ed9123b3b4e5d6 ', port=3306, db= ' CustomerService ') cur = conn.cursor () #定义字符串变量 and assign them Adduserinfosql = "INSERT into user_info values (sy Sdate (), null,null,%s,1,1,123456,18600000001,1,1,null) "Deluserinfosql =" Delete from User_info where id=%s "SelUserI Nfosql = "SELECT * from User_info where id=%s" addcount = 0 Delcount = 0 selcount = 0 # loop adds 1000 data and determines: If the ID is not        A new data is added to the data in the database, and if the ID has data in the database then the next ID is judged for I in range (1, 1001): Selcount = cur.execute (seluserinfosql% i) if (Selcount!=1): Addcount = cur.execute (adduserinfosql% i) addcount +=i # Loop Delete 1000 Data "    ' For I in range (1, 1001): Cur.execute (deluserinfosql% i) ' #提交事务, close cursor, close connection conn.commit () Cur.close () Conn.close () #打印出增删改查受影响的行数 print "ExecuteAdd statement after%s row data affected "% addcount print" After executing DELETE statement%s row data affected "% delcountexcept Mysqldb.error, e:print" Mysql Error%d:%s "% ( E.args[0], e.args[1])

  

  

Ready to be perfected

Python operation MySQL database (to be perfected)

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.