|
|
|
จากโค้ดนี้ผมจะนำค่าจาก editeText มาตรวจสอบที่ sqlite ได้ยังไงครับ รบกวนด้วยครับ |
|
|
|
|
|
|
|
คือผมยังไม่เข้าใจหลักการ select จาก loginActivity ไปเช็คค่าใน sqlite นะครับ
ไม่ทราบว่าพอมีโค้ดให้ดูเป็นตัวอย่างไหมครับ
และผมยังติดปัญหาตรงการกำหนดเงื่อนไขการ login หลังจากเช็คค่าใน sqlite แล้วครับ
รบกวนช่วยแนะนำด้วยครับ
คือผมพึ่งศึกษาได้ไม่นาน ยังไงถ้ามีโค๊ดตัวอย่างจะเยี่ยมมากเลยครับ
ด้านล่างนี้คือโค๊ะในส่วนของ sqlite ครับ
Code (Android-Java)
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;
import java.util.HashMap;
public class myDBClass extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "mydatabase";
// Table Name
private static final String TABLE_MEMBER = "members";
public myDBClass(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
// Create Table Name
db.execSQL("CREATE TABLE " + TABLE_MEMBER +
"(MemberID INTEGER PRIMARY KEY AUTOINCREMENT," +
" Name TEXT(100)," +
" Tel TEXT(100));");
Log.d("CREATE TABLE", "Create Table Successfully.");
}
// Insert Data
public long InsertData(String strMemberID, String strName, String strTel) {
// TODO Auto-generated method stub
try {
SQLiteDatabase db;
db = this.getWritableDatabase(); // Write Data
/**
* for API 11 and above
SQLiteStatement insertCmd;
String strSQL = "INSERT INTO " + TABLE_MEMBER
+ "(MemberID,Name,Tel) VALUES (?,?,?)";
insertCmd = db.compileStatement(strSQL);
insertCmd.bindString(1, strMemberID);
insertCmd.bindString(2, strName);
insertCmd.bindString(3, strTel);
return insertCmd.executeInsert();
*/
ContentValues Val = new ContentValues();
Val.put("MemberID", strMemberID);
Val.put("Name", strName);
Val.put("Tel", strTel);
long rows = db.insert(TABLE_MEMBER, null, Val);
db.close();
return rows; // return rows inserted.
} catch (Exception e) {
return -1;
}
}
// Select Data
public String[] SelectData(String strMemberID) {
// TODO Auto-generated method stub
try {
String arrData[] = null;
SQLiteDatabase db;
db = this.getReadableDatabase(); // Read Data
Cursor cursor = db.query(TABLE_MEMBER, new String[] { "*" },
"MemberID=?",
new String[] { String.valueOf(strMemberID) }, null, null, null, null);
if(cursor != null)
{
if (cursor.moveToFirst()) {
arrData = new String[cursor.getColumnCount()];
/***
* 0 = MemberID
* 1 = Name
* 2 = Tel
*/
arrData[0] = cursor.getString(0);
arrData[1] = cursor.getString(1);
arrData[2] = cursor.getString(2);
}
}
cursor.close();
db.close();
return arrData;
} catch (Exception e) {
return null;
}
}
// Show All Data
public ArrayList<HashMap<String, String>> SelectAllData() {
// TODO Auto-generated method stub
try {
ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
SQLiteDatabase db;
db = this.getReadableDatabase(); // Read Data
String strSQL = "SELECT * FROM " + TABLE_MEMBER;
Cursor cursor = db.rawQuery(strSQL, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
map = new HashMap<String, String>();
map.put("MemberID", cursor.getString(0));
map.put("Name", cursor.getString(1));
map.put("Tel", cursor.getString(2));
MyArrList.add(map);
} while (cursor.moveToNext());
}
}
cursor.close();
db.close();
return MyArrList;
} catch (Exception e) {
return null;
}
}
// Update Data
public long UpdateData(String strMemberID,String strName,String strTel) {
// TODO Auto-generated method stub
try {
SQLiteDatabase db;
db = this.getWritableDatabase(); // Write Data
/**
* for API 11 and above
SQLiteStatement insertCmd;
String strSQL = "UPDATE " + TABLE_MEMBER
+ " SET Name = ? "
+ " , Tel = ? "
+ " WHERE MemberID = ? ";
insertCmd = db.compileStatement(strSQL);
insertCmd.bindString(1, strName);
insertCmd.bindString(2, strTel);
insertCmd.bindString(3, strMemberID);
return insertCmd.executeUpdateDelete();
*
*/
ContentValues Val = new ContentValues();
Val.put("Name", strName);
Val.put("Tel", strTel);
long rows = db.update(TABLE_MEMBER, Val, " MemberID = ?",
new String[] { String.valueOf(strMemberID) });
db.close();
return rows; // return rows updated.
} catch (Exception e) {
return -1;
}
}
// Delete Data
public long DeleteData(String strMemberID) {
// TODO Auto-generated method stub
try {
SQLiteDatabase db;
db = this.getWritableDatabase(); // Write Data
/**
* for API 11 and above
SQLiteStatement insertCmd;
String strSQL = "DELETE FROM " + TABLE_MEMBER
+ " WHERE MemberID = ? ";
insertCmd = db.compileStatement(strSQL);
insertCmd.bindString(1, strMemberID);
return insertCmd.executeUpdateDelete();
*
*/
long rows = db.delete(TABLE_MEMBER, "MemberID = ?",
new String[] { String.valueOf(strMemberID) });
db.close();
return rows; // return rows deleted.
} catch (Exception e) {
return -1;
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TABLE_MEMBER);
// Re Create on method onCreate
onCreate(db);
}
}
Tag : Mobile, Android
|
|
|
|
|
|
Date :
2016-05-05 09:18:11 |
By :
rattapongza |
View :
1032 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|