What is SQLite?
SQLite is an Open Source Database which is embedded into Android. SQLite
supports standard relational database features like SQL syntax, transactions and
prepared statements. In addition it requires only little memory at runtime (approx. 250
KByte).
SQLite supports the data types TEXT (similar to String in Java), INTEGER (similar
to long in Java) and REAL (similar to double in Java). All other types must be
converted into one of these fields before saving them in the database. SQLite itself does
not validate if the types written to the columns are actually of the defined type, e.g.
writing an integer into a string column and vice versa.
SQLite is available on every Android device. Using an SQLite database in Android
does not require any database setup or administration. The SQL statements for creating
and updating the database must only be defined and the database is automatically
managed for users by the Android platform.
SQLite Operations:
1. Create a database.
2. Open the database.
3. Create a table.
4. Create an insert interface for data-sets.
5. Create a query interface for data sets.
6. Close the database.
Creating Table:
static final String DATABASE_USERDETAILS = "create table "+"emp_details"+"( "
+"EMPLOYEE_ID INTEGER primary key AUTOINCREMENT, "
+ "EMPLOYEE_NAME TEXT NOT NULL," +
"MOBILE TEXT NOT NULL ," +
"IMAGENAME TEXT ); ";
Inserting Data into Table:
public void insertEmployeeDetails(String emp_name,String mobile,String imageName)
{
ContentValues newValues = new ContentValues();
newValues.put("EMPLOYEE_NAME", emp_name);
newValues.put("MOBILE", mobile);
newValues.put("IMAGENAME", imageName);
db.insert("emp_details", null, newValues);
}
Checking Table:
public String checkempty()
{
String checkdb="EXIST";
String selectQuery = "SELECT * FROM emp_details";
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.getCount()<1)
{
cursor.close();
checkdb="NOT EXIST";
}
return checkdb;
}
Selecting Table Values:
public ArrayList<EmployeeModel> getEmployeeeDetails() {
ArrayList<EmployeeModel> EmployeeList = new ArrayList<EmployeeModel>();
try {
String selectQuery = "select * from emp_details";
Cursor cursor = db.rawQuery(selectQuery, null);
int c1 = cursor.getColumnIndex("EMPLOYEE_NAME");
int c2 = cursor.getColumnIndex("MOBILE");
int c3 = cursor.getColumnIndex("IMAGENAME");
int c4 = cursor.getColumnIndex("EMPLOYEE_ID");
if (cursor.moveToFirst()) {
do {
EmployeeModel emplist=new EmployeeModel();
emplist.setName(cursor.getString(c1));
emplist.setMobileNo(cursor.getString(c2));
emplist.setImage(cursor.getString(c3));
emplist.setEmp_ID(cursor.getString(c4));
EmployeeList.add(emplist);
} while (cursor.moveToNext());
}
} catch (Exception e) {
e.printStackTrace();
}
return EmployeeList;
}
Delete Query:
public void deleteContact(String number)
{
db.delete("emp_details", "EMPLOYEE_ID=?", new String[]{""+number});
}
Update Query:
public void Updatedetails(String name,String mobileNumber,String EmpId)
{
ContentValues updatedValues = new ContentValues();
updatedValues.put("EMPLOYEE_NAME", name);
updatedValues.put("MOBILE", mobileNumber);
String where="EMPLOYEE_ID = ?";
db.update("emp_details",updatedValues, where, new String[]{EmpId});
}
For Sample Program: Click Here
No comments:
Post a Comment