Windows installation mysqldb, python operation MySQL database additions and deletions

Source: Internet
Author: User
Tags sqlite database

The premise here is that the MySQL database is already installed on Windows, and the configuration is complete, and the table can be operated normally. On this basis just install Mysql-python-1.2.4b4.win32-py2.7.exe on OK, only 1M more. This is a bit like the jar package in JDBC.

Download Link: http://sourceforge.net/projects/mysql-python/,

Baidu Cloud Disk: Http://pan.baidu.com/s/1dDgnfpR Password: 7BNA

Then import MYSQLDB will be able to use, the following gives the test code:

 #coding =utf-8#python operation MySQL Database test code import time, MySQLdb, Sysprint "HelloWorld" #连接conn =mysqldb.connect (host= " localhost ", user=" root ", passwd=" Yanzi ", db=" MyDB ", charset=" UTF8 ") cursor = Conn.cursor () print" Connection succeeded "# #增 # sql =" Insert Into UserInfo (username, pswd) values (%s,%s) "# param = (" haha "," ha11 ") # n = cursor.execute (sql, param) # print n# conn.com  MIT () # #更新 # sql = "Update userinfo set pswd =%s where username =%s" # param = ("999999999999", "Zhang San") # Cursor.execute (SQL, param) #删sql = "Delete from UserInfo where username =%s" param = ("Zhang San") n = cursor.execute (sql, param) print nconn.commit () # Check sql = "SELECT * from userinfo" n = cursor.execute (sql) for rows in Cursor.fetchall (): For cols in Rows:print Co Ls,print "" #关闭指针对象和连接cursor. Close () conn.close ()  

The overall operation is very similar to JDBC, and it is OK to pass in a SQL and params. In the operation of the introduction of the concept of the cursor, in the SQLite database is also the cursor responsible for the operation, all a meaning. Note that Conn.commit () must be executed after deletion and increment, otherwise the operation is not valid. But in JDBC there is no such thing. Remember to release the cursor and conn when you close the database.

MySQLdb online documentation RELATED LINKS 1 Links 2 Links 3

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.