Elsa Elsa - 7 months ago 19
Android Question

How to get the data from the database created by another activity/ Can insert the data but fails to update data into database (Android studio,SQLite)

Thanks to Midhun MP, the first problem is solved! The second one is difficult for me, too. I have search for a good example that can show me how exactly the data will be passed between activities and the steps but failed...

Hello,guys. I'm a newb and my android-studio program is driving me crazy.

It's kind like a login program, I have 2 activities: The first one is for users to enter their password, I think it needs to get what user has entered and compare with the data in the database; The second one is for users to change password, I think this means update the data in the database

I've used sqlite to build a database for the code.(right now there is only one column for just one code but further there may be different users)

Here is my question:

1. According to my emulator, I can insert data into database, but fail to update it. Which is weird because when I click the update button, it tells me the code has been changed.

2. I need my first activity to get the code (which is saved in the database created by the second activity), and compare with what user has entered. But I don't know how to do this...

My database code:

public class DataCode extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Code4.dp";
public static final String TABLE_NAME = "Code_table";
public static final String COL_1 = "CODE";//a une seule ligne

public DataCode(Context context) {
super(context, DATABASE_NAME, null, 1);

public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + " (CODE TEXT )");//create the table

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

//insert data
public boolean insertData(String CODE) {
SQLiteDatabase db = this.getWritableDatabase();//insert to database
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1, CODE);

long result = db.insert(TABLE_NAME, null, contentValues);
if (result == -1)
return false;
return true;

//show data
public Cursor getCode(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);//store table in cursor
return res;

//modifier code
public boolean updateCode(String CODE) {
SQLiteDatabase db = this.getWritableDatabase();//create database
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1, CODE);

db.update(TABLE_NAME, contentValues, "code = ?", new String[]{ CODE });
return true;

and my second activity's code (it fails to update the data):

public class ModifierC extends AppCompatActivity {
DataCode myDbC;//creat the database

EditText editCode;
Button Modifier;

protected void onCreate(Bundle savedInstanceState) {

myDbC = new DataCode(this);//my database for code

editCode = (EditText)findViewById(R.id.password2);
Modifier = (Button)findViewById(R.id.buttonMP);
buttonMP();//modifier le code


public void buttonMP(){//button to change the password

new View.OnClickListener() {

public void onClick(View v) {

EditText txtpass2 = (EditText) findViewById(R.id.password2);

String pass2str = txtpass2.getText().toString();

Cursor res = myDbC.getCode();
if (res.getCount() == 0) {//if there is no data in the database, use the insert method
boolean isInserted = myDbC.insertData(editCode.getText().toString());
if (isInserted == true)
Toast.makeText(ModifierC.this, "Code enregistré", Toast.LENGTH_LONG).show();
Toast.makeText(ModifierC.this, "Pas encore de code", Toast.LENGTH_LONG).show();

else {//if there is already a code, use the update method
boolean isUpdated = myDbC.updateCode(editCode.getText().toString());//recuperer le code de password2
if (isUpdated == true)
Toast.makeText(ModifierC.this, "Code a changé", Toast.LENGTH_LONG).show();
Toast.makeText(ModifierC.this, "Error...", Toast.LENGTH_LONG).show();



And for the first activity, I use Cursor to pass the data

public class ParCode extends AppCompatActivity {
DataCode myC;

protected void onCreate(Bundle savedInstanceState) {

public void buttonOpen(View view) {
EditText txtpass1 = (EditText) findViewById(R.id.password); //get what user has entered
String pass1str = txtpass1.getText().toString();

Cursor coderes = myC.getCode();// search code in the database
String pass2str = coderes.getString(0);

BUT it won't work, I think I may miss something.

Sorry this maybe a too long and very stupid question. However I have spend over one week on this, so I think I should ask for help. It's my first time to build an android app, and all my question is because of lacking of basic knowledge. But I really have to prepare for my test and have no time to stuck on this right now T T...So please give me some advice!!!



The issue is happening because of the following line:

db.update(TABLE_NAME, contentValues, "code = ?", new String[]{ CODE });

Suppose your db consists of a value "Elsa", and now you are trying to update it to "Hello", the above code in sql query looks something similar to:

UPDATE Code_table SET CODE = 'Hello' WHERE code = 'Hello'

The above where clause will be false and the row won't be updated.

You need to use:

db.update(TABLE_NAME, contentValues, null, null);

The third and fourth parameters represents the WhereClause and WhereArgs. In your case you don't need to specify those (Because you don't have any unique or primary key fields defined).

Now the query will look like:

UPDATE Code_table SET CODE = 'Hello'