Direct Download Center

มาคุยกันก่อน


คลิกที่นี่ เข้ามารู้จักกันก่อน
Mr. P

Module Python

ไพธอน เชื่อมต่อดาต้าเบส MySQL

โพสต์27 ก.พ. 2553, 18:32โดยWisit P.   [ อัปเดต 11 ก.พ. 2554, 23:24 ]


    
หลายๆคนที่เอาไพธอน ไปทำเว็บพวก cgi อย่างหนึ่งที่จำเป็นก็คือ เว็บพวกนี้จะมีการติดต่อกับดาต้าเบส ที่นิยมมากก็คือ Mysql ฉะนั้นวันนี้ ผมจะมาแนะนำโมดูลตัวหนึ่ง ที่ทำหน้าที่ให้ไพธอนของเราสามารถติดต่อกับฐานข้อมูล Mysql ได้ครับ

    ก่อนการติดตั้งโมดูลนี้ คุณต้องมี MySQL แล้ว แต่ถ้ายังไม่มีแนะนำว่าให้ติดตั้งก่อน โดยสามารถติดตั้ง Appserv ซึ่งในตัว Appserv ของผมมี MySQL ติดตั้งมาพร้อมแล้ว เป็น MySQL เวอร์ชั่น 5.0.45 (สามารถดาวน์โหลดได้ที่นี่ และดูวิธีการติดตั้งที่นี่)

ทดลองพิมพ์ http://localhost/ ที่ address bar ของ web browser ของท่าน ควรจะได้ผลลัพธ์แบบนี้

  The AppServ Open Project - 2.5.9 for Windows


 phpMyAdmin Database Manager Version 2.10.2
 PHP Information Version 5.2.3 

