zoukankan      html  css  js  c++  java
  • python查询MySQL数据库操作

    python中的pymysql模块可实现与MySQL数据库的交互,基本原理:

    可通过以下步骤来实现该功能:

    1、在python中安装pymysql:

    pip install pymysql

    2、安装完成后导入pymysql模块

    from pymysql import *

    3、首先创建与MySQL的连接:

    conn= connect(host='localhost', port=3306, user = 'root', passward = '密码‘, database = '要操作的数据库名', charset = 'utf8')

    创建cursor对象:

    cursor = conn.cursor()

    查询MySQL数据库:

    cursor.execute('sql语句')

    该命令只会显示执行命令后变化的行数,不会显示查询结果,可通过以下命令显示:

    dis = cursor.fetchall()

    fetch有三种:fetchone、fetchmany及fetchall。

    在查询完毕后,需要关闭连接,首先关闭cursor,再关闭connection:

    cursor.close()
    conn.close()
  • 相关阅读:
    Merge Two Sorted Lists
    4Sum
    Letter Combinations of a Phone Number
    3Sum Closest
    3Sum
    Longest Common Prefix
    Roman to Integer
    Integer to Roman
    Container With Most Water
    Regular Expression Matching
  • 原文地址:https://www.cnblogs.com/jdwfff/p/10494407.html
Copyright © 2011-2022 走看看