• Python连接Mysql数据库


    最近对Python比较感兴趣

    写了一个简单的Demo来连接一下MySQL数据库,实现了简单的增删改查询功能,但是乱码问题还未能解决,等抽时间把所有乱码问题整理一下在写一篇博客。

    希望大家多多指点:

    注意:

    1,Python连接Mysql 数据库需要MysqlDb(MySQL-python-1.2.4b4.win32-py2.7.exe)这个是我用的,貌似不支持Python3.0的,我的Python用的2.7 的版本,MysqlDb下载好之后,直接点击会默认安装到Python的路径下(安装MySQLDb之前,MySQL也是要安装好的,mysqldb的作用就是相当于让Python能够连接到mysql中个人理解)


    控制台运行截图如下:


    #!/usr/bin/python

    #encoding=utf-8 
    import sys 
    import MySQLdb  
    reload(sys) 
    sys.setdefaultencoding('utf-8') 
    conn=MySQLdb.connect(host="localhost",user="root",passwd="root",db="ait",charset='utf8') 


    cursor =conn.cursor()
    #删除
    sql1="delete from user"
    f=cursor.execute(sql1)
    if(f>0):
    print 'delete success'
    conn.commit()


    #插入
    sql2='insert into user (username,password) value (11,22)' 
    f=cursor.execute(sql2)
    if(f>0):
    print 'insert success'
    conn.commit()


    #修改
    sql3='update user set username=123,password=456' 
    f=cursor.execute(sql3)
    if(f>0):
    print 'update success'
    conn.commit()


    #查询
    sql4 ="select * from user "
    cursor.execute(sql4) 
    row=cursor.fetchone() 
    print row


    #关闭
    cursor.close() 
    conn.close()
  • 相关阅读:
    POJ 2234 Matches Game 尼姆博弈
    复杂问题的简单抽象:魔兽世界中的兔子们
    POJ 2368 巴什博奕
    POJ 1067 取石子游戏 威佐夫博弈
    Codeforces 704A Thor 队列模拟
    Codeforces 703B Mishka and trip
    P1447 [NOI2010]能量采集
    P2652 同花顺
    P2034 选择数字 / P2627 [USACO11OPEN]Mowing the Lawn G
    P2515 [HAOI2010]软件安装
  • 原文地址:https://www.cnblogs.com/cxsabc/p/10627718.html
Copyright © 2020-2023  润新知