【linux系统下载_学习_教程_编程_软件】提供全系列Linux发行版下载学习-Linux部落
当前位置: 主页 > Linux脚本编程 >

python操作mysql

时间:2017-09-12 15:58来源:原创 作者:admin 点击:
------分隔线----------------------------
# encoding: utf-8
#!/usr/bin/python
import MySQLdb

db = MySQLdb.connect(host="127.0.0.1",user="linuxzone",passwd="qwe321",db="mysql",charset="utf8" )
cursor = db.cursor()

#查询数据
sql = "SELECT user,host FROM mysql.user"
      
try:
   cursor.execute(sql)
   results = cursor.fetchall()
   for row in results:
       user = row[0]
       host = row[1]
       print "user=%15s,host=%15s" % ( user,host )
       print "user=" + user + ",host=" + host
except:
   print "Error: unable to fecth data"

#插入数据
insert = "insert into dept (id,name) values (2,'linuxzone')"

try:
   cursor.execute(insert)
   db.commit()
except:
   print "Error: insert data error"
db.close()

#修改,删除数据操作跟插入数据一样



本文地址:http://www.linuxzone.net/shell/168.html
------分隔线----------------------------
相关文章
栏目列表