zoukankan      html  css  js  c++  java
  • python2.7.13安装MySQLdb模块及使用

    命令行安装

        pip install python-mysql
    

    或者在pycharm包中安装
    源码安装方式
    访问: http://www.lfd.uci.edu/~gohlke/pythonlibs/,下载MySQL_python-1.2.5-cp27-none-win_amd64.whl

    将其拷贝到Python安装目录下的Scripts目录下,在文件位置打开cmd,执行pip install MySQL_python-1.2.5-cp27-none-win_amd64.whl

    验证,python(command line)输入import MySQLdb,没报错,说明安装成功。

    #coding:utf-8
    
    import MySQLdb
    #建立和数据库的连接
    db = MySQLdb.connect(host= 'localhost',user="root",passwd="111111",db = "test")
    #获取操作游标
    cursor = db.cursor()
    #执行sql
    cursor.execute("select * from test.student")
    print cursor.fetchall()
    try:
        result =cursor.execute("insert into student (name,age) VALUES ('helloworld',24)")
        db.commit()
        print result
    except Exception as e:
        db.rollback()
    #关闭连接,释放资源
    db.close()
    
  • 相关阅读:
    关于postman返回参数
    字典(dict)
    序列(tuple)
    列表(List)
    字符串截取
    条件及循环语句
    函数
    变量和类型
    post请求
    django学习-5.获取url参数和name的作用
  • 原文地址:https://www.cnblogs.com/flyhgx/p/6734596.html
Copyright © 2011-2022 走看看