Python instance code to connect to MySQL

Source: Internet
Author: User

mysqldb:http://sourceforge.net/projects/mysql-python/
After the download is uncompressed and placed in the%python_home%/lib/site-packages directory, Python will automatically find the package.
MySQLdb is basically the Python version of the MySQL C API, followed by the Python Database API Specification v2.0.

Other:

1. Platform and version
Linux kernel 2.6,GCC 3.4.4,GLIBC 2.4
Python 2.4.3
MySQL 5.0.19
Mysql-python 1.2.1-P2


2. Installing Mysql-python
Tar Xvfz mysql-python-1.2.1_p2.tar.gz
CD MYSQL-PYTHON-1.2.1_P2
Python setup.py Build
Python setup.py Install

3. Use
Import MySQLdb


3.1. Connect
conn = mysqldb.connection (host, user, password, dbname)


3.2. Select a database
conn.select_db (' database name ')


3.3. Get cursor
cur = conn.cursor ()


3.4. Cursor Position setting
Cur.scroll (int, mode)
Mode can be a relative position or an absolute position, respectively relative and absolute.


3.5. Select
Cur.execute (' select clause ')
For example
Cur.execute (' select * FROM MyTable ')


row = Cur.fetchall ()
Or:
Row1 = Cur.fetchone ()


3.6. Insert
Cur.execute (' inset clause ')
For example
Cur.execute (' INSERT into table (Row1, Row2) VALUES (/' 111/',/' 222/') ')

Conn.commit ()

3.7. Update
Cur.execute (' update clause ')
For example
Cur.execute ("Update table Set row1 = ' where row2 = ' row2 '")

Conn.commit ()


3.8. Delete
Cur.execute (' delete clause ')
For example
Cur.execute ("Delete from table where Row1 = ' row1 '")


Conn.commit ()

Http://www.python123.cn/PythonInternet/20090609_11.html

Python instance code to connect to MySQL

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.