Use mysql in python

Source: Internet
Author: User

Reason

Recently, I am tossing a small item to capture the online page, parse it, and put the result in the database. If you understand the advantages of Python in this regard, you can choose it. Because I have a server installed with mysql, you can use it naturally. Many problems have been encountered during the database operation. Let's record them here.

Call mysql in python

After Baidu, you can use MySQLdb to perform database operations, view the documentation, and learn that python provides a mysql that directly implements the mysql C language API. MySQLdb is an encapsulation of it on a higher layer, so it is more convenient to use. We can use mysql, but a better way is to use MySQLdb.

Problems encountered during installation

On this page, refer.

  • Passpython setup.py buildThe installation prompt is displayed.No module named setuptools
    Solution: Install
    1
    sudo apt-get install python-setuptools
    • If you run the command again, mysql_config not found may still fail.
      In this case, install mysqld-dev.
      12
      sudo apt-get install libmysqld-dev  
      • An error may occur when you execute the command again'

        Building 'mysql' extension gcc-pthread-fno-strict-aliasing-DNDEBUG-g-fwrapv-O2-Wall-Wstrict-prototypes-fPIC-Dversion_info = (1, 2, 3, 'final', 0)-Dversion = 1.2.3-I/usr/include/mysql-I/usr/include/python2.7-c mysql. c-o build/temp. A linux-i686-2.7/mysql. o-DBIG_JOINS = 1-fno-strict-aliasing-DUNIV_LINUX In file already ded from mysql. c: 29: 0: pymemcompat. h: 10: 20: fatal error: Python. h: No such file or directory

        Solution

        1
        sudo apt-get install python-dev 

        This step is to install some development header files for python.

        • Basically, there will be no other problems after the first three methods. However, if mysql is installed on its own and the lib file is not put under/usr/local/lib, an error is returned.
          The solution is to connect the file to this directory, or modify the/etc/ld. so. cnf file of the system and put the directory where lib is located. You can use either of the two methods, and then make them take effect in ldconfig.
          For example, we use the first method.ln -s /usr/local/mysql/lib/mysql/libmysqlclient* /usr/libActual use
          1. Introduce the MySQLdb Library

            Import MySQLdb

          2. Connect to database

            Conn = MySQLdb. connect (host = "localhost", user = "root", passwd = "sa", db = "mytable", charset = "utf8 ")
            The connect method is used to establish a connection with the database, receive several parameters, and return the connection object.

          3. Execute the statement and obtain the result

            Cursor = conn. cursor () nw.cursor.exe cute (SQL, param)
            First, we use the connection object to obtain a cursor object. Next, we will use the method provided by cursor to work. These methods include: 1. execute commands; 2. Receive returned values.
            I will go into details later. I will not go into details here.

          4. Close database connection
            You need to close the pointer object and connection object respectively. They have the same name.

            12
            cursor.close()         conn.close() 
            Common Operation APIs

            Support for transaction operations, standard method commit () commit
            Rollback () rollback
            Cursor is used to execute commands:
            Callproc (self, procname, args): used to execute a stored procedure. The received parameters are the stored procedure name and parameter list. The returned values are the number of affected rows.
            Execute (self, query, args): executes a single SQL statement. The received parameters are the SQL statement itself and the list of parameters used. The returned values are the affected rows.
            Executemany (self, query, args): executes a single-pick SQL statement, but repeats the parameters in the parameter list. The returned value is the number of affected rows nextset (self): Move to the next result set.
            Cursor is used to receive the returned value:
            Fetchall (self): receives all returned result rows.
            Fetchmany (self, size = None): receives the size of returned results rows. If the size value is greater than the number of returned results rows, the returned cursor. arraysize data is returned.
            Fetchone (self): returns a result line.
            Scroll (self, value, mode = 'relative '): Move the pointer to a row. if mode = 'relative ', the value bar is moved from the current row. If mode = 'absolute', the value bar is moved from the first row of the result set.

            Insert the last sentence

            I upgraded my computer to ubuntu14.04 and re-installed it. My previous blog repository was gone and I pulled it back from github. Something went wrong. I deleted the file and this article was almost gone, but now I can see this article. Haha ~~


            Original article address: Success!

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.