使用python访问mysql,需要一系列安装
linux下MySQLdb安装见
Python MySQLdb在Linux下的快速安装
-------------------------------------------------------------
以下是windows环境下的:
1. 安装数据库mysql
下载地址:http://pile_args = [ '' ]
目前就遇到这几个问题,望补充
3. 增删改查代码示例及结果(just for test)
复制代码 代码如下:
CREATE TABLE `user` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`age` varchar(255) DEFAULT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
复制代码 代码如下:
#-*- coding:utf-8 -*-
#dbtest.py
#just used for a mysql test
'''''
Created on 2012-2-12
@author: ken
'''
#mysqldb
import time, MySQLdb, sys
#connect
conn=MySQLdb.connect(host="localhost",user="root",passwd="test_pwd",db="school",charset="utf8")
cursor = conn.cursor()
#add
sql = "insert into user(name,age) values(%s,%s)"
param = ("tom",str(20))
n = cursor.execute(sql,param)
print n
#更新
sql = "update user set name=%s where Id=9001"
param = ("ken")
n = cursor.execute(sql,param)
print n
#查询
n = cursor.execute("select * from user")
for row in cursor.fetchall():
for r in row:
print r,
print ""
#删除
sql = "delete from user where name=%s"
param =("ted")
n = cursor.execute(sql,param)
print n
cursor.close()
#关闭
conn.close()