db.insert 函数仅在表中插入一个自动递增的 ID,尽管我从活动编辑文本中获取值,但没有其他任何内容。表格行的其余单元格保持为空
这是我的代码:
public class SHelper extends SQLiteOpenHelper {
static final String dbName = "School";
static final String studentTable = "Student";
static final String colName = "Name";
static final String colPassword = "Password";
public static final int dbVersion=1;
public SHelper(Context context)
{
super(context, dbName, null, dbVersion);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "+studentTable+"("+colID+" INTEGER PRIMARY KEY, "+colName+" TEXT,"+colPassword+" TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + studentTable);
onCreate(db);
}
public void add(StudentData studentdata)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(colName, StudentData.getName());
values.put(colPassword, StudentData.getPassword());
db.insert(studentTable, null, values);
db.close();
}
}
public class StudentData {
private static String name;
private static String password;
public StudentData() {
super();
}
public StudentData(String name,String password) {
super();
StudentData.name = name;
StudentData.password = password;
}
public static String getName() {
return name;
}
public void setName(String name) {
StudentData.name = name;
}
public static String getPassword() {
return password;
}
public void setPassword(String password) {
StudentData.password = password;
}
}
public class stoActivity extends Activity {
SHelper db = new SHelper(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sto);
final EditText fullname=(EditText)findViewById(R.id.fullName);
final EditText password=(EditText)findViewById(R.id.password);
final String fullname=fullname.getText().toString();
final String password=password.getText().toString();
Button registerbutton = (Button) findViewById(R.id.registerButton);
registerbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
db.add(new StudentData(fullname,password));
Intent it=new Intent(stoActivity.this,otherActivity.class);
stoActivity.this.startActivity(it);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_register, menu);
return true;
}
}