Python與資料庫

一、建構資料庫

XAMPP ===> MySQL  

資料庫相關https://tech.digitgeek.net/t/237.html


二、python環境

1.python 環境的查看

conda info -e

2.建立環境

conda create --name db2024 python=3.9

3.進入環境

conda activate db2024

4.安裝套件

pip install seaborn
pip install matplotlib
pip install pandas
pip install numpy
pip install mysqlclient

5. 到指定的資料夾內(指定路徑)

cd 路徑 (python程式所在位置)

三、撰寫python程式碼

連結資料庫

抓取資料表


4 條回復   |  直到 17天前 | 195 次瀏覽




熱門評論
  1. 1
    digitgeek 17天前 支持  0 | 反對  0

    一、

    import MySQLdb
    conn=MySQLdb.connect(host="localhost",user="userdata",passwd="123456")
    cursor=conn.cursor()
    cursor.execute("SELECT VERSION()") # 資料庫版本
    print("資料庫版本: %s" % cursor.fetchone())


    1. 1
      digitgeek 17天前 支持  0 | 反對  0

      import MySQLdb
      import pandas as pd
      # 連結資料庫
      conn=MySQLdb.connect(host="localhost",user="userdata",passwd="123456",db="userdata",charset="utf8")
      # pandas 讀取SQL資料
      # 下的語法,都是SQL語法
      SQL="SELECT * FROM student_grades"
      # * 所有
      # 到student_grades資料表內所有資料
      # 利用Pandas抓取
      df = pd.read_sql(SQL,conn)
      #存成CSV
      df.to_csv('grades_data.csv',index=False,encoding='utf-8') # utf-8  big5
      conn.close() # 關閉資料庫連結
      print("資料儲存成功")


      1. 1
        digitgeek 17天前 支持  0 | 反對  0

        三、

        # 每位同學趨勢圖
        import pandas as pd
        import matplotlib.pyplot as plt
        plt.rcParams['font.sans-serif']=['Microsoft JhengHei']
        plt.rcParams['axes.unicode_minus']=False
        data=pd.read_csv("grades_data.csv")
        print(data.head())
        # 分組 student_id
        data_group=data.groupby('student_id')
        # 科目列表
        subjects=['chinese','english','math','physics','chemistry','biology','geography','history']
        #繪製每位同學的成績趨勢圖
        for student,exams in data_group:
            plt.figure(figsize=(10,6)) #畫布大小
            for i in subjects:
                exam_scores=exams.set_index('exam_type')[i]
                plt.plot(['第一次期中考','第二次期中考','期末考'],exam_scores,marker='o',label=i)
            plt.title(f"學生{student}成績趨勢")
            plt.xlabel("考試")
            plt.ylabel("成績")
            plt.legend()
            
            # 存成圖片 png svg
            plt.savefig(f'成績分布圖{student}.png',format='png')
            plt.savefig(f'成績分布圖{student}.svg',format='svg')
            # plt.show()


        1. 1
          digitgeek 17天前 支持  0 | 反對  0

          # 將每位學生的成績 三次考試製作成總成績1st 30% 2st30% 3st40%
          import MySQLdb
          import pandas as pd
          # 連結資料庫
          conn=MySQLdb.connect(host="localhost",user="userdata",passwd="123456",db="userdata",charset="utf8")
          # SQL 查詢 SELECT 
          SQL="""
          SELECT
              student_id,
              ROUND(SUM(CASE WHEN exam_type='第一次期中考' THEN chinese*0.3
                             WHEN exam_type='第二次期中考' THEN chinese*0.3
                             WHEN exam_type='期末考' THEN chinese*0.4 END),1) As chinese_total,
              ROUND(SUM(CASE WHEN exam_type='第一次期中考' THEN english*0.3
                             WHEN exam_type='第二次期中考' THEN english*0.3
                             WHEN exam_type='期末考' THEN english*0.4 END),1) As english_total,
              ROUND(SUM(CASE WHEN exam_type='第一次期中考' THEN math*0.3
                             WHEN exam_type='第二次期中考' THEN math*0.3
                             WHEN exam_type='期末考' THEN math*0.4 END),1) As math_total,
              ROUND(SUM(CASE WHEN exam_type='第一次期中考' THEN physics*0.3
                             WHEN exam_type='第二次期中考' THEN physics*0.3
                             WHEN exam_type='期末考' THEN physics*0.4 END),1) As physics_total,
              ROUND(SUM(CASE WHEN exam_type='第一次期中考' THEN chemistry*0.3
                             WHEN exam_type='第二次期中考' THEN chemistry*0.3
                             WHEN exam_type='期末考' THEN chemistry*0.4 END),1) As chemistry_total,
              ROUND(SUM(CASE WHEN exam_type='第一次期中考' THEN biology*0.3
                             WHEN exam_type='第二次期中考' THEN biology*0.3
                             WHEN exam_type='期末考' THEN biology*0.4 END),1) As biology_total,
              ROUND(SUM(CASE WHEN exam_type='第一次期中考' THEN geography*0.3
                             WHEN exam_type='第二次期中考' THEN geography*0.3
                             WHEN exam_type='期末考' THEN geography*0.4 END),1) As geography_total,
              ROUND(SUM(CASE WHEN exam_type='第一次期中考' THEN history*0.3
                             WHEN exam_type='第二次期中考' THEN history*0.3
                             WHEN exam_type='期末考' THEN history*0.4 END),1) As history_total
          FROM student_grades
          GROUP BY student_id;
          """
          df=pd.read_sql(SQL,conn)
          df.to_csv('學生總成績.csv',index=False,encoding='utf-8')
          conn.close() # 關閉資料庫連結
          print("資料儲存成功")




          登入後才可發表內容