---
title: "python操作mysql"
author: "lampeter123"
date: "2009-12-10T16:30:13Z"
tags: ["MySQL", "OS", "Python", "SQL"]
categories: ["编程语言"]
slug: "python-mysql"
draft: false
lastmod: "2009-12-10T16:35:56Z"
id: 545634
url: "https://lampeter123.iteye.com/blog/545634"
---

```
#-*- encoding: gb2312 -*-

import os, sys, string
import MySQLdb

try:
        conn = MySQLdb.connect(host='localhost',user='root',passwd='root',db='zftest')
except Exception, e:
        print e
        sys.exit()
cursor = conn.cursor()

sql = "INSERT INTO album (artist,title,created,updated) VALUES ('77','mytest','hongfu demo','2007-07-09')";
sql = "update album set title = '9999999999' where id = 44"
#sql = 'delete from album where id = 41'
#print sql 
cursor.execute(sql)
conn.commit()
sql = "select * from album"
cursor.execute(sql)
datas = cursor.fetchone()
#datas = cursor.fetchall()
print datas
#for s in datas:
#        print s[1] + '++++++' + s[2] + '+++++++' + s[3]
```