Let's first look at the Python connection MySQL code:
#! /Usr/bin/env python #-*-Coding: UTF-8-*-# This statement tells python to use UTF-8 Encoding #===================================================== ======================================== # # NAME: Python MySQL test # # AUTHOR: benyur # DATE: 2004-12-28 # # COMMENT: this is an example of python connection to mysql. # #===================================================== ========================================"" * ***** This is a MySQL test ***** Select: Conn = Connection () Conn. select_db (test) Cur = conn. cursor () Cur.exe cute (select * from user) Cur. scroll (0) Row1 = cur. fetchone () Row1 [0] Row1 [1] Row1 [2]
Insert: Cur.exe cute (insert into user (name, passwd) values (\ benyur \, \ 12345 \)) Cur. insert_id ()
Update: Cur.exe cute (update user set passwd =\123456 \ where name = \ benyur \)
Delete: Cur.exe cute (delete from user where id = 2) ********************************** "" From MySQLdb import * Def conn (): Conn = Connection () Conn. select_db (test) Cur = conn. cursor () Cur.exe cute (select * from user) Cur. scroll (0) Row1 = cur. fetchone () Row1 [0] Row1 [1] Row1 [2] Def usage (): Print _ doc __ If _ name __=__ main __: Usage () |
MySQLdb: http://sourceforge.net/projects/mysql-python/
Download and decompress the package and put it in the % Python_HOME % \ Lib \ site-packages directory. python will automatically find this package.
MySQLdb is basically the Python version of MySQL c api and follows the Python Database API Specification v2.0.