데이터를 데이터베이스에 삽입하는 응용 프로그램을 만들고 있습니다. 테이블이 생성되었지만 데이터베이스에 아무 것도 삽입되지 않아 오류가 발생하지 않습니다. 코드별로 한 줄씩 살펴 보려고했지만 잘못된 점은 발견 할 수 없습니다. 저장 버튼을 클릭하면 예기치 않게 닫힙니다. TreatmentRepo 클래스getter 및 setter를 사용하여 SQLite에서 데이터베이스에 데이터 삽입
public void insert(Treatment treatment) {
//Open connection to write data
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
String query = "select * from Treatment";
Cursor cursor = db.rawQuery(query, null);
int count = cursor.getCount();
values.put(Treatment.KEY_TreatmentID, count);
values.put(Treatment.KEY_TreatmentNumber, treatment.getEditTextTreatmentNumber());
values.put(Treatment.KEY_TreatmentType, treatment.getEditTextTreatmentType());
values.put(Treatment.KEY_TreatmentName, treatment.getEditTextTreatmentName());
db.insert(Treatment.TABLE, null, values);
db.close(); // Closing database connection
}
에서
삽입 방법이
public class Treatment {
// Labels table name
public static final String TABLE = "Treatment";
// Labels Table Columns names
public static final String KEY_TreatmentID = "TreatmentId";
public static final String KEY_TreatmentName = "TreatmentName";
public static final String KEY_TreatmentType = "TreatmentType";
public static final String KEY_TreatmentNumber = "TreatmentNumber";
// property help us to keep data
public int treatment_ID;
public String editTextTreatmentName;
public String editTextTreatmentType;
public int editTextTreatmentNumber;
public int getEditTextTreatmentNumber() {
return editTextTreatmentNumber;
}
public void setEditTextTreatmentNumber(int editTextTreatmentNumber) {
this.editTextTreatmentNumber = editTextTreatmentNumber;
}
public String getEditTextTreatmentName() {
return editTextTreatmentName;
}
public void setEditTextTreatmentName(String editTextTreatmentName) {
this.editTextTreatmentName = editTextTreatmentName;
}
public String getEditTextTreatmentType() {
return editTextTreatmentType;
}
public void setEditTextTreatmentType(String editTextTreatmentType) {
this.editTextTreatmentType = editTextTreatmentType;
}
}
XML 파일
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="368dp"
android:layout_height="495dp"
tools:context="com.instinctcoder.sqlitedb.StudentDetail$PlaceholderFragment"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Treatment Name"
android:id="@+id/treatmentName"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="30dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Treatment Type"
android:id="@+id/treatmentType"
android:layout_below="@+id/treatmentName"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="29dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Treatment Number"
android:id="@+id/treatmentNumber"
android:layout_below="@+id/treatmentType"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="29dp" />
<EditText
android:id="@+id/editTextTreatmentName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/treatmentType"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/treatmentName"
android:ems="10"
android:inputType="textShortMessage" />
<EditText
android:id="@+id/editTextTreatmentType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/treatmentNumber"
android:layout_alignEnd="@+id/editTextTreatmentName"
android:layout_alignRight="@+id/editTextTreatmentName"
android:layout_toRightOf="@+id/treatmentName"
android:ems="10"
android:inputType="textShortMessage" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/editTextTreatmentNumber"
android:layout_alignBottom="@+id/treatmentNumber"
android:layout_alignLeft="@+id/editTextTreatmentType"
android:layout_alignStart="@+id/editTextTreatmentType"
android:layout_alignRight="@+id/editTextTreatmentType"
android:layout_alignEnd="@+id/editTextTreatmentType" />
<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:onClick="onClick"
android:layout_toLeftOf="@+id/btnClose"
android:text="Save" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close"
android:id="@+id/btnClose"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete"
android:id="@+id/btnDelete"
android:layout_alignTop="@+id/btnSave"
android:layout_toLeftOf="@+id/btnSave" />
</RelativeLayout>
치료
내 온 클릭 방법
public void onClick(View v) {
if (v.getId() == R.id.btnSave) {
TreatmentRepo repo = new TreatmentRepo(this);
editTextTreatmentName = (EditText) findViewById(R.id.editTextTreatmentName);
editTextTreatmentType = (EditText) findViewById(R.id.editTextTreatmentType);
editTextTreatmentNumber = (EditText) findViewById(R.id.editTextTreatmentNumber);
btnSave = (Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(this);
int treatmentNumber = Integer.parseInt(editTextTreatmentNumber.getText().toString());
String treatmentType = editTextTreatmentType.getText().toString();
String treatmentName = editTextTreatmentName.getText().toString();
Treatment treatment = new Treatment();
treatment.setEditTextTreatmentNumber(treatmentNumber);
treatment.setEditTextTreatmentName(treatmentName);
treatment.setEditTextTreatmentType(treatmentType);
repo.insert(treatment);
Toast.makeText(this, "New Treatment Insert", Toast.LENGTH_SHORT).show();
startActivity(new Intent(this, view_treatment_entry.class));
}
}
입니다
logcat 오류를 –
에 추가하십시오. logcat에 오류가 없습니다. – Omade