varsha valanju varsha valanju - 1 month ago 9
Android Question

working with custom views and sqlite database-android

I m working on password manager app.
in which I have created database with one table.

table_name
:
entry


fileds
:
id, title, category, username, password, website, comment
.

And the main activity contains views for above fields.
but now I want to add a button called
"add filed"
through which user can add new view like one more username , or edittext for any other custom entry.

But the problem is...

How can I save those custom entries into database? Because, there may be a possibility that user want to add one custom entry for first password. and two custom entries for second password. so how can I manage those extra custom records into database.

plz help..

Answer

Use Alter Table query to add new field in table. For ex.,

 if (!isColumnExistInTableOrNot("entry", "username2" )) {
            db.execSQL("ALTER TABLE entry ADD COLUMN username2 varchar");

        }

the isColumnExistInTableOrNot() is.

 public static boolean isColumnExistInTableOrNot(SQLiteDatabase db,String tableName,
                                                    String ColumnName) {
        String Query = "select " + ColumnName + " from " + tableName;
        try {
            Cursor c = db.rawQuery(Query, null);
            if (c != null) {
                return true;
            }
        } catch (SQLiteException e) {
            return false;
        }
        return false;
    }
Comments