About AppServ Version 2.5.9 for Windows
AppServ is a merging open source software installer package for Windows includes : 

  •  Apache Web Server Version 2.2.4
  • PHP Script Language Version 5.2.3
  • MySQL Database Version 5.0.45
  • phpMyAdmin Database Manager Version 2.10.2
  •     
        เมื่อติดตั้ง MySQL เรียบร้อยแล้ว ให้ลองทำการเช็คว่า MySQL service เราทำงานอยู่หรือเปล่า user/password ที่เราตั้งค่าในตอนลงโปรแกรมใช้ได้ไหม โดยทำการคลิกที่ 
             phpMyAdmin Database Manager Version 2.10.2

    หากเข้าไม่ได้ให้แก้ไขก่อน ซึ่งโดยทั่วไปจะทำได้ปกติ

     
     

        ขั้นตอนต่อไปให้ไปโหลดโมดูล MySQL-python โดยก่อนโหลดมา เราต้องเช็คก่อนว่า เราใช้ python เวอร์ชั่นไหนอยู่ แล้วทำการโหลดโมดูลนี้ให้ตรงกับไพธอนเวอร์ชั่นของเรา ในที่นี้ผมเลือก MySQL-python-1.2.2.win32-py2.4.exe  เมื่อติดตั้งสำเร็จแล้ว เราสามารถเข้าไปเช็คดูที่ C:\Python24\Lib\site-packages จะพบโฟลเดอร์ MySQLdb ซึ่งโฟล์เดอร์นี้เราจะ import เข้าไปในโค๊ดของเรา

        ต่อไปเป็นโค๊ดทดสอบว่า เราสามารถใช้ไพธอนติดต่อกับดาต้าเบส MySQL ของเราได้ไหม โดยเราจะลองทำการเขียนโค๊ดไพธอนเพื่อทำการติดต่อกับดาต้าเบส test (ซึ่งโดยปกติ มันจะมาพร้อมกับตอนที่เราติดตั้ง Appserv อยู่แล้ว)



    ==== โค๊ดไพธอน ======

    import MySQLdb

    # ติดต่อฐานข้อมูล
    conn =MySQLdb.Connect(host="localhost",user="root",
                      passwd="merlin",db="test")
    cur = conn.cursor()

    # ค้นหาข้อมูล และแสดง
    sql = "SHOW TABLES FROM test"
    cur.execute(sql)
    row = cur.fetchone()
    print row

    # ปิดการเชื่อมต่อ
    cur.close()
    conn.close()


    pySerial โมดูลสำหรับใช้ python ติดต่อกับ serial port

    โพสต์26 ก.พ. 2553, 8:58โดยWisit P.   [ อัปเดต 2 ก.พ. 2554, 5:35 ]


    Logo    โมดูล pySerial ทำหน้าที่เป็นตัวคอยเชื่อมโยงไพธอนให้สามารถส่งข้อมูลออกทางพอร์ตอนุกรม (serial port) ของคอมพิวเตอร์ได้ โดยใช้โมดูล pySerial นี้รันเป็นแบล๊กกราวด์ของโปรแกรม

    คุณสมบัติที่สำคัญของ pySerial
    • สามารถตั้งค่าพอร์ตอนุกรม (serial port setting) โดยผ่านการส่งค่าคุณสมบัติผ่านไพธอนได้เลย
    • รองรับข้อมูลที่ส่งออกผ่านพอร์ตอนุกรมได้หลากหลายขนาด 
    • สามารถทำงานได้ทั้งโหมดที่รอตอบกลับ แะลโหมดไม่ต้องรอตอบกลับ ได้
    • ไฟล์ มีลักษณะเป็นแบบ API ให้ผู้ใช้เรียกใช้งานได้อย่างง่ายดาย
    • ไฟล์ที่อยู่ภายใน package นั้นเป็นโค๊ดไพธอน 100% ทำให้เราสามารถทำความเข้าใจได้ไม่ยากเกินไป
    • สามารถเข้ากันได้กับ io library ของไพธอนได้ (ตั้งแต่ไพธอน เวอร์ชั่น 2.6 ขึ้นไป)
    ความต้องการของระบบ
    • ไพธอน เวอร์ชั่น 2.3 ขึ้นไป (แต่ในไพธอน 3.x นั้นมีโมดูลนี้รวมอยู่แล้ว)
    • ต้องมี ctypes extension ของไพธอนอยู่แล้ว (ในไพธอนเวอร์ชั่น 2.6 ขึ้นไป มีอยู่แล้ว)
    การติดตั้ง pySerial 
        หลังจากติดตั้งแล้ว เราจะต้อง import serial เข้ามาในโปรแกรมของเรา โดยการติดตั้งบนวินโดวน์ให้เราเลือกติดตั้งโดยให้เลือกใช้แบบทุก user 
    หากเป็นการติดตั้งบน linux ให้ติดตั้งโดย root user

    ดาวน์โหลด package pySerial โดยเลือกให้ตรงกับเวอร์ชั่นของ Python ของเรา

    ดูตัวอย่างโค๊ดอย่างง่าย ในการใช้โมดูล pySerial ติดต่อกับพอร์ตอนุกรม บนวินโดวน์  คลิก

     
     

    โมดูลไพธอน และแพคเกจไพธอน คือ

    โพสต์26 ม.ค. 2553, 5:47โดยWisit P.   [ อัปเดต 28 ก.พ. 2553, 8:59 ]

            โมดูลไพธอน คืออะไร จริงๆแล้วก็คือ ไฟล์ที่มีนามสกุล .py ที่เก็บรวบรวมฟังก์ชั่นหลายๆ ฟังก์ชั่น ที่มีความเกี่ยวข้องกันไว้ในไฟล์เดียวกัน นั่นก็คือ ไฟล์โมดูลนั่นเอง ซึ่งการที่เราเก็บฟังก์ชั่นที่เกี่ยวข้องกันไว้ในโมดูลเดียวกัน ทำให้โปรแกรมเมอร์สามารถที่จะเข้าใจ และไล่โค๊ดได้อย่างง่ายดาย เนื่องจากในโมดูลหนึ่งๆ จะมีความสามารถในการจัดการเรื่องใดเรื่องหนึ่ง อย่างเช่น โมดูล wave.py เป็นโมดูลที่สำหรับอ่านและเขียนไฟล์ .wave ซึ่งเป็นไฟล์เสียงของระบบปฏิบัติการวินโดวน์

           ฉะนั้นไฟล์ wave.py นั้น เมื่อเวลาเรานำไปใช้ในการเขียนโค๊ดของไพธอน เราจะทำการ import โมดูล wave เข้ามาร่วมด้วย พูดง่ายๆ ก็คือ เมื่อเราพบข้อความ import xxx หรือ from xxx import * แสดงว่าเค้ามีการเรียกโมดูล xxx เข้ามาใช้งานในโค๊ดไพธอนของเค้าด้วย ซึ่งเราก็สามารถที่เข้าำไปเปิดดูโมดูลที่เค้าเรียกเข้ามา ก็จะทำให้เราเข้าใจได้เร็วขึ้น




            ซึ่งเมื่อเราเข้าไป ดูในรายละเอียดของโมดูลนั้นๆ ก็จะมีคำอธิบายและวิธีการนำไปใช้งานของฟังก์ชั่น (หรือว่าเมทธอด) อธิบายไว้ที่ส่วนหัวของโมดูลไฟล์นั้นเสมอๆ

        แพคเกจไพธอน คืออะไร หากเรามองว่าโมดูล คือไฟล์นามสกุล .py แล้ว แพคเกจคือ โฟล์เดอร์ที่เก็บนามสกุล .py รวมไว้เป็นโฟลเดอร์เดียวกัน เพราะบางครั้ง โค๊ดมันเยอะเกินกว่าที่จะเก็บไว้ได้ภายในไฟล์เดียว ทางผู้พัฒนาก็จะเอาไฟล์โมดูลหลายๆไฟล์ รวมเข้าไว้ในโฟลเดอร์เดียวกัน จึงทำให้เกิดเป็นแพคเกจขึ้นมา เราสามารถเรียกใช้แพคเกจ ได้เหมือนกับที่เรียกใช้โมดูล และโดยปกติแล้ว แพคเกจจะถูกรวบรวมเก็บไว้ที่ C:\Python24\Lib\site-packages ตัวอย่างเช่น แพคเกจของ MySQLdb ซึ่งใช้ในการทำให้ python ติดต่อกับดาต้าเบส MySQL


    ตัวอย่างคำอธิบายในโมดูล wave

     """Stuff to parse WAVE files.

    Usage.

    Reading WAVE files:
          f = wave.open(file, 'r')
    where file is either the name of a file or an open file pointer.
    The open file pointer must have methods read(), seek(), and close().
    When the setpos() and rewind() methods are not used, the seek()
    method is not  necessary.

    This returns an instance of a class with the following public methods:
          getnchannels()  -- returns number of audio channels (1 for
                             mono, 2 for stereo)
          getsampwidth()  -- returns sample width in bytes
          getframerate()  -- returns sampling frequency
          getnframes()    -- returns number of audio frames
          getcomptype()   -- returns compression type ('NONE' for linear samples)
          getcompname()   -- returns human-readable version of
                             compression type ('not compressed' linear samples)
          getparams()     -- returns a tuple consisting of all of the
                             above in the above order
          getmarkers()    -- returns None (for compatibility with the
                             aifc module)
          getmark(id)     -- raises an error since the mark does not
                             exist (for compatibility with the aifc module)
          readframes(n)   -- returns at most n frames of audio
          rewind()        -- rewind to the beginning of the audio stream
          setpos(pos)     -- seek to the specified position
          tell()          -- return the current position
          close()         -- close the instance (make it unusable)
    The position returned by tell() and the position given to setpos()
    are compatible and have nothing to do with the actual position in the
    file.
    The close() method is called automatically when the class instance
    is destroyed.

    Writing WAVE files:
          f = wave.open(file, 'w')
    where file is either the name of a file or an open file pointer.
    The open file pointer must have methods write(), tell(), seek(), and
    close().

    This returns an instance of a class with the following public methods:
          setnchannels(n) -- set the number of channels
          setsampwidth(n) -- set the sample width
          setframerate(n) -- set the frame rate
          setnframes(n)   -- set the number of frames
          setcomptype(type, name)
                          -- set the compression type and the
                             human-readable compression type
          setparams(tuple)
                          -- set all parameters at once
          tell()          -- return current position in output file
          writeframesraw(data)
                          -- write audio frames without pathing up the
                             file header
          writeframes(data)
                          -- write audio frames and patch up the file header
          close()         -- patch up the file header and close the
                             output file
    You should set the parameters before the first writeframesraw or
    writeframes.  The total number of frames does not need to be set,
    but when it is set to the correct value, the header does not have to
    be patched up.
    It is best to first set all parameters, perhaps possibly the
    compression type, and then write audio frames using writeframesraw.
    When all frames have been written, either call writeframes('') or
    close() to patch up the sizes in the header.
    The close() method is called automatically when the class instance
    is destroyed.
    """



    1-3 of 3