【发布时间】:2018-10-08 12:22:41
【问题描述】:
我创建了 3 个类(1. MainActivity、DBhelper 和 Student 类)。我是初学者,使用简单的代码,并且一直无法将 DBhelper 类与 MainActivity 连接起来。我认为一切正常,但是在单击“buttonAdd”后,我收到“我的应用程序已停止”的声明。 在 MainActivity 中,我只使用了 DBhelper 中的“loadStudents”和“addStudent”方法,因为即使是这些基本级别我也有问题。
应用程序的想法 --> 将新学生添加到 editText,删除它们等。我知道,最好使用 scrollList,但我只想知道如何首先使用 SQLite。
学生课 --> 我在“DBhelper 类”和“MainActivity”中使用它
public class Student {
private int studentID;
private String studentName;
public Student(int id, String studentName) { //konstruktor
this.studentID = id;
this.studentName = studentName;
}
public int getStudentID() {
return studentID;
}
public void setStudentID(int studentID) {
this.studentID = studentID;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
}
DBhelper 类
public class DBhelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "studentDB.db";
public static final String TABLE_NAME = "student";
public static final String COLUMN_ID = "studentID";
public static final String COLUMN_NAME = "studentName";
//initialize the database
public DBhelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + "("
+ COLUMN_ID + "INTEGER PRIMARYKEY,"
+ COLUMN_NAME + " TEXT " + ")");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
public void addStudent(Student student) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_ID, student.getStudentID());
values.put(COLUMN_NAME, student.getStudentName());
db.insert(TABLE_NAME, null, values);
db.close();
}
public String loadStudents() {
String result = "";
String query = "Select*FROM " + TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
while (cursor.moveToNext()) {
int result_0 = cursor.getInt(0);
String result_1 = cursor.getString(1);
result += String.valueOf(result_0) + " " + result_1 +
System.getProperty("line.separator");
}
cursor.close();
db.close();
return result;
}
public Student findStudent(int ID, String studentName) {
String query = "Select * FROM " + TABLE_NAME + "WHERE" + COLUMN_NAME + "
= " + "'" + studentName + "'";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
Student student = new Student(ID, studentName);
if (cursor.moveToFirst()) {
cursor.moveToFirst();
student.setStudentID(Integer.parseInt(cursor.getString(0)));
student.setStudentName(cursor.getString(1));
cursor.close();
} else {
student = null;
}
db.close();
return student;
}
public boolean deleteStudent(int ID, String studentName) {
boolean result = false;
String query = "Select*FROM" + TABLE_NAME + "WHERE" + COLUMN_ID + "= '"
+ String.valueOf(ID) + "'";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
Student student = new Student(ID, studentName);
if (cursor.moveToFirst()) {
student.setStudentID(Integer.parseInt(cursor.getString(0)));
db.delete(TABLE_NAME, COLUMN_ID + "=?",
new String[] {
String.valueOf(student.getStudentID())
});
cursor.close();
result = true;
}
db.close();
return result;
}
public boolean updateStudent(int ID, String name) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues args = new ContentValues();
args.put(COLUMN_ID, ID);
args.put(COLUMN_NAME, name);
return db.update(TABLE_NAME, args, COLUMN_ID + "=" + ID, null) > 0;
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
Button buttonLoad;
Button buttonAdd;
Button buttonFind;
Button buttonDelete;
Button buttonUpdate;
TextView textViewStudents;
EditText editTextID;
EditText editTextName;
DBhelper myDB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonLoad = (Button) findViewById(R.id.buttonLoad);
buttonAdd = (Button) findViewById(R.id.buttonAdd);
buttonFind = (Button) findViewById(R.id.buttonFind);
buttonDelete = (Button) findViewById(R.id.buttonDelete);
buttonUpdate = (Button) findViewById(R.id.buttonUpdate);
textViewStudents = (TextView) findViewById(R.id.textViewStudents);
editTextID = (EditText) findViewById(R.id.editTextID);
editTextName = (EditText) findViewById(R.id.editTextName);
myDB = new DBhelper(this);
//load
buttonLoad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textViewStudents.setText(myDB.loadStudents());
editTextID.setText("");
editTextName.setText("");
}
});
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int id = Integer.parseInt(editTextID.getText().toString());
String name = editTextName.getText().toString();
Student student = new Student(id, name);
myDB.addStudent(student);
editTextID.setText("");
editTextName.setText("");
}
});
}
// Maybe it will be better to use the methos under the onCreate method but I
//don't know what should I write as a paramether instead of "view" when I
//will use this method in the onCreate
// public void addStudent() {
// DBhelper myDB = new DBhelper(this);
// int id = Integer.parseInt(editTextID.getText().toString());
// String name = editTextName.getText().toString();
// Student student = new Student(id, name);
// myDB.addStudent(student);
// editTextID.setText("");
// editTextName.setText("");
// }
}
【问题讨论】:
标签: java android database sqlite