|
|
|
Android ดึงข้อมูลมาโชว์ได้แล้ว แต่ไม่สามารถอัพเดทใหม่ลงไปได้ |
|
|
|
|
|
|
|
มันไม่ยอมแก้ให้ ตรงปุ่มอัพเดท
UpdateData
package com.project2556.project;
import java.util.ArrayList;
import com.projectDB.project.DBTime;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class UpdateData extends Activity {
private Spinner pntDuration;
private String dataDuration;
private String dataDuration1;
private String dataDuration2;
private String dataDuration3;
private String num1;
private String num2;
private String num3;
SQLiteDatabase mDb;
Cursor myData;
String sday, sDuration1, sDuration1Time, sDuration2, sDuration2Time, sDuration3, sDuration3Time;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.updatedata);
sday = getIntent().getExtras().getString("day");
sDuration1 = getIntent().getExtras().getString("Duration1");
sDuration1Time = getIntent().getExtras().getString("Duration1Time");
sDuration2 = getIntent().getExtras().getString("Duration2");
sDuration2Time = getIntent().getExtras().getString("Duration2Time");
sDuration3 = getIntent().getExtras().getString("Duration3");
sDuration3Time = getIntent().getExtras().getString("Duration3");
final DBTime myDb = new DBTime(this);
mDb = myDb.getWritableDatabase();
myData = mDb.rawQuery("SELECT * FROM " + DBTime.TABLE_data
+ " WHERE " + DBTime.day + "='" + sday + "'"
//+ " AND " + DBTime.Duration1 + "='" + sDuration1 + "'"
//+ " AND " + DBTime.Duration1Time + "='" + sDuration1Time + "'"
//+ " AND " + DBTime.Duration2 + "='" + sDuration2 + "'"
//+ " AND " + DBTime.Duration2Time + "='" + sDuration2Time + "'"
//+ " AND " + DBTime.Duration3 + "='" + sDuration3 + "'"
//+ " AND " + DBTime.Duration3Time + "='" + sDuration3Time + "'"
, null);
final EditText num = (EditText) findViewById(R.id.edtnum);
final EditText edtday = (EditText)findViewById(R.id.edtday);
edtday.setText(sday);
Duration();
DurationSpinner();
dataDuration = String.valueOf(pntDuration.getSelectedItem());
if (dataDuration == "เช้า"){
dataDuration1 = "1";
num1 = num.getText().toString();}
if (dataDuration == "เที่ยง"){
dataDuration2 = "2";
num2 = num.getText().toString();}
if (dataDuration == "เย็น"){
dataDuration3 = "3";
num3 = num.getText().toString();}
//อัพเดท
Button buttonUpdate = (Button)findViewById(R.id.btnupdate);
buttonUpdate.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String day2 = edtday.getText().toString();
if(num.length() != 0 ) {
mDb.execSQL("UPDATE "+ DBTime.TABLE_data
+ " SET " + DBTime.day + "='" + day2
+ "', " + DBTime.Duration1 + "='" + dataDuration1
+ "', " + DBTime.Duration1Time + "='" + num1
+ "', " + DBTime.Duration2 + "='" + dataDuration2
+ "', " + DBTime.Duration2Time + "='" + num2
+ "', " + DBTime.Duration3 + "='" + dataDuration3
+ "', " + DBTime.Duration3Time + "='" + num3
+ "' WHERE " + DBTime.day + "='" + sday + "'"
+ " AND " + DBTime.Duration1 + "='" + sDuration1 + "'"
+ " AND " + DBTime.Duration1Time + "='" + sDuration1Time + "'"
+ " AND " + DBTime.Duration2 + "='" + sDuration2 + "'"
+ " AND " + DBTime.Duration2Time + "='" + sDuration2Time + "'"
+ " AND " + DBTime.Duration3 + "='" + sDuration3 + "'"
+ " AND " + DBTime.Duration3Time + "='" + sDuration3Time + "';");
Toast.makeText(getApplicationContext()
,"เพิ่มเรียบร้อยแล้ว", Toast.LENGTH_SHORT).show();
Intent newActivity = new Intent(UpdateData.this, Showdata.class);
startActivity(newActivity);
} else {
Toast.makeText(getApplicationContext()
,"กรุณาใส่เวลา", Toast.LENGTH_SHORT).show();
}
}
});
CC();
}
private void CC() {
dataDuration1 = " ";
dataDuration2 = " ";
dataDuration3 = " ";
num1 = " ";
num2 = " ";
num3 = " ";
}
// เช้า/เที่ยง/เย็น เรียกใช้ ************
private void Duration() {
pntDuration = (Spinner) findViewById(R.id.spinner1);
}
private void DurationSpinner() {
ArrayList<String> strDuration = new ArrayList<String>();
strDuration.add("เช้า");
strDuration.add("เที่ยง");
strDuration.add("เย็น");
ArrayAdapter<String> varDuration = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, strDuration);
varDuration
.setDropDownViewResource(android.R.layout.simple_spinner_item);
pntDuration.setAdapter(varDuration);
}
}
DBTime
package com.projectDB.project;
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;
public class DBTime extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "mydatabase";
private static final String TABLE_Time = "Time";
public static final String TABLE_data = "data";
public static final String day = "day";
public static final String Duration1 = "Duration1";
public static final String Duration1Time = "Duration1Time";
public static final String Duration2 = "Duration2";
public static final String Duration2Time = "Duration2Time";
public static final String Duration3 = "Duration3";
public static final String Duration3Time = "Duration3Time";
public DBTime(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_Time +
"(ID INTEGER PRIMARY KEY AUTOINCREMENT," +
" Morning1 TEXT(100)," +
" Morning2 TEXT(100)," +
" Morning3 TEXT(100)," +
" Morning4 TEXT(100));");
db.execSQL("CREATE TABLE " + TABLE_data +
"( ID INTEGER PRIMARY KEY AUTOINCREMENT ," +
" day TEXT(100)," +
" Duration1 TEXT(100)," +
" Duration1Time TEXT(100)," +
" Duration2 TEXT(100)," +
" Duration2Time TEXT(100)," +
" Duration3 TEXT(100)," +
" Duration3Time TEXT(100));");
Log.d("CREATE TABLE","Create Table Successfully.");
}
// Select All Data
public Cursor SelectAllData() {
// TODO Auto-generated method stub
try {
SQLiteDatabase db;
db = this.getReadableDatabase(); // Read Data
String strSQL = "SELECT ID As _id , * FROM " +TABLE_data ;
Cursor cursor = db.rawQuery(strSQL, null);
return cursor;
} catch (Exception e) {
return null;
}
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//พิมพ์ Log เพื่อให้เห็นว่ามีการ Upgrade Database
Log.w(DBTime.class.getName(),
"Upgread database version from version" + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
//ลบตาราง member ของเก่าทิ้ง
db.execSQL("DROP TABLE IF EXISTS Time");
db.execSQL("DROP TABLE IF EXISTS data");
onCreate(db);
}
}
ขอบคุณค้าบ
Tag : Mobile, Android
|
|
|
|
|
|
Date :
2013-11-11 06:03:33 |
By :
bbza |
View :
1049 |
Reply :
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เพิ่มเติม
Code (Android-Java)
String sql = "UPDATE "+ DBTime.TABLE_data
+ " SET " + DBTime.day + "='" + day2
+ "', " + DBTime.Duration1 + "='" + dataDuration1
+ "', " + DBTime.Duration1Time + "='" + num1
+ "', " + DBTime.Duration2 + "='" + dataDuration2
+ "', " + DBTime.Duration2Time + "='" + num2
+ "', " + DBTime.Duration3 + "='" + dataDuration3
+ "', " + DBTime.Duration3Time + "='" + num3
+ "' WHERE " + DBTime.day + "='" + sday + "'"
+ " AND " + DBTime.Duration1 + "='" + sDuration1 + "'"
+ " AND " + DBTime.Duration1Time + "='" + sDuration1Time + "'"
+ " AND " + DBTime.Duration2 + "='" + sDuration2 + "'"
+ " AND " + DBTime.Duration2Time + "='" + sDuration2Time + "'"
+ " AND " + DBTime.Duration3 + "='" + sDuration3 + "'"
+ " AND " + DBTime.Duration3Time + "='" + sDuration3Time + "' ";
mDb.execSQL(sql);
ควรจะเขียนเป็นแบบนี้ครับ เวลาที่ Debug ค่ามันจะง่ายกกว่า
|
|
|
|
|
Date :
2013-11-11 06:18:19 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองแก้ดูคับติด2วันไปไหนไม่ได้
|
|
|
|
|
Date :
2013-11-11 06:22:59 |
By :
bbza |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
p' win มันไม่มี error แดงให้ผมแก้เลย อัพไม่ยอมลงฐาน
|
|
|
|
|
Date :
2013-11-11 06:40:36 |
By :
bbza |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ได้ Debug เอาค่า sql ออกมาดูหรือเปล่าครับ
|
|
|
|
|
Date :
2013-11-11 08:39:08 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ถ้ามีค่าว่างมันอัพเดทไม่ได้หรอคับ
|
|
|
|
|
Date :
2013-11-11 11:02:55 |
By :
bbza |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|