Windows Python Connection database (MySQL, MongoDB)

Source: Internet
Author: User

An experimental platform

1 os:win7 64-bit flagship version SP1

2 python:2.7.10 x64

Two connected databases

1 connecting to MySQL Database

(1) Download mysql (5.6.25-winx64)

Recommended download free installation version, installation version of the easy to appear various problems, unpack the package, double-click the bin directory mysqld.exe, start the database service process, to shut down, go directly to the task Manager to kill the process on the line

(2) Download and install Navicat (this is a MySQL visual management tool, not required, but it is easy to use Python to perform database operations, the intuitive view of the database changes)

(3) Installing the 64-bit Python version of the MySQL class library (Mysql-python-1.2.3.win-amd64-py2.7.exe)

(4) The test code is as follows:

Import MySQLdb

con = mysqldb.connect (host= ' 127.0.0.1 ', user= ' root ', passwd= ', db= ' dbname ')

cursor = Con.cursor ()

sql = ' SELECT * FROM tablename limit 2 '

Cursor.execute (SQL)

row = Cursor.fetchall ()

For OO in row:

Print Oo

Cursor.close ()

Con.close ()

2 Connecting to MongoDB

(1) Download MongoDB (mongodb-win32-x86_64-3.0.5)

Still download the free installation version, this startup and MySQL boot some differences, you need to specify the location of database files, Mongod.exe--dbpath D:\data. (You may need to install the KB2731284 patch first if you can't start with this step)

(2) Download install Mongovue (function equivalent to navicat)

(3) Installing the 64-bit Python version of the MongoDB class library (Pymongo-3.0.3.win-amd64-py2.7.exe)

(4) The simple test code is as follows:

Import Pymongo

Con = Pymongo. Mongoclient (host = ' 127.0.0.1 ')

db = Con.dbname

Table = Db.tablename

UU =dict (name = ' user1 ', age =23,sex= ' female ')

Table.insert (UU)

U2 = Table.find_one ({"Name": ' User1 '})

Print U2

Con.close ()

Windows Python Connection database (MySQL, MongoDB)

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.