[Python] connecting Python and Pydev to Mysql database _ MySQL on Windows

Source: Internet
Author: User
Tags windows 7 x64
Mysql databases are cross-platform, not that Python must be connected to Mongodb. It is very easy to connect Python to the Mysql database. First, you must configure the Python development environment. for details, see [Python] Python development environment configuration for Windows. Helloworld and Pyth Mysql databases are cross-platform, it does not mean that Python must be connected to Mongodb.

It is very easy to connect Python to the Mysql database.

First, you must configure the Python development environment. for details, see [Python] Python development environment configuration for Windows, Helloworld, and Python Chinese, input and output, conditions, loops, arrays, and classes (click to open the link), and use the Mysql development environment, for details, see [Mysql] Mysql installation, deployment, and graphics "(click to open the link ).

Please be careful not to shake your hand to the md5 next to it

Why not download the manual installation package zip below? It is mainly in my Windows 7 x64 system. if you have not installed the Visual C ++ 9.0 runtime environment, that is, the VS2008 platform. Even if your computer has VS2010 and you have downloaded VCForPython27.msi from Microsoft's official website, the manual installation package still cannot be installed. So don't try it out. install it with one click.

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

import MySQLdb

Note the case sensitivity. If no error is reported, it indicates that Python has the ability to operate Mysql databases.

However, you still need to configure the Pydev environment in Eclipse, otherwise errors will be reported during programming. The details are as follows:

1. open the Eclipse Pydev interface and open the properties configuration window for example through Windows-> Preferences on the toolbar.

PyDev-> Python Interpreters opens the interface as shown in, and adds the MySQLdb package that has just been installed.

The specific location is in lib \ site-packages under your Python installation directory, which is prepared to store some installation packages for Python import.

2. then add this command to the command line to switch to the Forced Builtins tag library, New MySQLdb command, and click OK to save and exit. You can enable Python operations on Mysql databases in future development.

For example, it is a usertable in the test database of Mysql with the user name pc password admin:

The following Python completes addition, deletion, modification, and query of this table. The only thing worth noting is that when connecting to the database, the host must be written as 127.0.0.1. Python does not recognize localhost.

#-*-Coding: UTF-8-*-import MySQLdbconn = MySQLdb. connect (host = "127.0.0.1", user = "pc", passwd = "admin", db = "test" using cute ("select * from usertable ")); # query the usertable table for r in result in the test Database: # print the query result print r; print cur.exe cute ("select * from usertable" cannot exceed the query result quantity cur.exe cute ("insert into usertable (username, password) values (% s, % s) ", [" sadasd "," sssss "cannot be inserted into data cur.exe cute (" delete from usertable where username = % s ", ["sadasd"]); # delete data conn. commit (); # submit the preceding SQL statement to the database to execute cur. close (); # destroy the cursor conn. close (); # close the database connection
As shown in the running result, L indicates that the data is long. first, query the entire usertable, then check the number of usertable items, and then delete the inserted statement.

In the query statement, the returned result is a two-dimensional array result. each item in the result is a one-dimensional array r, which can be traversed as needed.

If cur.fetchmany format cur.exe cute ("select * from usertable"), it is the number of the current query results.

For insert into and delete from statements without return values, the values are replaced by % s, followed by an array of the same length to replace these values. If the value is a number, % d is used. This avoids the problem of string connection and injection.

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.