一个简单的注册页面,其中包含用户名,密码和确认密码这三个文本字段。我将创建一个数据库,其中包含三个属性id,name和password的表,其中id是主键,我们从用户名textfield动态获取的名称和我们从密码textfield动态获得的密码。但问题是数据库已创建但动态给出的值不会在表格中存储/更新。我是android的新手,我已经使用互联网资源编写代码,但我仍然不知道在这个阶段。在此先感谢.. !!
MainActivity.java :
public class MainActivity extends Activity
{
EditText editTextUserName,editTextPassword,editTextConfirmPassword;
Button btnCreateAccount;
DbHelper myHelper;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextUserName=(EditText)findViewById(R.id.editTextUserName);
editTextPassword=(EditText)findViewById(R.id.editTextPassword);
editTextConfirmPassword=(EditText)findViewById(R.id.editTextConfirmPassword);
myHelper = new DbHelper(this, "", null, 1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void newName(View v) {
// onClick function when account create button is clicked
String userName=editTextUserName.getText().toString();
String password=editTextPassword.getText().toString();
Bus bus = new Bus(userName, password);
myHelper.insertEntry(bus);
String confirmPassword=editTextConfirmPassword.getText().toString();
bus = new Bus(userName, password);
myHelper.insertEntry(bus);
editTextUserName.setText("");
editTextPassword.setText("");
editTextConfirmPassword.setText("");
Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();
}
}
public void count (View view) {
//onClick function when no of tuples in the table button is clicked
int a = myHelper.getNamesCount();
String b = Integer.toString(a);
editTextUserName.setText(b);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
myHelper.close();
}
}
还有另一个名为DbHelper.java的数据库助手类,代码为:
DbHelper.java :
public class DbHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME = "finalProjectDB.db";
private static final String TABLE_LOGIN = "Table1";
public static final String COLUMN_ID1 = "id";
public static final String COLUMN_NAME1 = "name";
public static final String COLUMN_PASSWORD1 = "password";
public DbHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
public SQLiteDatabase db;
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_NAMES_TABLE = "CREATE TABLE "+TABLE_LOGIN+"("+COLUMN_ID1+" INTEGER PRIMARY KEY,"+COLUMN_NAME1+" TEXT,"+COLUMN_PASSWORD1+" TEXT)";
db.execSQL(CREATE_NAMES_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_LOGIN);
onCreate(db);
}
public void insertEntry(Bus bus)
{
ContentValues newValues = new ContentValues();
newValues.put("COLUMN_NAME1",bus.getName());
newValues.put("COLUMN_PASSWORD1",bus.getPassword());
SQLiteDatabase db = this.getWritableDatabase();
db.insert("TABLE_LOGIN", null, newValues);
///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
db.close();
}
public int getNamesCount(){
String countQuery = "SELECT * FROM "+TABLE_LOGIN;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int count = cursor.getCount();
return count;
}
}
最后,Bus.java文件:
Bus.java :
public class Bus {
private int _id;
private String _name;
private String _password;
public Bus(){
}
public Bus(int id, String name, String password){
this._id = id;
this._name = name;
this._password = password;
}
public Bus(String name, String password){
this._name = name;
this._password = password;
}
public void setID(int id){
this._id = id;
}
public int getID(){
return this._id;
}
public void setName(String name) {
this._name = name;
}
public String getName() {
return this._name;
}
public void setPassword(String password) {
this._password = password;
}
public String getPassword() {
return this._password;
}
}
答案 0 :(得分:1)
您的代码中似乎有错误:
db.insert("TABLE_LOGIN", null, newValues);
因此,您尝试在不存在的表中插入值。
使用常量而不是字符串。