[Google Cloud] ทดสอบการจัดการฐานข้อมูลผ่าน App Script ตอนที่ 2

จาก blog ตอนที่แล้ว เราได้สร้าง Google Cloud Project เรียบร้อยแล้วครับ วันนี้เรามาทดสอบเขียน App Script กันครับ โดยทำตามขั้นตอน ดังนี้

📌 ตอนนี้เป็นการเขียน Code บน App Script นะครับ โดยเข้ามาที่ https://script.google.com

📌 มาจากนั้นสร้างไฟล์ใหม่เลย โดยเลือกเป็น Blank Project ครับ

📌 เขียน Code กำหนดตัวแปรที่จำเป็น และสร้าง function CreateDatabase โดย Code ของเราใช้ตัว jdbc driver ของ MySQL ในการเขื่อมต่อครับ ดังนี้

//IP ที่ได้จาก Google Cloud SQL
var address = '173.194.104.241';
//root username
var root = 'root';
//root password
var rootPwd = 'root_password';
//db username
var user = 'chatri';
//db password
var userPwd = 'debuggingsoft';
//ชื่อ DB ที่เราต้องการสร้าง
var db = 'guestbook';

var instanceUrl = 'jdbc:mysql://' + address;
var dbUrl = instanceUrl + '/' + db;

// Create a new database within a Cloud SQL instance.
function createDatabase() {
  var conn = Jdbc.getConnection(instanceUrl, user, userPwd);
  conn.createStatement().execute('CREATE DATABASE ' + db);
}

📌 เมื่อเราทดสอบ Run (กดปุ่ม Play แต่ถ้าจะ Debug ให้กดปุ่มรูป แมลง แทนครับ) และเลือก Fucntion createDatabase() ระบบมันแจ้งถามเรื่องสิทธิ ก็ใช้ Allow ไป ดังรูป

📌 กลับมาดูที่ DB ของเราก่อนนครับ ว่าไม่มี database ชื่อ Guest Book

📌 จัดการอะไรเรียบร้อยแล้ว สามารถ Run App Script ได้เลยครับ

📌 กลับมาดูที่ DB ของเราก่อนนครับ พบว่าตอนนี้มี database ชื่อ Guest Book แล้วครับ ดังรูป

📌 ส่วน function อื่นๆ เช่น การสร้าง User อีกคนเพิ่มจัดการ DB, การสร้าง Table / Insert ข้อมูล และก็การ Select ข้อมูลออกมา ผมขอแปะ Code ไว้ตรงนี้ครับ

  • Script การสร้าง User เพื่อจัดการฐานข้อมูล

//Create a new user for your database with full privileges.
function createUser() {
  var conn = Jdbc.getConnection(dbUrl, user, userPwd);
  var stmt = conn.prepareStatement('CREATE USER ? IDENTIFIED BY ?');

  stmt.setString(1, 'test');
  stmt.setString(2, 'test123');
  stmt.execute();
  conn.createStatement().execute('GRANT ALL ON `%`.* TO ' + user);
}
  • Script การสร้าง Table
//Create a new table in the database.
function createTable() {
  var conn = Jdbc.getConnection(dbUrl, user, userPwd);
  conn.createStatement().execute('CREATE TABLE entries '
                                 + '(guestName VARCHAR(255), content VARCHAR(255), '
                                 + 'entryID INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(entryID));');
}

  • Script การ Insert ข้อมูล

function insertData() {
  var conn = Jdbc.getConnection(dbUrl, user, userPwd);
  conn.createStatement().execute('INSERT INTO entries (guestName, content) values ("Chatri Nganbenchaong","DebuggingSoft");');
}
  • Script การ Select ข้อมูล และส่งผลลัพธ์มาที่เมล์ของเราครับ
function retriveData(){
  //สร้าง Connection
  var conn = Jdbc.getConnection(dbUrl, user, userPwd);
  //สร้าง Statement
  var stmt = conn.createStatement();
  //กำหนดจำนวน Row ที่ Select สูงสุด
  stmt.setMaxRows(1000);
  //รัน Query
  var rs = stmt.executeQuery('SELECT * FROM entries;');
  //วน Loop แสดงข้อมูล และเก็บลง Logger เนื่องจาก Cloud ไม่มี Console
  while (rs.next()) {
    var note = rs.getString("guestName") + ': ' + rs.getString("content");
    Logger.log(note);
  }
  //เคลียร์ Resource
  rs.close();
  stmt.close();

  //สร้าง Log และส่ง Email
  var recipient = Session.getActiveUser().getEmail();
  var subject = 'ข้อมูลที่ได้ Insert ลงไปใน Table entries';
  var body = Logger.getLog();
  MailApp.sendEmail(recipient, subject, body);
}

ท้ายสุดแล้วสำหรับคนที่สงสัยว่า ทำไมผมไม่ลองเข้าไป Query ข้อมูลใน Google Cloud SQL ตรงๆดูหละ ผมขอตอบสั้นง่ายๆว่า ณ ตอนนี้ (2015-05-11) เจ้าตัว Google Cloud SQL ยังไม่มีตัวจัดการครับ โดยบทความถัดไป ผมเขียนการใช้ MySQL WorkBench ในการเข้าไปจัดการ Google Cloud SQL ครับ


Discover more from naiwaen@DebuggingSoft

Subscribe to get the latest posts sent to your email.