Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get Id of a Particulat Record Insert in database  RSS feed

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();


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
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)";

// Upgrading database
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed

// Create tables again

// 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?

Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!