Click here to Skip to main content
15,886,647 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I intend to create a database and a table. Later, within an activity, on button click, I want to save the text received from screen into the table 'users'. I get the error like' unable to resolve' in the following line boolean.. where accountid and accountname are marked as red:

Java
public class listmasteritems extends Activity implements  OnClickListener{
public EditText editText;
SqliteHelper sqliteHelper;
//    SQLiteDatabase db;
    String AccountName;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listmasteritems);
        editText = (EditText)findViewById(R.id.idRelationShip);
        editText.setOnClickListener(this);
    sqliteHelper = new SqliteHelper(this);
    }

    public void onBtnIncludeClick( View v)
    {
        boolean result = sqliteHelper.saveUser(accountid,accountname);


SqlliteHelper:

public class SqliteHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "MyDatabase";
    private static final int DATABASE_VERSION = 1;
    public SqliteHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "CREATE TABLE users " +
                "(accountid INT PRIMARY KEY, accountname text)";
        db.execSQL(sql);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i2) {
        db.execSQL("DROP TABLE IF EXISTS users");
        onCreate(db);
    }

    public boolean saveUser (String accountid, String accountname)
    {
        Cursor cursor = getUser(accountname);
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("accountname", accountname);
        //contentValues.put("hobbies", hobbies);
        //contentValues.put("zodiac", zodiac);
        long result;
        if (cursor.getCount() == 0) { // Record does not exist
            contentValues.put("accountname", accountname);
            result = db.insert("users", null, contentValues);
        } else { // Record exists
            result = db.update("users", contentValues, "ac=?", new String[] { accountname });
        }

        if (result == -1) {
            return false;
        } else {
            return true;
        }
    }
Posted
Updated 4-Mar-15 21:34pm
v2
Comments
Pandu Rang 5-Mar-15 2:02am    
post the code always in readable manner with proper indentation .

1 solution

The following piece of your code is incomplete ...
Java
public void onBtnIncludeClick( View v)
{
    boolean result = sqliteHelper.saveUser(accountid,accountname);

and the variables accountid and accountname are not declared anywhere.

Also, when you post your message please don't say I get a message like .... Post the exact message and indicate in your code the exact point at which it occurs.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900