Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,025

HOME > Windows Azure > Windows Azure (Mobile Services) and Android > ตอนที่ 6 : Android สร้างตาราง Table บน Mobile Services และการ Insert ข้อมูล



Clound SSD Virtual Server

ตอนที่ 6 : Android สร้างตาราง Table บน Mobile Services และการ Insert ข้อมูล

ตอนที่ 6 : Android สร้างตาราง Table บน Mobile Services และการจัดเก็บ Insert ข้อมูล บทความนี้ขอต่อจากตอนที่แล้ว ซึ่งจะเป็นการสร้าง Table ไว้สำหรับจัดเก็บข้อมูลบน Mobile Services การเขียน Android App เพื่อสร้าง Column หรือฟิวด์ พร้อม ๆ กับการส่งข้อมูลจาก Android แล้วนำไปจัดเก็บ Insert ไว้ใน Table ของ Mobile Services บน Windows Azure

Android Azure Mobile Services Table Insert

ตอนนี้เรามี Mobile Services อยู่ 1 ตัว

Android Azure Mobile Services Table Insert

ในหน้า Get Started จะแสดงรายละเอียดของการเชื่อมต่อ และได้อธิบายไว้ในบทความก่อน ๆ หน้านี้แล้ว

ขั้นตอนการสร้าง Table หรือตาราง

Android Azure Mobile Services Table Insert

ตลิกที่ DATA และเลือก CREATE

Android Azure Mobile Services Table Insert

ใส่ชื่อตารางในที่นี้จะใส่เป็น MyMember








Android Azure Mobile Services Table Insert

ได้ตารางขึ้นมา 1 รายการชื่อว่า MyMember

Android Azure Mobile Services Table Insert

ให้คลิกเข้าไปใน Table (ตาราง) ซึ่งตอนนี้ยังไม่มี Column และ Rows

เพิ่มเติม
จะสังเกตุว่าไม่มีเครื่องมือสำหรับการสร้าง Table และการ Insert ข้อมูล (มีแต่ลบ Column และบน Rows ข้อมูล) แต่ทั้งนี้เราสามารถที่จะสร้าง Column และ Insert ข้อมูลได้จากการเขียน App บน Android

Android Azure Mobile Services Table Insert

เมนู SCRIPT เป็นพวก Script ที่ไว้ทำหน้าที่รับข้อมูลจาก Android แล้ว Insert ลงใน Table การทำงานคล้าย ๆ กับ Stored Procedure ซึ่งเราสามารถเขียน Script เพิ่มเติมได้ แต่ตอนนี้แนะนำให้กำหนดเป็นค่า Default ซะก่อน

Android Azure Mobile Services Table Insert

หลัก ๆ จะมีอยู่ 4 ตัวคือ Insert , Update , Delete , Read

Android Azure Mobile Services Table Insert

กลับมาบน Project ของ Android ตอนนี้เรายังคงมีแค่ไฟล์ MainActivity.java และ activity_main.xml

ขั้นตอนการสร้าง Class สำหรับจัดการกับ Table และการสร้าง Column ฟิวด์

Android Azure Mobile Services Table Insert

ให้สร้าง Class ขึ้นมา 1 ตัวโดยไปที่ คลิกขวา New -> Class

Android Azure Mobile Services Table Insert

การตั้งชื่อไฟล์ของ Class จะต้องชื่อเดียวกับ Table เท่านั้น

Android Azure Mobile Services Table Insert

เราได้ไฟล์ MyMember.java ไว้สำหรับการจัดการกับตาราง MyMember เรียบร้อยแล้ว

Android Azure Mobile Service Table Insert

ตอนนี้ในไฟล์ MyMember.java ยังไม่มีคำสั่งใด ๆ นอกจากค่า Default เราจะมาเริ่มสร้าง Column ให้กับตาราง

Android Azure Mobile Services Table Insert

MyMember.java
package com.example.thaicreate;

public class MyMember {
	
