install Mysql-python under Windows
Download Address: https://pypi.python.org/pypi/MySQL-python/1.2.5 install to the system.
Install Mysql-python under Linux to connect to MySQL:
Download Address: https://pypi.python.org/pypi/MySQL-python/
After extracting, go to directory and execute Python setup.py install
Frequently encountered problems during the installation process:
1, the hint can not find mysql_config words, is generally due to MySQL is LNMP one-click installation package installation, the path
FIX: Locate Mysql_config find Mysql_config The location of this file, and then ln-s do a soft connection to/usr/bin/under.
2, Ubuntu under the prompt missing ' x86_64-linux-gnu-gcc ', need to install Python-dev package:
FIX: sudo apt-get install python-dev-y
3, CentOS prompt command ' gcc ' failed with exit status 1
Solution: Yum Install gcc python-devel-y
After the installation is complete, go to Python and execute import mysqldb to see if the import succeeds.
Add:
When I was operating in Ubuntu, I found that I could not connect to the database, SS-LNT found that MySQL only listens on port 3306 on the loopback address and needs to be modified.
Modify Ubuntu MySQL to change its listening port 127.0.0.1:3306 to allow external connections:
Edit/etc/mysql/my.cnf (possibly configuration parameters in other files in this directory, look for them)
modifying bind-address = 0.0.0.0 means that any IP access is allowed.
Then perform/etc/init.d/mysql restart restart the MySQLServer service to
# Here is an example of a Python operations database:
#!/usr/bin/env python#-*-coding:utf8-*-import mysqldbconn = mysqldb.connect (host = ' 192.168.2.14 ', port = 3306,user = ' r Oot ', passwd = ' 123456 ', db = ' demo ', # operation database first need to create cursor cur = conn.cursor () # The Execute () method can be written to a pure SQL statement via the cursor cur operation, as follows: # Create a data table # Cur.execute ("CREATE TABLE teacher (ID int (5), name varchar (), class varchar (), age varchar (10))") # Insert Data # Cur.execute (" Insert into teacher values (Zhangsan ', ' science ', 15) ") # Modify Data # Cur.execute (" Update teacher set id=100 where Name= ' Zhangsan ' ") # Delete Data # cur.execute (" Delete from teacher where id=100 ") #插入一条数据" You can also use the following notation "sqli=" insert into teacher values ( %s,%s,%s,%s) "Cur.execute (Sqli, (max, ' Zhangsan ', ' science ', 15) # Use Executemany to insert multiple values into the data table once, and return the value to the number of rows affected. sqli= "INSERT into teacher values (%s,%s,%s,%s)" Cur.executemany (sqli,[(one, ' Wangwu ', ' art ', '), (8, ' John ', ' math ', 22), ( 3, ' Tom ', ' physical ', 25),]) # finally close the cursor, perform the commit operation, and close the database connection Cur.close () Conn.commit () Conn.close ()
Retrieving and outputting data
#!/usr/bin/env python#-*-coding:utf8-*-import mysqldbconn = mysqldb.connect (host = ' 192.168.2.14 ', port = 3306,user = ' r Oot ', passwd = ' 123456 ', db = ' demo ', ' cur = conn.cursor () # Get the number of data in the table AA = Cur.execute ("SELECT * from Teacher") Cur.fetchone () The # Fetchone () method can help us get the data in the table, but each time the output is executed one line satisfies the condition of the value Cur.fetchone () ... cur.scroll (0, ' absolute ') # This allows the cursor to be positioned to the first data in the table info = Cur.fetchmany (AA) for I in Info:print icur.close () conn.commit () Conn.close ()
About Python on the database operation of the small part of the introduction to everyone so much, I hope to help you!