I am trying to create and insert a row for a login registration.
This is the code i use for my main page
and my db class...
what i am trying to achieve...
what i would like to do is to register a user and password for security. I would also like those values passed to the insertrow() function. But when i try to click submit the app crashes.
This is the code i use for my main page
Code:
public class MainActivity extends AppCompatActivity {
//database
DBHelper helper = new DBHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//clicking button to send user/pass
public void onSubmit(View view){
//getting text values and storing it as variables.
EditText usertxt=(EditText)findViewById(R.id.ETuser);
EditText passtxt=(EditText)findViewById(R.id.ETpass);
EditText cpasstxt=(EditText)findViewById(R.id.ETcpass);
TextView out=(TextView)findViewById(R.id.TVoutput);
//storing them as string
String struser = usertxt.getText().toString();
String strpass = passtxt.getText().toString();
String strcpass = cpasstxt.getText().toString();
//Checking to see if username password or confirm password is blank.
if(TextUtils.isEmpty(struser) || TextUtils.isEmpty(strpass) || TextUtils.isEmpty(strcpass)){
Toast pass = makeText(MainActivity.this,"Please fill all fields.", LENGTH_SHORT);
pass.show();
}
//checks to see if the pass and confirm pass is the same. if not then display message saying it doesnt match.
if(!(strpass).equals(strcpass)){
Toast pass = makeText(MainActivity.this,"Does not match", LENGTH_SHORT);
pass.show();
}else{
helper.insertRow();
}
//setting text value of this text id.
out.setText(usertxt.getText().toString()+"/"+passtxt.getText().toString()+"/"+cpasstxt.getText().toString());
}
}
and my db class...
Code:
public class DBHelper extends SQLiteOpenHelper {
SQLiteDatabase db;
public DBHelper(Context context){
super(context, "Testing.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table account (id integer primary key not null auto_increment, user text, pass text, rank text)");
this.db = db;
}
public void insertRow(){
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
Cursor cursor = db.rawQuery("SELECT * FROM account", null);
int count = cursor.getCount();
values.put("id", count+1);
values.put("user", "derek");
values.put("pass", "testpass");
db.insert("account", null, values);
db.close();
}
public String searchPass(String user){
db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT user, pass FROM account",null);
String a, b;
b = "Not Found";
if(cursor.moveToFirst()){
do{
a = cursor.getString(0);
if(a.equals(user)){
b = cursor.getString(1);
break;
}
}
while(cursor.moveToNext());
}
return b;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS account");
this.onCreate(db);
}
}
what i am trying to achieve...
what i would like to do is to register a user and password for security. I would also like those values passed to the insertrow() function. But when i try to click submit the app crashes.