Use mysql python in python
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 C language API that directly implements mysql when _ mysql is provided. 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
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.
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
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/lib
Actual use
-
Introduce the MySQLdb Library
Import MySQLdb
-
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.
-
Execute the statement and obtain the result
Cursor = conn. cursor ()
N=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.
-
Close database connection
You need to close the pointer object and connection object respectively. they have the same name.
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!