• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get Id of a Particulat Record Insert in database

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there I am a Application in SQLite in Which I am inserting a data in SQLite and Later I want to get Id of that record How can I do that

this is my Activity code from where I am inserting value on database

String email=et_Email.getText().toString();
String name=et_UserName.getText().toString();
String phone=et_Phoneno.getText().toString();
String pass=et_Password.getText().toString();

UserData userdata=new UserData();
userdata.setEmail(email);
userdata.setName(name);
userdata.setPass(pass);
userdata.setPhone(phone);

db.addContact(userdata);


and this is DBHelper class

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 2;

// Database Name
private static final String DATABASE_NAME = "userdetail";

// Contacts table name
private static final String TABLE_NAME = "userdata";

// Contacts Table Columns names
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_PH_NO = "phone_number";
private static final String COLUMN_EMAIL= "email";
private static final String COLUMN_PASS = "password";


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



// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "create table userdata(id Integer primary key,name text,phone_number text,email text,password text)";
db.execSQL(CREATE_CONTACTS_TABLE);
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);

// Create tables again
onCreate(db);
}

// Adding new contact
public void addContact(UserData UserData) {
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(COLUMN_NAME, UserData.getName()); // Contact Name
values.put(COLUMN_PH_NO, UserData.getPhone()); // Contact Phone
values.put(COLUMN_EMAIL, UserData.getEmail());
values.put(COLUMN_PASS, UserData.getPass());
db.insert(TABLE_NAME, null, values);
// Inserting Row
db.close(); // Closing database connection

}


How can I get id of that Particular Record that I am inserting

Record are inserting successfully.


I have one more question Where is this SQLite database is stored in Mobile Phone.I mean in which directory?


Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic