0

hey guys i want to insert my username and password permanently in sqlite so when i install it in other phone it will be there and i can login with the inserted username and password on my db. how to do this?

public class DatabaseHelper extends SQLiteOpenHelper{

public DatabaseHelper(Context context, String name, CursorFactory factory,
        int version) {
    super(context, name, factory, version);
    // TODO Auto-generated constructor stub
}
SQLiteDatabase db;
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "contacts.db";
private static final String TABLE_NAME = "contacts";
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_EMAIL = "email";
private static final String COLUMN_UNAME = "uname";
private static final String COLUMN_PASS = "pass";

private static final String COLUMN_BIRTH = "birth";
private static final String COLUMN_PHONE = "phone";
private static final String COLUMN_COURSE = "course";
private static final String TABLE_CREATE = "create table if not exists contacts (id integer primary key not null, name text not null, email text not null, uname text not null, pass text name not null);";

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME,null,DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(TABLE_CREATE);
    this.db = db;
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    String query = "DROP TABLE IF EXISTS" + TABLE_NAME;
    db.execSQL(query);
    this.onCreate(db);
}
public void insertContact(Contact c)
{
    db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    String query = "select * from contacts";
    Cursor cu = db.rawQuery(query, null);
    int count = cu.getCount();

    values.put(COLUMN_ID, count);

    values.put(COLUMN_NAME, c.getName());
    values.put(COLUMN_EMAIL, c.getEmail());
    values.put(COLUMN_UNAME, c.getUname());
    values.put(COLUMN_PASS, c.getPass());
    values.put(COLUMN_BIRTH, c.getBirth());
    values.put(COLUMN_PHONE, c.getPhone());
    values.put(COLUMN_COURSE, c.getCourse());
    db.insert(TABLE_NAME, null, values);
    db.close();
}
public String searchPass(String uname)
{
    db = this.getReadableDatabase();
    String query = "select uname, pass from "+ TABLE_NAME;
    Cursor cu = db.rawQuery(query, null);
    String a,b = null;
    if(cu.moveToFirst())
    {
        do{
            a = cu.getString(0);

            if(a.equals(uname))
            {
                b = cu.getString(1);
                break;
            }
        }
        while(cu.moveToNext());

    }
    return b;

}

} thank for your help..

madara09
  • 81
  • 1
  • 8

1 Answers1

1

There are are two ways: First is in your DatabaseHelper's onCreate() method after creating table insert your record.

Second is create a SQLite database file separately which contains your data. and put it in assets folder, and when app is start check for data base is exist or not, if not then copy to your local database. Note that in this approach do not write Create table queries in DatabaseHelper.

EDIT

For First way:

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(TABLE_CREATE);
    this.db = db;
    Contact c=new Contact();
    // set your data
    insertContact(c);
}

For Second Way:

Follow this link : Copy Database from assets folder in unrooted device

Community
  • 1
  • 1
Asif Patel
  • 1,744
  • 1
  • 20
  • 27