Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9039165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:40:01+00:00 2026-06-16T09:40:01+00:00

I have a activity in which I want to create a database and enter

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T09:40:03+00:00Added an answer on June 16, 2026 at 9:40 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an activity which shows an image (ViewCollection.java). I want to only create
I have a phonegap activity which I want to start via a button press:
I have an Activity which uses a Fragment . I simply want to pass
So I have this class which extends an activity. But I want to draw
my question is simple. I have class TouchSurfaceView which extends GLSurfaceView. I want create
I have an sqlite database in which I have a content I want to
in my app in the first activity i have created a database which stores
I have an activity which contains a Button,Whenever user click on button, we create
I would like to create an activity which displays a country map. I want
I have an activity which have ListView. When I want to access this ListView

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.