	@com.google.gson.annotations.SerializedName("id")
	private int mId;
	
	@com.google.gson.annotations.SerializedName("name")
	private String mName;
	
	@com.google.gson.annotations.SerializedName("email")
	private String mEmail;	
	
	public MyMember() {
		// empty
	}

	public MyMember(String name, String email) {
		this.setName(name);
		this.setEmail(email);
	}
	
	public final void setName(String name) {
		mName = name;
	}
	
	public final void setEmail(String email) {
		mEmail = email;
	}
	
}


ตัวอย่างการสร้าง Column ฟิวด์ที่ประกอบด้วย id,name และ email โดยที่ฟิวด์ id เป็น Column ฟิวด์บังคับที่จะต้องมีทุกครั้ง








เพิ่มเติม
1. ฟิวด์แรกจะต้องชื่อว่า id เป็นชนิดแบบ int
2. การสร้าง Column จะเรียกใช้งานทุก ๆ ครั้งที่ App ทำงาน โดยที่โปรแกรมจะตรวจสอบก่อนว่ามี Column แล้วหรือยัง ถ้ามีแล้วจะไม่สร้างเพิ่ม
3. ในกรณีที่มี Column ใหม่ สามารถมาสร้างเพิ่มได้ทันที เพราะเมื่อโปรแกรมทำงานใหม่อีกครั้ง Column ใหม่ก็จะถูกสร้างขึ้นมาใหม่

การ Insert ข้อมูลลงใน Table ตัวอย่างการ Insert ข้อมูลไปยัง Mobile Services

