Python 操作mysql 增删改查

#!C:/Python27  
#coding=utf-8

import MySQLdb

# MySQL相关设置
mysql_host = '127.0.0.1'
mysql_user = 'root'
mysql_passwd = 'test'
mysql_port = '3306'
mysql_database = 'test'


def my_connect():
    """链接数据库"""

    global conn, cursor
    
    #print MySQLdb.version_info
    
    try:
        conn = MySQLdb.connect(host=mysql_host, user=mysql_user, passwd=mysql_passwd,charset='utf8', autocommit=True, db=mysql_database)
        
        print u"\nMessage:连接MySQL成功"
    except Exception:
        print(u"\nMessage:连接失败")
        exit(20)
    cursor = conn.cursor()
    
def add():
    
    #add
    try:
        sql = "insert into user(name,age,insertTime) values(%s,%s,%s)"
        print sql
        param = ("tom",int(20),"2017-09-19 17:00:17")
        my_connect()#打开链接
        cursor.execute(sql,param)
        cursor.close()
        conn.close()
    except StandardError as e:
        print "错误在这里》》》》》",e,"《《《《《错误在这里"
        #conn.rollback()
def update():
    #更新
    my_connect()
    sql = "update user set name='%s' where id='%d'" %("ken",15)
    print  sql
    try:
        cursor.execute(sql)
        cursor.close()
        conn.close()
    except StandardError as e:
        print "更新数据异常",e
def Select():
    #查询
    try:
        n = cursor.execute("SELECT t.id,t.name,t.age,t.* from user t  ORDER BY t.insertTime DESC LIMIT 10")
        data = cursor.fetchall()
        for row in data:
            #注意int类型需要使用str函数转义
            print ('id: ',row[0], '  name: ',row[1],' age ',row[2])
        #提交事务
        cursor.close()#关闭游标
        conn.close()#释放数据库资源        
    except  Exception :
        #异常情况下,进行事务回滚
        conn.rollback()
        print(u"\n操作失败,数据已回滚")
def delete():
    #删除
    my_connect()
    sql = "delete from user where name='%s'"%("tom1")
    #parama =("tom")
    cursor.execute(sql)
    cursor.close()
    conn.close()
my_connect()
Select()
add()
update()
delete()

你可能感兴趣的:(Python,资料)