下载MySQL_python-1.2.2-py2.5-win32.egg
拷目录下mysqldb,_mysql_py,_mysql_pyd,_mysql_exce..到site_packages目录下
#-*- encoding: gb2312 -*-
import os, sys, string
import MySQLdb
#导入mysqldb模块
#建立数据联接
try:
conn = MySQLdb.connect(host='localhost',user='root',passwd='root',db='test')
except Exception, e:
print e
sys.exit()
#获得句枸
cursor = conn.cursor()
#下面的SQL你会吧
sql = "INSERT INTO topics (tid, subject, content) VALUES ('', 'test', 'hongfu')";
cursor.execute(sql)
sql = "SELECT * FROM test LEFT JOIN topics ts USING(t_id) WHERE ts.t_id > 10";
#执行
cursor.execute(sql)
#查出所有记录
alls = cursor.fetchall()
if alldata:
for rec in alls:
print rec[0], rec[1]
#关闭句枸
cursor.close()
关闭数据联接
conn.close()