zoukankan      html  css  js  c++  java
  • Python操作MySQL数据库

    一、安装mysqlclient库

    注:需要在cmd命令界面下安装

    这里安装的是1.3.12版本:pip install mysqlclient==1.3.12

    二、Python操作数据库

           首先需要连接操作的数据库,host为数据库的主机IP地址,port为MySQL的端口号,user为数据的用户名,passwd为数据库的登录密码,database为数据库的名称。

    import MySQLdb
    
    # 连接mysql数据库,不加charset="utf8",打印的中文是乱码
    connction=MySQLdb.Connect(host='118.24.25.113 ',
                       user='root',
                       passwd='123456',
                       database='ff_test',
                       port=3366,
                       charset="utf8")
    
    # 通过cursor创建游标
    cursor=connction.cursor()
    
    # SQL语句查询student表中所有记录
    sql="select * from  student"
    
    # 执行sql语句
    cursor.execute(sql)
    
    # 获取一条数据,返回的是一个元祖,里面每一个元素代表一个字段
    result=cursor.fetchone()
    
    print(result)
  • 相关阅读:
    hive metastore && hiveserver2 . 基本配置
    Flink HA 搭建坑
    protobuf 编译安装
    编译Hadoop 2.7.2支持压缩 转
    centos 6挂载磁盘
    python
    python之面向对象(一)
    python
    python-文件压缩和解压
    python-configparser模块
  • 原文地址:https://www.cnblogs.com/lengjf/p/9559276.html
Copyright © 2011-2022 走看看