我正在开发一个项目,它允许将SQLite数据库中的列存储到列表活动中,但我遇到了数据库的问题。具体来说,当我运行项目告诉我在我的数据库中没有这样的列时,我在android监视器中遇到致命异常。这是我设置数据库的代码:
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
public class myDB extends SQLiteOpenHelper{
private static final int DB_VERSION = 1;
private static final String TB_Student = "student";
public static final String ID = "_id";
public static final String NAME = "name";
public static final String STATUS = "status";
public static final String MAJOR = "major";
public myDB(Context contextr) {
super(contextr, NAME , null , DB_VERSION , null);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("Create Table " + TB_Student + " (" +
ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
NAME + " TEXT NOT NULL, " +
MAJOR + " TEXT NOT NULL, " +
STATUS + "TEXT NOT NULL" + ");");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
public Cursor getStudents() {
String[] cols = new String[] { ID, NAME, MAJOR, STATUS};
SQLiteDatabase db = getReadableDatabase();
return db.query(TB_Student, cols, null, null, null, null, NAME);
}
public Cursor getStudent(int studentId) {
String[] cols = new String[] { ID, NAME, MAJOR, STATUS};
String sel = ID + "=?";
String[] selArgs = new String[] { String.valueOf(studentId)};
SQLiteDatabase db = getReadableDatabase();
return db.query(TB_Student, cols, sel, selArgs, null, null, null);
}
public long numStudents() {
SQLiteDatabase db = getReadableDatabase();
SQLiteStatement st = db.compileStatement("SELECT COUNT(1) " + "FROM "+ TB_Student + ";");
return st.simpleQueryForLong();
}
public long addStudent(String name, String major, String status) {
ContentValues cv = new ContentValues();
cv.put(NAME, name);
cv.put(MAJOR, major);
cv.put(STATUS, status);
SQLiteDatabase db = getWritableDatabase();
return db.insert(TB_Student, null, cv);
}
public boolean delStudent(int studentId) {
String sel = ID + "=?";
String[] selArgs = new String[] { String.valueOf(studentId) };
SQLiteDatabase db = getReadableDatabase();
return (db.delete(TB_Student, sel, selArgs) > 0);
}
}
这是我的mainActivity代码:
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class MainActivity extends ListActivity {
myDB newmyDB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//newmyDB.addStudent("Mike DiLoreto", "CPE", "2018");
Button addStudent = (Button) findViewById(R.id.addStudent);
newmyDB = new myDB(this.getApplicationContext());
setListAdapter(new SimpleCursorAdapter(this,
R.layout.list_entry, // list_entry.xml
newmyDB.getStudents(),
new String [] { myDB.NAME, myDB.MAJOR, myDB.STATUS },
new int[] { R.id.textViewLeft, R.id.textViewMiddle, R.id.textViewRight }, // text view IDs here
0));
addStudent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivityForResult(intent, 1);
}
});
}
}
另外,这是监控日志: https://i.stack.imgur.com/E1iHe.png
我已经尝试在模拟器上删除应用并执行干净的构建,所以我不确定该怎么做。
确保空格正确。