Python, Pydev connection to MySQL database under "Python" Windows platform

Source: Internet
Author: User
Tags mysql in

MySQL database is cross-platform, not that Python must be connected to MongoDB.

Python's connection to the MySQL database is simple.

First of all, you need to configure the Python development environment, see "Python" version of the Python development environment configuration, Helloworld,python Chinese issues, input and output, conditions, loops, arrays, classes (click Open link), With the MySQL development environment, see "MySQL" MySQL installation, deployment and graphics (click the Open link).

After that, open the Python's official website (click the Open link), for example, directly download a button to install the package Mysql-python-1.2.5.win32-py2.7.exe, be careful not to shake the point to the side of the MD5


Why not download the manual install package zip below? Mainly in my Windows7 x64 system, if you do not have the Visual C + + 9.0 operating environment, that is, the VS2008 platform. Even if your computer has VS2010, download the Vcforpython27.msi on Microsoft's official website, this manual installation package does not know why still cannot install. So don't toss it, just install it with one click.


Then, open the console, enter Python, and then enter:

Import MySQLdb

Note the case, if the same as no error, prove that Python has the ability to operate the MySQL database.


However, you still need to configure the Pydev environment in eclipse, or you will continue to error when programming. Specific as follows:

1. Open Eclipse's Pydev interface and open the Properties Configuration window as shown by the Windows->preferences on the toolbar

Pydev->python interpreters opens as an interface, add the MYSQLDB package just installed.

The exact location is in the lib\site-packages of your Python installation directory, which is ready to store some packages for Python import.


2, after also add this command in the command line, switch the forced Builtins tag Library, new A MYSQLDB command, click OK, save exit, finished. It is possible to open Python's operation on MySQL database in future development.


For example, is a usertable table in the test database of MySQL in which the user is named PC Password admin:


The following python to complete the list of additions and deletions to the operation, the only noteworthy is that in connection with the database, the host must be written 127.0.0.1,python is not recognized localhost.

#-*-coding:utf-8-*-import mysqldbconn=mysqldb.connect (host= "127.0.0.1", user= "PC", passwd= "admin", db= "test", port= 3306); #数据库连接cur =conn.cursor (); #初始化游标result =cur.fetchmany (Cur.execute ("SELECT * from usertable"); # Query the Usertable table in the test database for R in result: #打印查询结果 the    print r;print cur.execute ("SELECT * from usertable"); Find the number of query results Cur.execute ("INSERT into usertable (Username,password) VALUES (%s,%s)", ["SADASD", "sssss"]), #插入数据cur. Execute ("Delete from usertable where username=%s", ["SADASD"]); #删除数据conn. commit (); #提交上面的sql语句到数据库执行cur. Close (); Destroy cursor Conn.close (); #关闭数据库连接
Run the results as shown, where l for this data is a long, first the entire Usertable table query out, then check usertable how many items, and then delete the inserted statement.


In the query statement, the result of a two-dimensional array results, using the foreach traversal each of which is a one-dimensional array r, can be traversed as necessary.

If you do not use the Cur.fetchmany method to format the Cur.execute ("SELECT * from usertable") This statement, directly is the number of current query results.

For insert into and delete from statements that do not have a return value, the value is replaced with%s, followed by an array of the same length, replacing the values. If the value is a number,%d is used. This avoids string connection and injection problems.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Python, Pydev connection to MySQL database under "Python" Windows platform

Related Article

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.