zoukankan      html  css  js  c++  java
  • python连接MySQL

    使用Python连接MySQL

    安装MySQL

    yum install -y mysql-server mysql-devel
    

    安装MySQL-python驱动

    pip install MySQL-python
    

    测试

    ➜  2-3_Django ipython
    Python 2.7.11 (default, Mar  7 2017, 08:25:27) 
    Type "copyright", "credits" or "license" for more information.
    
    IPython 5.3.0 -- An enhanced Interactive Python.
    ?         -> Introduction and overview of IPython's features.
    %quickref -> Quick reference.
    help      -> Python's own help system.
    object?   -> Details about 'object', use 'object??' for extra details.
    
    In [1]: import MySQLdb
    
    In [2]: 
    

    操作示例

    #coding=utf-8
    import MySQLdb
    
    conn= MySQLdb.connect(
            host='localhost',
            port = 3306,
            user='root',
            passwd='123456',
            db ='test',
            )
    cur = conn.cursor()
    
    # 创建数据表
    cur.execute("create table student(id int ,name varchar(20),class varchar(30),age varchar(10))")
    
    # 插入一条数据
    cur.execute("insert into student values('2','Tom','3 year 2 class','9')")
    
    
    #修改查询条件的数据
    # cur.execute("update student set class='3 year 1 class' where name = 'Tom'")
    
    #删除查询条件的数据
    # cur.execute("delete from student where age='9'")
    
    cur.close()
    conn.commit()
    conn.close()
    

    相关错误解决方法

    编译时报如下错误

    ‘_mysql_connectionobject’ has no member named ‘open’
    

    http://f.dataguru.cn/thread-132530-1-1.html

  • 相关阅读:
    Java 运动模糊
    Efounds笔试
    Algorithms code
    Java 画图
    Java 笔记
    Java 对二值化图片识别连通域
    工厂模式
    数据库克隆
    SQL优化
    微信调起jssdk一闪而过
  • 原文地址:https://www.cnblogs.com/ZhangRuoXu/p/6706414.html
Copyright © 2011-2022 走看看