Usually the main programming language is Java, the development of the main use of MySQL, often in order to test, debugging purposes need to operate the database, such as backup, insert test data, modify test data, some times can not be simple with SQL to complete the task, or are very good to complete the task, write in Java and a little too troublesome , I think of Python. Python syntax is concise, without compiling, can be better to complete the task. Today, I looked at Python's operation on MySQL and made a note of it.
First of all, install the required environment, MySQL and Python will not say, necessary things.
Mainly installs the MySQLdb, may go to sf.net to download, the specific address is http://sourceforge.net/projects/mysql-python/
If you use Ubuntu, direct
sudo apt-get install Python-mysqldb
After the installation is complete, you can test it in the Python interpreter
Input
Python code
- Import mysqldb #注意大小写!!
If you do not make an error, it proves that the installation was successful and may continue
MySQLdb in Python is equivalent to the Java MySQL JDBC Driver,python also has a similar data interface Specification Python DB API,MYSQLDB is the implementation of MySQL. The operation is also relatively simple and other platforms or language operations database, is to establish a connection to the database system, and then to the database input SQL, and then get the results from the database.
First write the simplest, create a database:
Python code
- #!/usr/bin/env python
- #coding =utf-8
- ###################################
- # @author Migle
- # @date 2010-01-17
- ##################################
- #MySQLdb Example
- #
- ##################################
- Import MySQLdb
- #建立和数据库系统的连接
- conn = MySQLdb.connect (host=' localhost ', user=' root ', passwd=' longforfreedom ')
- #获取操作游标
- cursor = Conn.cursor ()
- #执行SQL, create a database.
- Cursor.execute ("" "Create Database Python" ")
- #关闭连接, freeing up resources
- Cursor.close ();
Create a database, create a table, insert data, insert multiple data
Python code
- #!/usr/bin/env python
- #coding =utf-8
- ###################################
- # @author Migle
- # @date 2010-01-17
- ##################################
- #MySQLdb Example
- #
- ##################################
- Import MySQLdb
- #建立和数据库系统的连接
- conn = MySQLdb.connect (host=' localhost ', user=' root ', passwd=' longforfreedom ')
- #获取操作游标
- cursor = Conn.cursor ()
- #执行SQL, create a database.
- Cursor.execute ("" "Create database if not exists Python " ")
- #选择数据库
- conn.select_db (' python ');
- #执行SQL, create a data table.
- Cursor.execute ("" "CREATE TABLE test (ID int, info varchar )" "")
- Value = [1,"inserted?"];
- #插入一条记录
- Cursor.execute ("INSERT into test values (%s,%s)", value);
- Values=[]
- #生成插入参数值
- For I in range:
- Values.append ((i,' Hello mysqldb, I am recoder ' + str (i)))
- #插入多条记录
- Cursor.executemany ("" "insert into test values (%s,%s)" ", values);
- #关闭连接, freeing up resources
- Cursor.close ();
The process of querying and inserting is similar, just one more step to get the results of the query
Python code
- #!/usr/bin/env python
- #coding =utf-8
- ######################################
- #
- # @author Migle
- # @date 2010-01-17
- #
- ######################################
- #
- # MYSQLDB Query
- #
- #######################################
- Import MySQLdb
- conn = MySQLdb.connect (host=' localhost ', user=' root ', passwd=' longforfreedom ', db=' python ')
- cursor = Conn.cursor ()
- Count = Cursor.execute (' select * from Test ')
- Print ' has a total of%s records ', Count
- #获取一条记录, each record is returned as a single tuple
- Print "Get only one record:"
- result = Cursor.fetchone ();
- Print result
- #print ' ID:%s info:%s '% (Result[0],result[1])
- Print ' ID:%s info:%s '% result
- #获取5条记录, note that because Fetchone () was previously executed, the cursor already refers to the second record, that is, all records starting from the second bar
- Print "Get only 5 records:"
- Results = Cursor.fetchmany (5)
- For R in results:
- Print R
- Print "Get all results:"
- #重置游标位置, 0, offset, Mode=absolute | Relative, default is relative,
- Cursor.scroll (0,mode=' absolute ')
- #获取所有结果
- Results = Cursor.fetchall ()
- For R in results:
- Print R
- Conn.close ()
manipulating MySQL with Python