Python中操作MySQL入门实例
一、安装MySQL-python 复制代码 代码如下: # yum install -y MySQL-python 二、打开数据库连接 复制代码 代码如下: #!/usr/bin/python import MySQLdb conn = MySQLdb.connect(user='root',passwd='admin',host='127.0.0.1') 三、操作数据库 复制代码 代码如下: def insertdb(): sql = 'insert into test(name,`sort`) values ("%s","%s")' exsql = sql % ('hello','python') cur.execute(exsql) conn.commit() return 'insert success' def selectdb(): print 'cursor move to top:' row = cur.fetchone() print 'cursor move to top:' many = cur.fetchmany(count) def deletedb():
四、关闭连接 复制代码 代码如下: cur.close() conn.close() 注意顺序。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |