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)