Question

I have a save button that should basically create a new entry in my SQLite database IF there isn't already one with the same 'name and number'(both are stored as strings). If there is already an entry with the same name and number, it should make a popup asking if they want to overwrite.

What actually happens is it creates a new entry whether or not the entry already exists. Code that runs when you click save:

final String thename=TeamName.getText().toString()
final String thenum=TeamNum.getText().toString();
if ((thename.length()!=0) && (thenum.length()!=0)){
    ScoutingFormData info1 = new ScoutingFormData(this);
    info1.open();
    final long returnedId=info1.scanFor(thename,thenum);
    if (returnedId==-1){
        info1.createEntry(thename,thenum);
    }
    else
    {
        final Dialog overw=new Dialog(this);
        ....//The code that goes here is unimportant to my question so I'm hiding it.
        overw.show();
    }
    info1.close();  
}

Code in ScoutingFormData.java:

public long scanFor(String thename, String thenum) {
    String[] columns=new String[]{KEY_ROWID,KEY_NAME,KEY_NUM};
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
    int iID = c.getColumnIndex(KEY_ROWID);
    int iName = c.getColumnIndex(KEY_NAME);
    int iNum = c.getColumnIndex(KEY_NUM);
    for (c.moveToFirst(); !c.isAfterLast();c.moveToNext()){
        if ((c.getString(iName)==thename) && (c.getString(iNum)==thenum)){
            return Long.parseLong(c.getString(iID));
        }
    }
    return -1;
}

I don't see how it could possibly return -1 if an entry already exists with the same name+number.

Était-ce utile?

La solution

Use equals to compare String values.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top