Insert new record into SQLite Database Android
This blog is in continuity with my previous post which was related to creation of a database helper class. After creating database in android and open the connection we need to add entries into the database.
But before that we just need to create a POJO (Plain Old Java Object) for the table so that we can store the values and directly send it to the database. POJO of PreferenceInfo can be downloaded from this link. (PreferenceInfo.java)
In order to add a
new entry into the database we get a SQLiteDatabase writable instance
by adding following line
SQLiteDatabase db = this.getWritableDatabase();
After getting the
database we must need to set the values that we need to persist into
the database to sepecific column name:
ContentValues newValues = new ContentValues();
newValues.put(E_MAIL, preferenceInfo.getEmail());
newValues.put(PASSWORD, preferenceInfo.getPassword());
newValues.put(USER_NAME, preferenceInfo.getUserName());
newValues.put(MOBILE_NUMBER, preferenceInfo.getMobileNumber());
newValues.put(KEY_MESSAGE, preferenceInfo.getKeyMessage());
Now, insert the
values into the table by using insert method of the SQLiteDatabase
instance
db.insert(REGISTER_TABLE, //table
null,//ColumnHack,
newValues); // key/value -> keys = column names/ values = column values
Now after each and
everything we must need to close the database connection after adding
the data into database.
db.close();
Updated
AndroidHelper file and PreferenceInfo.java POJO.
Comments
Post a Comment