Android DB Connectivity
Step:1
Create java class for all DB Operations at the location where [Link] file is present i.e.
appjava[Link]…. --> right click new class
give the name as “DBHelper”
======== Code in [Link]============
step:2
Extend this class using SQLiteOpenHelper
hover and click on implement methods
hover and define constructor and keep only context part and remove the remaining par.
Step:3
declare following objects globally
1. private static final String DB_NAME="[Link]";
2. private static final int DB_VERSION=1;
3. private static final String TB_NAME="studinfo";
4. private static final String COL1="sid";
5. private static final String COL2="sname";
6. ContentValues valOb = new ContentValues();
7. Context conObj;
Initialize context object in DBHelper constructor as:
conObj=context;
[ OnCreate and onUpgrade provide object for SQLiteDatabase]
In onCreate methodcreate table as:
[Link]("CREATE TABLE studinfo(sid INTEGER,sname text)");
[Link](conObj, "Table Created", Toast.LENGTH_SHORT).show();
Write following code in OnUpgrade method
[Link]("DROP TABLE IF EXISTS studinfo");
Step:4
Create method to insert record as:
public long insertRec(int sid,String sname)
{
1. long rst=-1;
2. SQLiteDatabase sqldb=[Link]();
3. try {
4. [Link](COL1,sid);
5. [Link](COL2, sname);
6. rst=[Link](TB_NAME,null,valOb);
7. }catch(SQLiteConstraintException ex1) {
8. [Link](conObj, [Link](), Toast.LENGTH_SHORT).show();
9. }
10. catch(SQLiteDatabaseLockedException ex2)
11. { [Link](conObj, [Link](), Toast.LENGTH_SHORT).show();}
12. catch(Exception ex3){[Link](conObj, [Link](), Toast.LENGTH_SHORT).show();}
13. finally {
14. [Link]();
15. }
16. return rst;
}
Step:5
Create method to Read record as:
1. public Cursor getData()
2. {
3. SQLiteDatabase sqldb=[Link]();
4. return [Link](TB_NAME,null,null,null,null,null,null);
5. }
======= Code in [Link]===========
- Set linear layout with vertical horizontal orientation
- Add two textviews ,two edit text , two buttons as Save Record, Read Record
- Add one ListView
======= Code in [Link]===========
1. Declare two EditText objects, 2 buttons objects and one ListView obj
2. Declare DBHelper object as ; DBHelper DBobj;
3. Initialize all the objects of line 1 using findby….
4. Init DBHelper obj as : DBobj=new DBHelper(this);
5. Declare one String data[] array
6. Insert button code:
I. if([Link]().toString().isEmpty() || [Link]().toString().isEmpty())
II. [Link]([Link], "ERROR - SID or SNAME is Empty",
Toast.LENGTH_SHORT).show();
III. else {
IV. int sid = [Link]([Link]().toString());
V. String sname = [Link]().toString();
VI. long id = [Link](sid, sname);
VII. if (id > 0) {
a. [Link]([Link], "Record Saved", Toast.LENGTH_SHORT).show();
b. [Link]("");
c. [Link]("");
VIII. }
IX. }
7. Code to read record/s
a. Cursor crObj=[Link]();
b. int k=0;
c. if([Link]())
d. {
e. do {
f. int sid=[Link]([Link]("sid"));
g. String s=[Link]([Link]("sname"));
h. data[k]=""+sid+"---"+s;
i. k++;
j. }while([Link]());
k. }
l. [Link]();
m. if(k==0)
n. [Link]([Link], "No Records", Toast.LENGTH_SHORT).show();
o. else
p. {
q. ArrayAdapter <String> adpObj=new ArrayAdapter<>([Link],
[Link].simple_list_item_1,data);
r. [Link](adpObj);
s. }