First of all, the environment required for installation is not mentioned in Mysql and Python.
MySQLdb is installed.
Free in http://linux.bkjia.com/
The username and password are both www.bkjia.com
The specific download directory is/pub/2011/08/26/Python. Use MySQLdb to connect to MySQL/MySQLdb/
If Ubuntu is used, run sudo apt-get install python-mysqldb directly. After the installation is complete, test it in the Python interpreter and enter
Import MySQLdb # case sensitive !! If no error is reported, the installation is successful and may continue.
First, write the simplest one to create a database:
- #! /Usr/bin/env python
- # Coding = UTF-8
- ImportMySQLdb
- # Establish a connection to the Database System
- Conn = MySQLdb. connect (host ='Localhost', User ='Root', Passwd ='Longforfreedom')
- # Retrieve operation cursor
- Cursor = conn. cursor ()
- # Execute SQL to create a database.
- Cursor.exe cute ("Create database python """)
- # Close the connection and release resources
- Cursor. close ();
Create databases, create tables, insert data, and insert multiple data records
- #! /Usr/bin/env python
- # Coding = UTF-8
- ImportMySQLdb
- # Establish a connection to the Database System
- Conn = MySQLdb. connect (host ='Localhost', User ='Root', Passwd ='Longforfreedom')
- # Retrieve operation cursor
- Cursor = conn. cursor ()
- # Execute SQL to create a database.
- Cursor.exe cute ("Create database if not exists python """)
- # Selecting a database
- Conn. select_db ('Python');
- # Execute SQL to create a data table.
- Cursor.exe cute ("Create table test (id int, info varchar (100 ))""")
- Value = [1,"Inserted? "];
- # Insert a record
- Cursor.exe cute ("Insert into test values (% s, % s )", Value );
- Values = []
- # Generating insert parameter values
- ForIInRange (20):
- Values. append (I,'Hello mysqldb, I am recoder'+ Str (I )))
- # Insert multiple records
- Cursor.exe cute.pdf ("Insert into test values (% s, % s )""", Values );
- # Close the connection and release resources
- Cursor. close ();
The query process is similar to the insert process, but there is only one more step to get the query result.
- #! /Usr/bin/env python
- # Coding = UTF-8
- ImportMySQLdb
- Conn = MySQLdb. connect (host ='Localhost', User ='Root', Passwd ='Longforfreedom', Db ='Python')
- Cursor = conn. cursor ()
- Count = cursor.exe cute ('Select * from Test')
- Print 'Total % s record records', Count
- # Obtain a record. Each record is returned as a tuples.
- Print "Get only one record :"
- Result = cursor. fetchone ();
- PrintResult
- # Print 'id: % s info: % s' % (result [0], result [1])
- Print 'Id: % s info: % s'% Result
- # Obtain five records. Note that the cursor has pointed to the second record because fetchone () was previously executed, that is, all records starting from the second record.
- Print "Get only five records :"
- Results = cursor. fetchtasks (5)
- ForRInResults:
- PrintR
- Print "Get all results :"
- # Reset the cursor position. The value 0 indicates the offset. mode = absolute | relative. The default value is relative,
- Cursor. scroll (0, Mode ='Absolute')
- # Getting all results
- Results = cursor. fetchall ()
- ForRInResults:
- PrintR
- Conn. close ()