วันอังคารที่ 30 มกราคม พ.ศ. 2561

w3

ทำ tutorial SQL ใน w3school
และ Import CSV to DBMS

What I have done & learn
  • ได้รู้คำสั่งของ SQL เบื้องต้น เช่น
    • select
    • distinct
    • where
    • and or not
    • insert into
    • update
  • ได้รู้คำสั่งเบื้องต้นในการนำข้อมูลจากไฟล์ csv เข้า dbms (MySQL)
    • ได้รู้ว่ามีปุ่ม Import จากไฟล์ ใน MySQL Workbench
    • ได้รู้คำสั่งการ
      • สร้าง table ( บรรทัดที่ 3-9 )
      • ลบ table ( บรรทัดที่ 1)
      •  import จาก ref2 ( บรรทัดที่ 12-17 )

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
DROP TABLE MyScore;

CREATE TABLE MyScore(
Term varchar(255),
Subject varchar(255),
Credit int,
Grade varchar(255),
ID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY (ID)
);

LOAD DATA LOCAL INFILE  'C:/Users/NTP/Desktop/database/_CSV SpreadSheet for DBMS.csv'
INTO TABLE MyScore
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
Problem & Decision
  • ไม่สามารถกด Import จากไฟล์ csv ได้
    • ไม่มี Primary Key ใน table จึงต้องเพิ่ม primary key

    วันอังคารที่ 23 มกราคม พ.ศ. 2561

    w2 : ลองใช้งาน DBMS


    ลองใช้งาน DBMS

    What I have done & learn

    DBMS คืออะไร
    DBMS คือ ระบบการจัดการฐานข้อมูล หรือซอฟต์แวร์ที่ดูแลจัดการเกี่ยวกับฐานข้อมูล โดยอำนวยความสะดวกให้แก่ผู้ใช้ทั้งในด้านการสร้าง การปรับปรุงแก้ไข


    ติดตั้ง MySQL Community
    สร้าง connection ชื่อว่า localhost
    ทำการโหลด Sample เข้ามาใน Database โดยการกด File > โหลดสคริป > เลือกไฟล์ที่โหลดจากเว็บ แล้วกดปุ่มรูปสายฟ้า (สีเหลือง)

    ใช้คำสั่ง SELECT โดยให้แสดงแค่ column (attribtues) ที่ต้องการคือ lastname ,firstname ,jobtitle จากตาราง employee


    ลองใช้คำสั่ง SELECT เพื่อเรียกข้อมูลของ employees มาแสดงทุก column (ใช้เครื่องหมาย * )



    การ Query แบบเรียงตาม lastname

    การ Query เรียงตาม lastname แบบไม่เอาตัวซ้ำ




    • ได้เรียนรู้การใช้คำสั่ง Query แบบเบื้องต้น เช่น SELECT
      • สามารถใช้ Distinct เพื่อหาอันที่ไม่ซ้ำ
      • สามารถเรียงลำดับโดยเพิ่มคำสั่ง ORDER BY ตามด้วย attributes 



    Problem & Decision

      • เมื่อเข้าโปรแกรม MySQLWorkBench เจอกับข้อความว่า There is no connection to the MySQL Server
        • เลือก startup/shutdown ที่แท็บ instance แล้วกด start server

      Reference
      1. http://www.mysqltutorial.org/basic-mysql-tutorial.aspx
      2. http://www.mindphp.com/

      วันเสาร์ที่ 20 มกราคม พ.ศ. 2561

      w2 : calGPA in Python

      CALGPA in Python


      What I have done & learn

      • ทำการ Export file CSV จาก Spreadsheet 
      • ได้หาวิธีการอ่านไฟล์ csv 
      • ทำการเขียนโค้ดเพื่ออ่านไฟล์ csv แล้วทำการคิดเกรด
      • 
        
      import csv
      
      def main():
          semester = []
          sem_index = -1
          grade , credit = 0,0
          grade_to_num = { 'A':4, 'B+':3.5 , 'B':3 , 'C+':2.5, 'C':2, 'D+':1.5, 'D':1, 'F':0}
          with open('csvgrade.csv', 'r') as f:
              reader = csv.reader(f)
              for row in reader:
                  if row[1] != '':
                      grade += int(row[1]) * grade_to_num[row[2]]
                      credit += int(row[1])
                      semester[sem_index] = round(grade/credit,2)
                  else:
                      semester.append(0)
                      grade = 0
                      credit = 0
                      sem_index += 1
          print("GPA > ",semester)
          print("GPAX > ",sum(semester)/len(semester))
      
      main()
      
      https://pastebin.com/9dTmrUPD


      ผลลัพท์จากการรันโค้ด




      Problem & Decision

      Reference
      1. http://www.pythonforbeginners.com/systems-programming/using-the-csv-module-in-python



      วันพุธที่ 10 มกราคม พ.ศ. 2561

      w1 : spreadsheet calculate GPA

      Enter your grades/transcript in a spreadsheet
      then calculate your GPA


      What I have done & learn

      • สร้าง Spreadsheet
      • ทำการสร้าง custom function ใน spreadsheet เพื่อนำข้อมูลเกรดไปคำนวณหา GPA
      •  1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        function calGPA(weight_column,grade_column){
          var grade_to_int = { 'A':4, 'B+':3.5 , 'B':3 , 'C+':2.5, 'C':2, 'D+':1.5, 'D':1, 'F':0}
          var gpa = 0
          var totalweight = 0
          for (var i = 0 ; i < weight_column.length ; i++){
            if (check_valid_cell(grade_column[i][0]) && check_valid_cell(weight_column[i][0])) {
              var grade = grade_to_int[grade_column[i][0]]
              var weight = weight_column[i][0]
              gpa += weight*grade
              totalweight += weight
            }
          }
          gpa = gpa/totalweight
          return gpa
        }
        
        function check_valid_cell(input){
          if (input == ""  || input == "เกรด" || input == "หน่วยกิต"){
              return false;
          } else { 
              return true; 
          }
        }
        
      • เรียกใช้ฟังก์ชั่นโดยพิมพ์ =round(calGPA(B3:B37,C3:C37),2) ใน Cell ใดๆ โดยที่ B3:B37 เป็น column ของ หน่วยกิต และ C3:C37 เป็น column ของ เกรด
        • ฟังก์ชั่น round เป็นการปัดเศษทศนิยมลงให้เหลือ 2 ตำแหน่ง
      • หากส่งค่าที่เป็น column มาในฟังก์ชั่น (เช่น C3 ถึง C37 ) จะถูกมองเป็น Array 2 มิติ เช่น a[x][y] โดยที่ x คือเลขแถว และ y คือ column


      Problem & Decision
      • จะเปลี่ยนเกรดที่เป็นตัวหนังสือ (A, B+, B,...) ให้เป็นตัวเลข (4, 3.5, 3,...)ยังไงดี
        • หาวิธีสร้าง custom function ใน spreadsheet ของ google (ref 1)
      • รับข้อมูลทั้ง column มาใช้ใน custom function ยังไง
        • หาวิธีนำข้อมูลมาใช้ (ref 5) พบว่าค่า colum (เช่น C3:C37 , B3:B37) ถูกส่งเข้ามาเป็น Array 2 มิติ
      • สร้าง Dictionary เพื่อแปลงเกรดที่เป็นตัวหนังสือ (A,B+,B,...) เป็นตัวเลขใน Javascript ยังไง
        • (ref 3)

      wk12 : form

      First form จาก table employee จะเห็นว่า มี Jon และ Lester มี 2 เบอร์ จึงไม่เป็น 1st form จากกฏคือ  “each attribute of a table must ha...