This article describes how to install MySQL-python in Windows and Linux, and the problems encountered during the installation process. It is of great reference value for your reference.
Install MySQL-python in Windows
: The https://pypi.python.org/pypi/MySQL-python/1.2.5 can be installed to the system.
Install MySQL-python in linux to connect to MySQL:
: Https://pypi.python.org/pypi/MySQL-python/
After decompression, go to the directory and execute python setup. py install
Problems frequently encountered during installation:
1. If you cannot find mysql_config, it is generally because mysql uses the one-click installation package of lnmp.
Solution: locate mysql_config and find the location of the mysql_config file. Then, connect ln-s to/usr/bin.
2. If 'x86 _ 64-linux-gnu-gcc 'is missing in Ubuntu, install the python-dev package:
Solution: sudo apt-get install python-dev-y
3. The command 'gcc 'failed with exit status 1 is displayed in CentOS.
Solution: yum install gcc python-devel-y
After installation, go to python and run import MySQLdb to check whether the import is successful.
Supplement:
During my operations on ubuntu, I found that I could not connect to the database. ss-lnt found that mysql only listened to port 3306 on the loopback address and needed to be modified.
Modify mysql of Ubuntu and change its listening port 127.0.0.1: 3306 to allow external connections:
Edit/etc/mysql/my. cnf (you may need to configure parameters in other files in this directory. Check carefully)
Modify bind-address = 0.0.0.0 to allow access from any IP address.
Then run/etc/init. d/mysql restart to restart the mysqlserver service.
# The following is an example of a Python database operation:
#! /Usr/bin/env python #-*-coding: utf8-*-import MySQLdbconn = MySQLdb. connect (host = '2017. 168.2.14 ', port = 3306, user = 'root', passwd = '000000', db = 'Demo',) # create a cursor cur = conn to operate the database. cursor () # Use the cursor cur to operate the execute () method to write a pure SQL statement, as follows: # create a data table # cur.exe cute ("create table teacher (id int (5 ), name varchar (20), class varchar (20), age varchar (10) ") # insert data # cur.exe cute (" insert into teacher values (23, 'hangsan ', 'scien Ce ', 15) ") # modify data # cur.exe cute (" update teacher set id = 100 where name = 'hangsan '") # delete data # cur.exe cute ("delete from teacher where id = 100 ") # insert a piece of data [this can also be written as follows] sqli = "insert into teacher values (% s, % s)" cur.exe cute (sqli, (23, 'hangsan ', 'science', 15) # insert multiple values to the data table at one time using execute.pdf. The returned value is the number of affected rows. Sqli = "insert into teacher values (% s, % s)" cur.exe cute.pdf (sqli, [(11, 'wangw', 'art ', 23), (8, 'john', 'Math', 22), (3, 'Tom ', 'Physical', 25),]) # Close the cursor, execute the submit operation and close the database connection cur. close () conn. commit () conn. close ()
Retrieve and output data
#! /Usr/bin/env python #-*-coding: utf8-*-import MySQLdbconn = MySQLdb. connect (host = '2017. 168.2.14 ', port = 3306, user = 'root', passwd = '000000', db = 'Demo',) cur = conn. cursor () # obtain the number of data entries in the table aa = cur.exe cute ("select * from teacher") cur. the fetchone () # fetchone () method can help us obtain the data in the table, but every execution of a row outputs the value cur that meets the conditions. fetchone ()...... cur. scroll (0, 'absolute ') # this way, you can locate the cursor to the first data in the table. info = cur. fetchur (aa) for I in info: print icur. close () conn. commit () conn. close ()
I want to help you with the introduction of Python to database operations!