zoukankan      html  css  js  c++  java
  • python操作MySQL数据库(windows)

    使用python操作mysql数据库需要安装一个第三方的模块, MySQLdb

    先简单说下MySQLdb在windows下的快速安装, 其他环境下安装在此不多赘述

    说一下windows下安装已经编译好的版本, 一分钟左右就可以搞定, 以下是32位和64位的两个版本, 安装方法一样

    MySQL-python-1.2.3.win32-py2.7.exe下载链接:

    http://pan.baidu.com/s/1pJvyVsJ 密码:fepi

    MySQL-python-1.2.3.win-amd64-py2.7.exe

    链接:http://pan.baidu.com/s/1hqkNRjI 密码:b70m

    安装步骤上几个截图简单说明:

    双击安装, 选择安装路径, 完成

    测试:

    >>>import MySQLdb

    没有报错即可。

    查询数据例子:

     1 #! -*-coding:utf-8 -*- 
     2 #/usr/bin/env python
     3 
     4 '''
     5 Created on 2014-03-11 15:35:24
     6 @author: leaf
     7 @summary: test python mysql
     8 '''
     9 
    10 import MySQLdb 
    11 
    12 try:
    13     conn = MySQLdb.connect( host = 'localhost', user = 'root', passwd = 'test', db = 'test', port = 3306 )
    14     cursor = conn.cursor()
    15     cursor.execute( 'select * from users' )
    16     alldata = cursor.fetchall()
    17     if alldata:
    18          for rec in alldata:
    19              print rec[0],rec[1]
    20     cursor.close()
    21     conn.close()
    22 except MySQLdb.Error, e:
    23     print "Mysql Error %d: %s" %(e.args[0], e.args[1])
  • 相关阅读:
    20209/29
    2020/9/30
    2020/10/1
    ATM测试总结报告
    20201020 千锤百炼软工人
    20201024 千锤百炼软工人
    20201025 千锤百炼软工人
    20201023 千锤百炼软工人
    20201018 千锤百炼软工人
    20201022 千锤百炼软工人
  • 原文地址:https://www.cnblogs.com/leaf1117/p/3593733.html
Copyright © 2011-2022 走看看