Python中操作MySQL入门实例
一、安装MySQL-python
# yum install -y MySQL-python
二、打开数据库连接
#!/usr/bin/python
import MySQLdbconn = MySQLdb.connect(user='root',passwd='admin',host='127.0.0.1')
conn.select_db('test')
cur = conn.cursor()
三、操作数据库
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():
sql = 'select `name` from test where `sort` = "%s"'
exsql = sql % ('python')
count = cur.execute(exsql)
for row in cur:
print rowprint 'cursor move to top:'
cur.scroll(0,'absolute')row = cur.fetchone()
while row is not None:
print row
row = cur.fetchone()print 'cursor move to top:'
cur.scroll(0,'absolute')many = cur.fetchmany(count)
print manydef deletedb():
sql = 'delete from test where `sort` = "%s"'
exsql = sql % ('python')
cur.execute(exsql)
conn.commit()
return 'delete success'
print insertdb()
print insertdb()
selectdb()
print deletedb()
四、关闭连接
cur.close()
conn.close()
注意顺序。
您可能感兴趣的文章:
- Python中操作MySQL入门实例
- Mysql入门笔记(2):Python操作MySQL数据库9个实用实例
- java操作mysql入门代码实例(含插入、更新和查询)
- Python操作Mysql实例代码教程在线版(查询手册)
- Python操作Mysql实例代码教程(查询手册)
- Python操作Mysql实例代码教程(查询手册)
- Python操作Mysql实例代码教程
- Python读取ini文件、操作mysql、发送邮件实例
- Python操作Mysql实例代码教程
- Python操作Mysql实例代码教程(查询手册)
- python操作Mysql实例
- Python 操作Mysql实例代码教程
- Python读取ini文件、操作mysql、发送邮件实例
- python操作mysql实例代码
- Python操作Mysql实例代码
- Python读取ini文件、操作mysql、发送邮件实例
- Mysql入门笔记(0):python操作mysql数据库(增,删,改,查)
- Python操作MySQL的9个实用实例
- python 03 入门和操作mysql
- python操作mysql详细实例