使用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