使用Pycharm操作MySql数据库


安装PyMySql模板

  pip install pymysql

配置MySQL连接参数

  pymysql.Connect()

    host、post、user、passwd、db、charset

获取cursor()对象

  cursor()

执行MySql语句

  cursor().execute()

  cursor().commit()

具体代码:

# -- coding: utf-8 --
import pymysql

def load_all_from_mysql():
    connect = pymysql.Connect(
        host = 'localhost',
        port = 3306,
        user = 'root',
        password = '******',
        db = '数据库名',
        charset = 'utf8'
    )
    cursor = connect.cursor()
    sql = "SELECT * from 表名"
    cursor.execute(sql)
    # print(cursor.fetchall())
    for row in cursor.fetchall():
        print(row)

load_all_from_mysql()

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM