`
bzu334fc
  • 浏览: 15172 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

【转载】Python连接MySQL数据库

 
阅读更多

【转载】Python连接MySQL数据库
2011年08月07日
  原文地址:Python连接MySQL数据库
  文章来自:python - 搜搜关键词订阅
  
  
  
  Python连接MySQL数据库 
  (2011-08-06 18:04:45)
    1.连接的关键是安装MySQLdb模块(下载地址:   http://www.codegood.com/archives/4),我用的Python版本是2.7.2,要下载与Python相对应的版本.
   
    2.下载好后安装,它会自动检测到计算机Python的安装路径,并自动填写模块解压路径(我的是:D:\Python27\Lib\site-packages\)。2.7版本的就是好,再也不用修改MySQLdb模块下的文件了。
   
  3.编写conmysql.py:
   
  def getmysqlinfo(): 
    import MySQLdb 
    conn = MySQLdb.connect(host="localhost", \ 
            user="root", \ 
            passwd="wangxing", \ 
            db="test") 
    cursor = conn.cursor() 
    cursor.execute("select * from age") 
    res = cursor.fetchall() 
    print res 
    cursor.close() 
    conn.close()
   
    4.Python (command line)下运行测试:
  
  
  
    
  前一篇: Python 遍历目录 获得文件属性 
  原文地址:Python连接MySQL数据库
  文章来自:python - 搜搜关键词订阅
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics