Install MySQLdb in Windows, and add, delete, modify, and query the MySQL database using Python _ MySQL

Source: Internet
Author: User
Install MySQLdb in Windows, and use Python to add, delete, modify, and query the MySQL database.

The premise here is that the MySQL database has been installed on windows, and the configuration is complete, the normal table creation can be performed. On this basis, you only need to install mysql-python-1.2.4b4.win32-py2.7.exe on the OK page, only 1 MB more. This is similar to the jar package in jdbc.

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

Baidu Cloud Disk: http://pan.baidu.com/s/1ddgnfprpassword :7bna

Then import MySQLdb to use it. the test code is given below:

# Coding = UTF-8 # python operation MySQL database test code import time, MySQLdb, sysprint "HelloWorld" # connection conn = MySQLdb. connect (host = "localhost", user = "root", passwd = "yanzi", db = "mydb", charset = "utf8") cursor = conn. cursor () print "connection successful" ## add # SQL = "insert into userinfo (username, pswd) values (% s, % s)" # param = ("haha ", "ha11") # n = cursor.exe cute (SQL, param) # print n # conn. commit () # update # SQL = "update userinfo set pswd = % s where username = % s" # param = ("999999999999", "James ") # cursor.exe cute (SQL, param) # delete SQL = "delete from userinfo where username = % s" param = ("zhang san") n = cursor.exe cute (SQL, param) print nconn. commit () # Query SQL = "select * from userinfo" n = cursor.exe cute (SQL) for rows in cursor. fetchall (): for cols in rows: print cols, print "# Close pointer object and connection cursor. close () conn. close ()

The overall operation is similar to that in Jdbc. it is OK to pass an SQL statement and params. The concept of cursor is introduced in the operation. in the SQlite database, Cursor is also responsible for the operation. Note: After deletion and addition, you must execute conn. commit (). Otherwise, the operation is invalid. But this is not the case in Jdbc. When you close the database, remember to release cursor and conn.

MySQLdb online document link 1 link 2 link 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.