MainActivity.java
        /*** Create to Mobile Services ***/
        try {
			mClient = new MobileServiceClient(
				      "https://thaicreate.azure-mobile.net/",
				      "QUjngFknhHZjdaGgYAAzdoXkOzKoxi24",
				      this);
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        /*** Create getTable ***/
        mMyMember = mClient.getTable(MyMember.class);
        
        	/*** Item for Insert ***/
        	MyMember item = new MyMember();
    		item.setName("Win");
    		item.setEmail("[email protected]");
    		
    		// Insert the new item
    		mMyMember.insert(item, new TableOperationCallback<MyMember>() {

    			public void onCompleted(MyMember entity, Exception exception, ServiceFilterResponse response) {
    				
    				if (exception == null) {
    					// Suscess
    				} else {
    					// Failed
    				}

    			}
    		});


จากตัวอย่างจะเป็นการ Insert ข้อมูลจาก Android ไปยัง Table ชื่อว่า MyMember ของ Mobile Services ที่อยู่บน Windows Azure

Code เต็ม ๆ ทั้งหมด

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/lblText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>


MyMember.java
package com.example.thaicreate;

public class MyMember {
	
	@com.google.gson.annotations.SerializedName("id")
	private int mId;
	
	@com.google.gson.annotations.SerializedName("name")
	private String mName;
	
	@com.google.gson.annotations.SerializedName("email")
	private String mEmail;	
	
	public MyMember() {
		// empty
	}
	
	public MyMember(String name, String email) {
		this.setName(name);
		this.setEmail(email);
	}
	
	public final void setName(String name) {
		mName = name;
	}
	
	public final void setEmail(String email) {
		mEmail = email;
	}
	
}


MainActivity.java
package com.example.thaicreate;

import java.net.MalformedURLException;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

import com.microsoft.windowsazure.mobileservices.MobileServiceClient;
import com.microsoft.windowsazure.mobileservices.MobileServiceTable;
import com.microsoft.windowsazure.mobileservices.ServiceFilterResponse;
import com.microsoft.windowsazure.mobileservices.TableOperationCallback;

public class MainActivity extends Activity {
	
	private MobileServiceClient mClient;
	private MobileServiceTable<MyMember> mMyMember;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        /*** Create to Mobile Services ***/
        try {
			mClient = new MobileServiceClient(
				      "https://thaicreate.azure-mobile.net/",
				      "QUjngFknhHZjdaGgYAAzdoXkOzKoxi24",
				      this);
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        /*** Create getTable ***/
        mMyMember = mClient.getTable(MyMember.class);
        
        final TextView lbText = (TextView) findViewById(R.id.lblText);
		
        if (mClient != null) {
        	
        	/*** Item for Insert ***/
        	MyMember item = new MyMember();
    		item.setName("Win");
    		item.setEmail("[email protected]");
    		
    		// Insert the new item
    		mMyMember.insert(item, new TableOperationCallback<MyMember>() {

    			public void onCompleted(MyMember entity, Exception exception, ServiceFilterResponse response) {
    				
    				if (exception == null) {
    					lbText.setText("Insert Data Successfully.");
    				} else {
    					lbText.setText("Insert Data Failed! Error = " + exception.getCause().getMessage());
    				}

    			}
    		});
    		
        }
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}


Screenshot

Android Azure Mobile Services Table Insert

แสดงผลบนหน้าจอ App ของ Android

Android Azure Mobile Services Table Insert

เมื่อกลับไปดูบน Portal Management ของ Mobile Services จะเห็นว่า Column ถูกสร้าง และ Rows ถูก Insert

บทความถัดไปที่แนะนำให้อ่าน




บทความที่เกี่ยวข้อง


   
Share

Property & Method (Others Related)

How to read data in Mobile Services - Android (Windows Azure)
How to insert data to Mobile Services - Android (Windows Azure)
How to update data to Mobile Services - Android (Windows Azure)
How to delete data in Mobile Services - Android (Windows Azure)

ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2013-05-08 16:10:17 / 2017-03-24 11:13:27
  Download : Download  ตอนที่ 6 : Android สร้างตาราง Table บน Mobile Services และการ Insert ข้อมูล
 Sponsored Links / Related

 
ตอนที่ 1 : รู้จัก Android Mobile Services บน Windows Azure คืออะไร
Rating :

 
ตอนที่ 2 : เตรียมความพร้อมของ Android ก่อนที่จะเขียน Android กับ Mobile Services
Rating :

 
ตอนที่ 3 : การสร้าง Android Mobile Services และการเรียกใช้งานแบบง่าย ๆ
Rating :

 
ตอนที่ 4 : การเชื่อมต่อ Android กับ Mobile Services บน Windows Azure
Rating :

 
ตอนที่ 5 : สร้าง Project บน Eclipse การเชื่อมต่อไปยัง Mobile Services ด้วย Android
Rating :

 
ตอนที่ 7 : Android อ่าน Data จาก Table ของ Mobile Services และแสดงผลบน App
Rating :

 
ตอนที่ 8 : อ่านข้อมูล Mobile Services แบบมีเงื่อนไข Where และแสดงผลบน Android
Rating :

 
ตอนที่ 9 : การทำ Authentication in Azure Mobile Services ด้วย Android
Rating :

 
ตอนที่ 10 : การทำ Push Notifications in Mobile Services ด้วย Android
Rating :

 
ตอนที่ 11 : การทำ Validate และ Modify data in Mobile Services บน Andriod
Rating :

 
ตอนที่ 12 : การทำ Refine Mobile Services queries with paging บน Android
Rating :

 
ตอนที่ 13 : การเขียน Scripts to authorize users in Mobile Services บน Android
Rating :

 
ตอนที่ 14 : Show Case 1 : Register Form (Android and Mobile Services)
Rating :

 
ตอนที่ 15 : Show Case 2 : Login User Password (Android and Mobile Services)
Rating :

 
ตอนที่ 16 : Show Case 3 : Update Data (Android and Mobile Services)
Rating :

 
ตอนที่ 17 : Show Case 4 : Delete Data (Android and Mobile Services)
Rating :

 
ตอนที่ 18 : บทความอื่น ๆ เกี่ยวกับ Android Mobile Services บน Windows Azure
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 00
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่