Site icon บริษัท โค๊ดบี จำกัด

สอน SQLite ใช้งานฐานข้อมูลแบบออฟไลน์กับ Unity C#

สอน-SQLite

สอน-SQLite

<h3>สอน SQLite ใช้งานฐานข้อมูลแบบออฟไลน์กับ Unity C&num;<&sol;h3>&NewLine;<p><&sol;br><br &sol;>&NewLine;SQLite เป็นระบบฐานข้อมูลที่ทำงานในลักษณะออฟไลน์หรือเป็นฐานข้อมูลที่ถูกเก็บไว้ในเครื่อง client นิยมนำไปใช้สำหรับพัฒนาโมบายแอพพลิเคชั่น เราจะสังเกตุเห็นว่าเวลาที่เราเล่นเกมส์มือถือจะสามารถเล่นได้ทั้งแบบออนไลน์และออฟไลน์ ข้อมูลต่าง ๆ จะถูกเก็บแบบออฟไลน์เมื่อเราเล่นเกมส์แบบไม่เชื่อมต่ออินเตอร์เนต และจะถูกอัพเดทไปที่ฐานข้อมูลบน Server เมื่อเราเชื่อมต่ออินเตอร์เนต<br &sol;>&NewLine;<&excl;--more--><&sol;p>&NewLine;<p><&sol;br><&sol;p>&NewLine;<h4>สร้าง Unity2D โปรเจค และฐานข้อมูล SQLite<&sol;h4>&NewLine;<ul>&NewLine;<li>1&period; สร้างโปรเจค Unity และเลือกไปที่โปรเจคแบบ 2D ตั้งชื่อว่า SQL&lowbar;lite<&sol;li>&NewLine;<li>2&period; เปิดโปรแกรม Terminal ไปที่โฟลเด้อโปรเจค SQL&lowbar;lite&sol;Assets และพิมพ์คำสั่ง เพื่อสร้างฐานข้อมูลชื่อว่า test ดังนี้<&sol;li>&NewLine;<pre class&equals;"theme&colon;terminal lang&colon;default decode&colon;true " >SQLite3 test&period;db<&sol;pre>&NewLine;<li>3&period; ฐานข้อมูลจะยังไม่ถูกสร้าง จนกว่าจะมี table มากกว่า 1 ในฐานข้อมูล ฉะนั้นให้เราพิมพ์คำสั่งเพื่อสร้างตารางดังนี้&NewLine;<pre class&equals;"theme&colon;terminal lang&colon;default decode&colon;true " >CREATE TABLE COMPANY&lpar; &NewLine; ID INT PRIMARY KEY NOT NULL&comma; &NewLine; NAME TEXT NOT NULL &NewLine;&rpar;&semi;<&sol;pre>&NewLine;<p>จะเห็นว่ามีไฟล์ฐานข้อมูลชื่อว่า test&period;db ถูกสร้างขึ้นมาแล้ว อยู่ใน SQL&lowbar;lite&sol;Assets&sol;test&period;db<br &sol;>&NewLine;ใช้คำสั่ง insert เพื่อเพิ่มข้อมูลเข้าไปในตาราง COMPANY เขียนคำสั่งดังนี้<&sol;p>&NewLine;<pre class&equals;"theme&colon;terminal lang&colon;default decode&colon;true " >INSERT INTO COMPANY &lpar;ID&comma;NAME&rpar; &NewLine;VALUES &lpar;1&comma; 'Witoon Pomipon'&rpar;&semi;<&sol;pre>&NewLine;<p><&sol;br><br &sol;>&NewLine;<strong>ตัวอย่างคำสั่งในโปรแกรม Terminal<&sol;strong><br &sol;>&NewLine;<&sol;br><br &sol;>&NewLine;<img src&equals;"https&colon;&sol;&sol;www&period;codebee&period;co&period;th&sol;labs&sol;wp-content&sol;uploads&sol;2016&sol;10&sol;สอน-SQLite-5&period;png" alt&equals;"สอน-SQLite" &sol;>&NewLine;<&sol;li>&NewLine;<p><&sol;br><&sol;p>&NewLine;<&sol;ul>&NewLine;<p><&sol;br><&sol;p>&NewLine;<h4>เขียน C&num; ติดต่อกับฐานข้อมูล SQLite<&sol;h4>&NewLine;<ul>&NewLine;<li>1&period; ดาวน์โหลดไฟล์ต่าง เพื่อให้สามารถใช้งาน SQLite ได้ที่ <a href&equals;"http&colon;&sol;&sol;www&period;sqlite&period;org&sol;download&period;html" target&equals;"&lowbar;blank">http&colon;&sol;&sol;www&period;sqlite&period;org&sol;download&period;html<&sol;a> วางไฟล์ทั้งหมดไว้ในโฟลเด้อ Assets&sol;Plugins<br &sol;>&NewLine;<&sol;br><br &sol;>&NewLine;<img src&equals;"https&colon;&sol;&sol;www&period;codebee&period;co&period;th&sol;labs&sol;wp-content&sol;uploads&sol;2016&sol;10&sol;สอน-SQLite-4&period;png" alt&equals;"สอน-SQLite" &sol;><br &sol;>&NewLine;<&sol;br>&NewLine;<&sol;li>&NewLine;<p><&sol;br><&sol;p>&NewLine;<li>2&period; เปิดโปรแกรม Unity3D และโปรเจค SQL&lowbar;lite จากนั้นคลิกขวาที่หน้าต่าง Assets เลือกไปที่ Create -> C&num; Script และตั้งชื่อว่า AppController&period;cs<br &sol;>&NewLine;<br &sol;>&NewLine;<img src&equals;"https&colon;&sol;&sol;www&period;codebee&period;co&period;th&sol;labs&sol;wp-content&sol;uploads&sol;2016&sol;10&sol;สอน-SQLite-3&period;png" alt&equals;"สอน-SQLite" &sol;><&sol;li>&NewLine;<p><&sol;br><&sol;p>&NewLine;<li>3&period; ดับเบิ้ลคลิกที่ไฟล์ AppController&period;cs ระบบจะเปิดโปรกรม MonoDevelop ขึ้นมาให้ทดลองเขียนโปรแกรมเพื่อติดต่อกับฐานข้อมูล test&period;db ดังนี้<br &sol;>&NewLine;<&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " >using UnityEngine&semi; &NewLine;using System&period;Collections&semi; &NewLine;using System&period;Data&semi; &NewLine;using Mono&period;Data&period;SqliteClient&semi; &NewLine; &NewLine;public class AppController &colon; MonoBehaviour &lbrace; &NewLine; &NewLine; &NewLine;&Tab;void Start &lpar;&rpar; &lbrace; &NewLine;&Tab;&Tab;connect&lpar;&rpar;&semi; &NewLine;&Tab;&rcub; &NewLine; &NewLine;&Tab;void connect&lpar;&rpar;&lbrace; &NewLine;&Tab;&Tab;string conn &equals; "URI&equals;file&colon;test&period;db"&semi; &sol;&sol; ชี้ไปที่ฐานข้อมูล &NewLine;&Tab;&Tab;IDbConnection dbconn &equals; new SqliteConnection&lpar;conn&rpar;&semi; &NewLine;&Tab;&Tab;IDbCommand dbcmd &equals; dbconn&period;CreateCommand&lpar;&rpar;&semi; &NewLine;&Tab;&Tab;dbconn&period;Open&lpar;&rpar;&semi; &sol;&sol; เปิดการเชื่อมต่อ &NewLine; &NewLine; &NewLine;&Tab;&Tab;string sqlQuery &equals; "SELECT name FROM COMPANY"&semi; &sol;&sol; เลือก name จากตาราง COMPANY &NewLine;&Tab;&Tab;dbcmd&period;CommandText &equals; sqlQuery&semi; &NewLine;&Tab;&Tab;IDataReader reader &equals; dbcmd&period;ExecuteReader&lpar;&rpar;&semi; &sol;&sol; เริ่ม Execute &NewLine;&Tab;&Tab;while &lpar;reader&period;Read&lpar;&rpar;&rpar; &NewLine;&Tab;&Tab;&lbrace; &NewLine;&Tab;&Tab;&Tab;string name &equals; reader&period;GetString&lpar;0&rpar;&semi; &NewLine;&Tab;&Tab;&Tab;Debug&period;Log&lpar;"name &equals;"&plus;name&rpar;&semi; &sol;&sol; แสดง name ใน console &NewLine;&Tab;&Tab;&rcub; &NewLine; &NewLine;&Tab;&Tab;reader&period;Close&lpar;&rpar;&semi; &NewLine;&Tab;&Tab;reader &equals; null&semi; &NewLine;&Tab;&Tab;dbcmd&period;Dispose&lpar;&rpar;&semi; &NewLine;&Tab;&Tab;dbcmd &equals; null&semi; &NewLine;&Tab;&Tab;dbconn&period;Close&lpar;&rpar;&semi; &NewLine;&Tab;&Tab;dbconn &equals; null&semi; &NewLine;&Tab;&rcub; &NewLine;&rcub;<&sol;pre>&NewLine;<p><&sol;br><&sol;p>&NewLine;<li>4&period; จะเห็นว่าเราได้ข้อมูล name จากตาราง COMPANY ที่เรา insert ไว้ก่อนหน้านี้แสดงออกมาทางหน้าต่าง console<br &sol;>&NewLine;<&sol;br><br &sol;>&NewLine;<img src&equals;"https&colon;&sol;&sol;www&period;codebee&period;co&period;th&sol;labs&sol;wp-content&sol;uploads&sol;2016&sol;10&sol;สอน-SQLite-6&period;png" alt&equals;"สอน-SQLite" &sol;><br &sol;>&NewLine;<&sol;br>&NewLine;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><&sol;br><&sol;p>&NewLine;

Exit mobile version