1

データベースを更新して、データベース内の重複フィールドをチェックする方法を知りたいです。曜日と時間が別の行で同じである場合、エラーが発生する必要があります。

DBヘルパー:

package me.wouter.schoolwork;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class Schedule {

public static final String KEY_ROWID = "_id"; //Row Id 
public static final String KEY_HOUR = "schedule_hour"; 
public static final String KEY_DAY = "schedule_day";
public static final String KEY_LOCATION = "schedule_location";
public static final String KEY_SUBJECT = "schedule_subject";
public static final String KEY_START = "schedule_start";
public static final String KEY_END = "schedule_end"; 

private static final String DATABASE_NAME = "PlanYourDay";
private static final String DATABASE_TABLE = "schedule";
private static final int DATABASE_VERSION = 1;

private dBHelper classHelper;
private final Context classContext;
private SQLiteDatabase classDatabase;
public Cursor c;

private static class dBHelper extends SQLiteOpenHelper {

    public dBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_HOUR + " TEXT NOT NULL, " +
                KEY_DAY + " TEXT NOT NULL, " +
                KEY_LOCATION + " TEXT NOT NULL, " +
                KEY_SUBJECT + " TEXT NOT NULL, " +
                KEY_START + " TEXT NOT NULL, " +
                KEY_END + " TEXT NOT NULL);"

        );

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }

}

public Schedule(Context c){
    classContext = c;
}

public Schedule open() throws SQLException{
    classHelper = new dBHelper(classContext);
    classDatabase = classHelper.getWritableDatabase();
    return this;
}
public Schedule close(){
    classHelper.close();
    return this;
}

public long createEntry(String subject, String day, String hour,
        String location, String start, String end) {
    ContentValues cv = new ContentValues();
    cv.put(KEY_SUBJECT, subject);
    cv.put(KEY_DAY, day);
    cv.put(KEY_HOUR, hour);
    cv.put(KEY_LOCATION, location);
    cv.put(KEY_START, start);
    cv.put(KEY_END, end);
    return classDatabase.insert(DATABASE_TABLE, null, cv);

}

public String getData() {
    String[] columns = new String[]{KEY_ROWID, KEY_SUBJECT, KEY_HOUR, KEY_DAY, KEY_LOCATION, KEY_START, KEY_END};
    c = classDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
    String result = "";

    int iRow = c.getColumnIndex(KEY_ROWID);
    final int iSubject = c.getColumnIndex(KEY_SUBJECT);
    int iHour = c.getColumnIndex(KEY_HOUR);
    int iDay = c.getColumnIndex(KEY_DAY);
    int iLocation = c.getColumnIndex(KEY_LOCATION);
    int iStart = c.getColumnIndex(KEY_START);
    int iEnd = c.getColumnIndex(KEY_END);

    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        result = result + c.getString(iRow) + " " + c.getString(iSubject) + " " + c.getString(iHour) + " "
                + c.getString(iDay) + " " + c.getString(iLocation) + " " + c.getString(iStart) + " "
                + c.getString(iEnd) + "\n";
    }

    return result;
}

    }

DB インサーター:

package me.wouter.schoolwork;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

    public class ScheduleTest extends Activity implements OnClickListener {
// object variables
Button bLoadSql, bSaveSql;
TextView viewSubject, viewDay, viewHour, viewLocation, viewStart, viewEnd;
EditText editSubject, editDay, editHour, editLocation, editStart, editEnd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sql);
    allTheVariables();
    bLoadSql.setOnClickListener(this);
    bSaveSql.setOnClickListener(this);

}

private void allTheVariables() {
    // defines variables in xml file
            // buttons
            bLoadSql = (Button) findViewById(R.id.bLoadSql);
            bSaveSql = (Button) findViewById(R.id.bSaveSql);
            // textviews
            viewSubject = (TextView) findViewById(R.id.viewSubject);
            viewDay = (TextView) findViewById(R.id.viewDay);
            viewHour = (TextView) findViewById(R.id.viewHour);
            viewLocation = (TextView) findViewById(R.id.viewLocation);
            viewStart = (TextView) findViewById(R.id.viewStart);
            viewEnd = (TextView) findViewById(R.id.viewEnd);
            // edittext
            editSubject = (EditText) findViewById(R.id.editSubject);
            editDay = (EditText) findViewById(R.id.editDay);
            editHour = (EditText) findViewById(R.id.editHour);
            editLocation = (EditText) findViewById(R.id.editLocation);
            editStart = (EditText) findViewById(R.id.editStart);
            editEnd = (EditText) findViewById(R.id.editEnd);

}

@Override
public void onClick(View arg0) {
    switch (arg0.getId()) {
    case R.id.bLoadSql:
        Intent i = new Intent("me.wouter.schoolwork.SCHEDULEVIEW");
        startActivity(i);
        break;
    case R.id.bSaveSql:

        boolean didItWork = true;
        try{
        String subject = editSubject.getText().toString();
        String day = editDay.getText().toString();
        String hour = editHour.getText().toString();
        String location = editLocation.getText().toString();
        String start = editStart.getText().toString();
        String end = editEnd.getText().toString();

        Schedule entry = new Schedule(this);
        entry.open();
        entry.createEntry(subject, day, hour, location, start, end);
        entry.close();
        }catch (Exception e){
            didItWork = false;
            String error = e.toString();
            Dialog d = new Dialog(this);
            d.setTitle("Failed");
            TextView tv = new TextView(this);
            tv.setText(error);
            d.setContentView(tv);
            d.show();

        }finally{
            if(didItWork){
                Dialog d = new Dialog(this);
                d.setTitle("Inserted");
                TextView tv = new TextView(this);
                tv.setText("Succes");
                d.setContentView(tv);
                d.show();
            }
        }

        break;
    }

}

  }

ビューアー/ローダー:

package me.wouter.schoolwork;

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

public class ScheduleView extends Activity {

TextView subject;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sqlview);
    subject = (TextView) findViewById(R.id.subject);
    Schedule info = new Schedule(this);
    info.open();
    String data =  info.getData();
    subject.setText(data);
    info.close();
}


    }

前もって感謝します!

4

1 に答える 1

1

必要なのは、適切な競合句を使用して、DB スキーマに複数列の一意性制約を設定することです。

public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
            KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            KEY_HOUR + " TEXT NOT NULL, " +
            KEY_DAY + " TEXT NOT NULL, " +
            KEY_LOCATION + " TEXT NOT NULL, " +
            KEY_SUBJECT + " TEXT NOT NULL, " +
            KEY_START + " TEXT NOT NULL, " +
            KEY_END + " TEXT NOT NULL, " +
            "UNIQUE (" + KEY_DAY + ", " + KEY_HOUR + ") ON CONFLICT ROLLBACK);"
    );

これにより、明示的なチェックを記述しなくても、日/時間の組み合わせが一意でない場合に制約違反エラーが生成されます。

于 2013-01-01T20:03:59.017 に答える