I am trying to do an app where you have an account and you have to login to use the app. So obviously you need to register for the app. The problem is when I enter the sign up form and enter all the details, the data doesn't go into the database. Any help?
Reference used: https://www.youtube.com/watch?v=KxlLsk5j3rY
This is the method to insert the contact into the database.
public void insertContact(Account a)
{
ContentValues values = new ContentValues();
String query = "select * from users";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
int count = cursor.getCount();
values.put(COLUMN_ID,count);
values.put(COLUMN_FirstName, a.getFirstname());
values.put(COLUMN_LastName, a.getLastname());
values.put(COLUMN_Username, a.getUsername());
values.put(COLUMN_Password, a.getPassword());
values.put(COLUMN_Email, a.getEmail());
values.put(COLUMN_ClubName, a.getEmail());
values.put(COLUMN_Address, a.getAddress());
db.insert(TABLE_NAME, null, values);
Log.e("Contact Entered", "DATABASE");
db.close();
}
The Register Button event
public void onRegisterClick(View v)
{
if(v.getId() == R.id.btnregister) {
EditText name = (EditText) findViewById(R.id.txtname);
EditText lastname = (EditText) findViewById(R.id.txtlastname);
EditText username = (EditText) findViewById(R.id.txtusername);
EditText password = (EditText) findViewById(R.id.txtpassword);
EditText email = (EditText) findViewById(R.id.txtemail);
EditText clubname = (EditText) findViewById(R.id.txtclubname);
EditText address = (EditText) findViewById(R.id.txtaddress);
//Text entered in the textview is assigned to a variable and to string
String namestr = name.getText().toString();
String lastnamestr = lastname.getText().toString();
String userstr = username.getText().toString();
String passstr = password.getText().toString();
String emailstr = email.getText().toString();
String clubnamestr = clubname.getText().toString();
String addresssrt = address.getText().toString();
Account a = new Account();
a.setFirstname(namestr);
a.setLastname(lastnamestr);
a.setUsername(userstr);
a.setPassword(passstr);
a.setEmail(emailstr);
a.setClubname(clubnamestr);
a.setAddress(addresssrt);
datab.insertContact(a);
Log.e("insertContact(a)", "DATABASE");
Toast temp = Toast.makeText(CreateAccount.this, "Account created!", Toast.LENGTH_SHORT);
temp.show();
Intent i = new Intent(CreateAccount.this, MainActivity.class);
startActivity(i);
}
}
You can simply use SuperDatabase to control your database with pure SQL. I've created a library to make things easier on Android. Import library to Gradle and use SuperDatabase.
Documentations here : https://github.com/sangeethnandakumar/TestTube
This worked for me. Adding BeginTransaction() and SetTransactionSuccessful helped.
public void insertContact(Account a)
{
ContentValues values = new ContentValues();
db.beginTransaction();
String query = "select * from users";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
int count = cursor.getCount();
values.put(COLUMN_ID,count);
values.put(COLUMN_FirstName, a.getFirstname());
values.put(COLUMN_LastName, a.getLastname());
values.put(COLUMN_Username, a.getUsername());
values.put(COLUMN_Password, a.getPassword());
values.put(COLUMN_Email, a.getEmail());
values.put(COLUMN_ClubName, a.getEmail());
values.put(COLUMN_Address, a.getAddress());
db.insert(TABLE_NAME, null, values);
Log.e("Contact Entered", "DATABASE");
db.setTransactionSuccessful();
db.endTransaction();
db.close();
}
Related
I'm extremely beginner to android development, I'm making a simple app that has a registration form, but after the fields filled with data and when the save button pressed the app closed immediately.
I've included the code because I don't know where's the faulty part:
public void AddNew(View v)
{
if(Image_path.isEmpty())
{
Toast.makeText(this,"please choose a profile picture",Toast.LENGTH_SHORT).show();
return;
}
EditText ed_full_name = findViewById(R.id.full_name);
EditText ed_mobie = findViewById(R.id.phone);
EditText ed_id_num = findViewById(R.id.id_num);
EditText ed_address = findViewById(R.id.address);
EditText ed_section_id = findViewById(R.id.straction_id);
EditText ed_card_id = findViewById(R.id.card_id);
EditText ed_dis_id = findViewById(R.id.dis_id);
String full_name = ed_full_name.getText().toString();
String mobile = ed_mobie.getText().toString();
String id_num = ed_id_num.getText().toString();
String address = ed_address.getText().toString();
String card_id = ed_card_id.getText().toString();
String section_id = ed_section_id.getText().toString();
String dis_id = ed_dis_id.getText().toString();
if(full_name.isEmpty() || mobile.isEmpty() || id_num.isEmpty() || address.isEmpty() || card_id.isEmpty() || section_id.isEmpty())
{
Toast.makeText(this,"Please fill all fields",Toast.LENGTH_SHORT).show();
return;
}
DBHelper mHelper = new DBHelper(this);
SQLiteDatabase db = mHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("full_name", full_name);
values.put("mobile", mobile);
values.put("id_num", id_num);
values.put("address", address);
values.put("section_id", section_id);
values.put("card_id", card_id);
values.put("image_path", Image_path);
values.put("dis_id", dis_id);
db.insertWithOnConflict("Clients",
null,
values,
SQLiteDatabase.CONFLICT_REPLACE);
db.close();
Toast.makeText(this, "Saved successfully! ", Toast.LENGTH_SHORT).show();
finish();
You are calling finish(); on your Activity, that will cause it to close. If it is the only Activity on your App's backstack, the App will also close.
can any one help me to send a code for add update delete in android studio
i have done update but not working properly
public boolean updateInfor(String username, String dob, String password, String gender){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues con = new ContentValues();
con.put(UserProfile.Users.DOB, dob);
con.put(UserProfile.Users.PASSWORD, password);
con.put(UserProfile.Users.GENDER, gender);
String where = UserProfile.Users.USERNAME + " = ?";
String[] wheree = {username};
long r = db.update(UserProfile.Users.TABLE_NAME, con, where, wheree);
if(r == 1)
return false;
else
return true;
}
public void editButton(View view){
DBHelper db = new DBHelper(this);
un = (EditText) findViewById(R.id.editText9);
dob = (EditText) findViewById(R.id.editText10);
pw = (EditText) findViewById(R.id.editText11);
int res = gender.getCheckedRadioButtonId();
String ress = String.valueOf(res);
boolean x = db.updateInfor(un.getText().toString(), dob.getText().toString(), pw.getText().toString(), ress);
if(x == false)
Toast.makeText(this, "Unsuccessfull", Toast.LENGTH_SHORT);
else
Toast.makeText(this, "Successfull", Toast.LENGTH_SHORT);
}
public void delete(View view){
DBHelper db = new DBHelper(this);
un = (EditText) findViewById(R.id.editText9);
db.deleteInfo(un.getText().toString());
}
public void ReadAll(View view){
DBHelper db = new DBHelper(this);
Cursor cursor = db.readAllInfor();
List <String> l1 = new ArrayList<>() ;
while(cursor.moveToNext()){
l1.add(cursor.getString(0));
l1.add(cursor.getString(1));
l1.add(cursor.getString(2));
l1.add(cursor.getString(3));
}
AlertDialog.Builder alert = new AlertDialog.Builder(this);
CharSequence[] ch = l1.toArray(new CharSequence[l1.size()]);
alert.setItems(ch, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
alert.show();
}
public void ReadOne(View view){
DBHelper db = new DBHelper(this);
un = (EditText) findViewById(R.id.editText9);
dob = (EditText) findViewById(R.id.editText10);
pw = (EditText) findViewById(R.id.editText11);
Cursor cursor = db.readAllInfor(un.getText().toString());
while(cursor.moveToNext()){
dob.setText(cursor.getString(1));
pw.setText(cursor.getString(2));
}
}
public void deleteInfo(String Username){
SQLiteDatabase db = this.getWritableDatabase();
String where = UserProfile.Users.USERNAME + "=?";
String[] whereeee = {Username};
db.delete(UserProfile.Users.TABLE_NAME,where,whereeee);
}
public Cursor readAllInfor(String username){
SQLiteDatabase db = this.getWritableDatabase();
String[] col = {UserProfile.Users.USERNAME, UserProfile.Users.DOB, UserProfile.Users.PASSWORD, UserProfile.Users.GENDER};
String where = UserProfile.Users.USERNAME + " = ?";
String[] wheree = {username};
Cursor cursor = db.query(UserProfile.Users.TABLE_NAME,
col,
where,
wheree,
null,
null,
null);
return cursor;
}
public boolean updateInfor(String username, String dob, String password, String gender){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues con = new ContentValues();
con.put(UserProfile.Users.DOB, dob);
con.put(UserProfile.Users.PASSWORD, password);
con.put(UserProfile.Users.GENDER, gender);
String where = UserProfile.Users.USERNAME + " = ?";
String[] wheree = {username};
long r = db.update(UserProfile.Users.TABLE_NAME, con, where, wheree);
if(r == 1)
return false;
else
return true;
}
public Cursor readAllInfor(){
SQLiteDatabase db = this.getReadableDatabase();
String[] col = {UserProfile.Users.USERNAME, UserProfile.Users.DOB, UserProfile.Users.PASSWORD, UserProfile.Users.GENDER};
Cursor cursor = db.query(UserProfile.Users.TABLE_NAME,col,null,null, null, null, null);
return cursor;
}
public boolean addInfo(String username, String dob, String password, String gender){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues con = new ContentValues();
con.put(UserProfile.Users.USERNAME, username);
con.put(UserProfile.Users.DOB, dob);
con.put(UserProfile.Users.PASSWORD, password);
con.put(UserProfile.Users.GENDER, gender);
long r = db.insert(UserProfile.Users.TABLE_NAME, null, con);
if(r == 1)
return false;
else
return true;
}
New to Android Studio so some help would go a long way!
Trying to do a search where the user inputs an email and if its in the database, then it shows up in a text view.
I think my select is correct but here's the method in my Database Handler class:
public String searchEmail(String email)
{
SQLiteDatabase db = this.getWritableDatabase();
String fName;
String sName;
String selectQuery = "SELECT FIRSTNAME, SURNAME FROM friends_table WHERE EMAIL=? ";
Cursor c = db.rawQuery(selectQuery, new String[] { email });
if (c.moveToFirst()) {
fName = c.getString(c.getColumnIndex("FIRSTNAME"));
sName = c.getString(c.getColumnIndex("SURNAME"));
}
c.close();
return email;
}
Then my search:
public void searchFriend(View v) {
EditText emailSearch = (EditText) findViewById(R.id.emailSearch);
String emailSearchString = emailSearch.getText().toString();
myDB.searchEmail(emailSearchString);
//I need to get the first name and surname from the db handler class
TextView firstName = (TextView)findViewById(R.id.firstname);
TextView surname = (TextView)findViewById(R.id.surname);
firstName.setText("");
surname.setText("");
}
I'm just finding to difficult to understand how I'll show the first name and surname if the email matches.
Thanks guys, I love you
Create a new class User (with getters, setters) and try this:
public User searchEmail(String email) {
SQLiteDatabase db = this.getWritableDatabase();
User u = null;
String selectQuery = "SELECT FIRSTNAME, SURNAME FROM friends_table WHERE EMAIL=? ";
Cursor c = db.rawQuery(selectQuery, new String[] { email });
if (c.moveToFirst()) {
u = new User();
u.setfName(c.getString(c.getColumnIndex("FIRSTNAME")));
u.setsName(c.getString(c.getColumnIndex("SURNAME")));
}
c.close();
return u;
}
Display results in activity:
public void searchFriend(View v) {
EditText emailSearch = (EditText) findViewById(R.id.emailSearch);
TextView firstName = (TextView)findViewById(R.id.firstname);
TextView surname = (TextView)findViewById(R.id.surname);
String emailSearchString = emailSearch.getText().toString();
User user = myDB.searchEmail(emailSearchString);
firstName.setText(user.getfName());
surname.setText(user.getsName());
}
Create a new class User and try this:
public User searchEmail(String email)
{
SQLiteDatabase db = this.getWritableDatabase();
User u = null;
String selectQuery = "SELECT FIRSTNAME, SURNAME FROM friends_table WHERE EMAIL=? ";
Cursor c = db.rawQuery(selectQuery, new String[] { email });
if (c.moveToFirst()) {
u = new User();
u.setfName(c.getString(c.getColumnIndex("FIRSTNAME")));
u.setsName(c.getString(c.getColumnIndex("SURNAME")));
}
c.close();
return u;
}
I've a database in android studio that has a table name EMP_TABLE with columns E_NAME, E_AGE and E_DEPT.
My fragment layout has two editText fields, a button and a TextView field. I want to perform a query on that database such that when I enter the E_NAME attribute in 1st edittext field, E_AGE attribute in 2nd edittext field and press the button the corresponding attribute of E_AGE field appear in textView field.
The query looks like SELECT E_AGE FROM EMP_TABLE WHERE E_NAME=a1 AND E_DEPT=a2. I'm not so familier with the cursor and the query, help me with this.
This is what I did so far in my OnClick method. Any help will be appreciated.
actTextView1 = (AutoCompleteTextView) view.findViewById(R.id.acTextView1);
actTextView2 = (AutoCompleteTextView) view.findViewById(R.id.acTextView2);
result = (TextView) view.findViewById(R.id.resultField);
calculateButton = (Button) view.findViewById(R.id.calBtn);
calculateButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
DatabaseHelper myDBhelper = new DatabaseHelper(getActivity());
String a1 = actTextView1.getText().toString();
String a2 = actTextView2.getText().toString();
c = myDBhelper.query("EMP_TABLE", null, null, null, null, null, null);
if (c.moveToFirst()) {
do {
Toast.makeText(getActivity(), "Age is: " + c.getString(0), Toast.LENGTH_LONG).show();
} while (c.moveToNext());
}
To insert:
public void insertIntoTable(String E_NAME_VALUE, String E_AGE_VALUE,String E_DEPT_VALUE) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(E_NAME, E_NAME_VALUE);
cv.put(E_AGE, E_AGE_VALUE);
cv.put(E_DEPT, E_DEPT_VALUE);
db.insert(EMP_TABLE , null, cv);
}
To Fetch stored value from database:
public String fetchValueFromTable(String E_NAME_VALUE,String E_DEPT_VALUE) {
String E_AGE_VALUE="" ;
SQLiteDatabase db = this.getWritableDatabase();
SELECT E_AGE FROM EMP_TABLE WHERE E_NAME=a1 AND E_DEPT=a2
String query = "SELECT * FROM " + EMP_TABLE + " WHERE " + E_NAME+ "='" + E_NAME_VALUE+ "' ANd "+ E_DEPT+ "='" + E_DEPT_VALUE;
Cursor cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()) {
E_AGE_VALUE=cursor.getString(cursor.getColumnIndex(E_AGE));
}
return E_AGE_VALUE;
}
I'm trying to copy some data in a table and insert into another table. I entered the data into an ArrayList, however, is only copied a given and not all those present. What am I missing?
private void Tras() {
String numero_ricevuto = (i.getStringExtra("numero"));
SQLiteDatabase db = mHelper.getWritableDatabase();
final ArrayList<Dettaglio_i> dettaglii = new ArrayList<Dettaglio_i>();
String sql = "SELECT data, unita_di_misura FROM prev WHERE numero = '"+numero_ricevuto+"'";
Cursor c = db.rawQuery(sql, null);
while (c.moveToNext()){
Dettaglio_i e = new Dettaglio_i();
e.data_ = c.getString(0);
e.unita_di_misura = c.getString(1);
dettaglii.add(e);
}
c.close();
ContentValues cv = new ContentValues();
Dettaglio_i e = new Dettaglio_i();
cv.put(VerTable.SI_NO, "0");
cv.put(VerTable .DATA, e.data_);
cv.put(VerTable .U_M, e.unita_di_misura);
boolean inserite = false;
if (!inserite){
long result = db.insert(VerTable .TABLE_NAME, null, cv);
if (result > 0){
inserite = true;
}
}
if (inserite){
}
db.close();
}
Try replacing your code with this:
private void Tras() {
String numero_ricevuto = (i.getStringExtra("numero"));
SQLiteDatabase db = mHelper.getWritableDatabase();
String insert = String.format("INSERT INTO %s (%s, %s, %s) ",
VerTable.TABLE_NAME, VerTable.SI_NO, VerTable.DATA, VerTable .U_M);
String select = "SELECT '0', data, unita_di_misura FROM prev WHERE " +
"numero='"+numero_ricevuto+"'";
db.rawQuery(insert+select, null);
db.close();
}