Python uses MySQL in python

Source: Internet
Author: User
Reason

Recently in tossing a small thing need to crawl the Web page. Then the parsing. Put the results in the database.

Learn that Python has an advantage in this area and choose it.

Since I have a server with MySQL installed, naturally use it. In the process of database operation encountered a lot of problems, here record, we encourage each other.

The call to MySQL in Python

Baidu can be followed by MYSQLDB database operation. View the documentation to learn about the C language API that provides a mysql-only implementation of MySQL in Python. MySQLdb is a higher level of encapsulation, therefore, it is more convenient to use. We can use MySQL, but the better way is to use MYSQLDB

Problems encountered during installation

On this page http://sourceforge.net/projects/mysql-python/can download to the latest version number of MYSQLDB, after decompression run installation, there may be some problems.

Running the installation via Python setup.py build will prompt no module named Setuptools

Workaround, install the

sudo apt-get install Python-setuptools

Run again, there may still be an error mysql_config not found

At this point we need to install Mysqld-dev

sudo apt-get install Libmysqld-dev

The error may also occur if you run it again. Like this '

Building ' mysql ' extension gcc-pthread-fno-strict-aliasing-dndebug-g-fwrapv-o2-wall-wstrict-prototypes-fpic-dver Sion_info= ("final", 0)-dversion=1.2.3-i/usr/include/mysql-i/usr/include/python2.7-c Mysql.c-o build/ Temp.linux-i686-2.7/mysql.o-dbig_joins=1-fno-strict-aliasing-duniv_linux-duniv_linux in file included from MYSQL.C : 29:0: pymemcompat.h:10:20:fatal error:python.h:no such file or directory

How to Solve

sudo apt-get install Python-dev

This step is to install some of Python's development-Used header files.

Basically, there are no more problems after the first three.

But suppose MySQL is installed on its own. And the Lib file does not put to/usr/local/lib below will also error.

The solution is to soft-connect the file to this folder, or change the system's/ETC/LD.SO.CNF file, and put the folder in which we are lib. Both methods are available. Then in Ldconfig, let it take effect.

For example, we use the first method of Ln-s/usr/local/mysql/lib/mysql/libmysqlclient*/usr/lib

Actual use

Introducing the MYSQLDB Library

Import MySQLdb

Connecting to a database

Conn=mysqldb.connect (host= "localhost", user= "root", passwd= "sa", db= "MyTable", charset= "UTF8")

The Connect method provided is used to establish a connection to the database, receive several parameters, and return the connection object.

Running statements and fetching results

Cursor=conn.cursor () N=cursor.execute (Sql,param)

First, we use the connection object to get a cursor object, and then we use the method provided by the cursor to do the work. These methods contain two main classes: 1. Run command, 2. Receive return value

Later, specifically. It's unknown here.

End. To close a database connection

You need to close the pointer object and the connection object separately. They have names, the same way.

Cursor.close ()

Conn.close ()

Frequently use the Operations API

Support for transactional operations, standard method commit () Commit

Rollback () rollback

The cursor is used to run the command:

Callproc (self, procname, args): Used to run stored procedures, received as stored procedure name and list of parameters, return value is the number of rows affected

Execute (Self, query, args): Runs a single SQL statement, receives the number of references to the SQL statement itself and the list of parameters used, and returns the number of rows affected

Executemany (self, Query, args): Runs a heads-up SQL statement, but runs repeatedly in the list of parameters, the return value is the number of rows affected Nextset (self): move to the next result set

The cursor is used to receive the return value of the method:

Fetchall (self): receives all the returned result rows.

Fetchmany (self, Size=none): Receives a size bar that returns the result row. Assuming that the value of size is greater than the number of result rows returned, the cursor.arraysize data is returned.

Fetchone (self): Returns a result row.

Scroll (self, value, mode= ' relative '): Moves the pointer to a row. Assuming that mode= ' relative ', the value bar is moved from the current row, assuming mode= ' absolute ', Represents the move value bar from the first row of the result set.

In the end, insert a sentence

The computer upgraded to ubuntu14.04 again, the previous blog repository was gone, and once again pulled back from GitHub. Something went wrong in the middle. I deleted the file and this article was almost gone. It's just good to see this article right now.

  • 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.