简体   繁体   中英

SQLite database not updating

I have a activity in which I want to create a database and enter data into it if no database exists previously. Otherwise, I want to update the value which had been entered before.

I'm trying to do this by implementing 'if-else'.

Here is the code for selecting between the two possibilities:

public void submitusername(View view) {
    userdatabase name = new userdatabase(this);
    name.open();
    String user = name.getusername();
    name.close();

    EditText cinone = (EditText) findViewById(R.id.username);
    username = cinone.getText().toString();

    if(user == null){
        userdatabase entry = new userdatabase(Editprofile.this);
        entry.open();
        entry.createEntry(username, null, null, null);
        entry.close();

        Context context = getApplicationContext();
        CharSequence text = "Added new user!";
        int duration = Toast.LENGTH_LONG;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();

    } else {
        userdatabase update = new userdatabase(Editprofile.this);
        update.open();
        update.updateUsername(username);
        update.close();

        Context context = getApplicationContext();
        CharSequence text = "Username updated!";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    }
}

Here is the method which calls the 'username' from the database:

public String getusername() {                                                               
    String[] columns = new String[]{USER_NAME};
    Cursor c = userDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);

    String result = "";

    int iName = c.getColumnIndex(USER_NAME);

    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        result = result + c.getString(iName);
    }

    return result;
}

Method for updating the username:

public void updateUsername(String name) {                                                       
    ContentValues cvUpdate = new ContentValues();
    cvUpdate.put(USER_NAME, name);

    userDatabase.update(DATABASE_TABLE, cvUpdate, KEY_ROWID + "=" + 0 , null);
}

The problem I encounter is that When I run the program and the database does not exist (no username), on clicking the button I still get the toast "Username updated" and no data is inserted. When it clearly should create the database, insert the username for the first time and show the toast "Added new user!"

Also, if I remove the 'if-else' condition, not check for existing data and just keep the "createEntry" part everything works fine; the data gets inserted correctly. So, it means that my "createEntry" part is correct.

What could be the problem? I have implemented getusername() in other activities and it works fine.

try after changing your code as:

   userdatabase name = new userdatabase(Editprofile.this);
    name.open();
    String user = name.getusername();
    name.close();

    EditText cinone = (EditText) findViewById(R.id.username);
            username = cinone.getText().toString();

  if(user.equals("")){

        //your code here
    }

    else {
          //your code here
    }

and change your getusername() method as:

public String getusername() {                                                               
    String[] columns = new String[]{USER_NAME};
    Cursor c = userDatabase.query(DATABASE_TABLE, 
                                         columns,
                     null,
                     null,
                     null,
                     null,
                     null);

    String result = "";
    c.moveToFirst();
    if(c.getCount()>0){

      int iName = c.getColumnIndex(USER_NAME);

      for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {

        result = result + c.getString(iName);
       }
     }
    return result;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM