Professional Documents
Culture Documents
Database Example in Android
Database Example in Android
java
1
package pac.SQLite;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
7
import android.widget.LinearLayout;
8
import android.widget.TextView;
9
import android.widget.Toast;
10
public class SQLiteExample extends Activity {
11
LinearLayout Linear;
12
SQLiteDatabase mydb;
13
private static String DBNAME = "PERSONS.db";
14
15
16
@Override
17
18
19
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
20
Linear = (LinearLayout)findViewById(R.id.linear);
21
Toast.makeText(getApplicationContext(), "Creating table.", Toast.LENGTH_SHORT).show();
22
23
dropTable();
24
createTable();
25
26
27
Linear.addView(t0);
28
Toast.makeText(getApplicationContext(), "Creating table complete.",
29
30
31
Toast.LENGTH_SHORT).show();
insertIntoTable();
35
36
Toast.LENGTH_SHORT).show();
Linear.addView(t2);
37
showTableValues();
38
Toast.makeText(getApplicationContext(), "Showing table values",
39
40
Toast.LENGTH_SHORT).show();
updateTable();
43
44
TextView t4 = new TextView(this);
45
t4.setText("Showing table values after updation..........");
46
47
Linear.addView(t4);
48
showTableValues();
49
deleteValues();
50
51
52
Linear.addView(t5);
53
54
TextView t6 = new TextView(this);
55
56
Linear.addView(t6);
57
Toast.LENGTH_SHORT).show();
showTableValues();
58
setColor(t0);
59
setColor(t1);
60
setColor(t2);
61
setColor(t3);
62
setColor(t4);
63
setColor(t5);
64
setColor(t6);
65
66
67
t.setTextColor(Color.BLACK);
68
t.setPadding(20, 5, 0, 5);
69
t.setTextSize(1, 15);
70
}
71
72
73
try{
74
mydb.close();
77
}catch(Exception e){
78
79
}
80
}
81
84
try{
85
VALUES('CODERZHEAVEN','GREAT INDIA')");
86
87
88
Toast.LENGTH_LONG);
94
}
95
99
10
0
10
10
2
10
t.setText("========================================");
//Linear.removeAllViews();
10
Linear.addView(t);
10
if(allrows.moveToFirst()){
5
do{
10
6
10
7
10
10
9
final TextView place_ = new TextView(this);
11
11
1
String ID = allrows.getString(0);
11
11
3
id_.setTextColor(Color.RED);
11
4
id_.setPadding(20, 5, 0, 5);
name_.setTextColor(Color.RED);
11
5
11
6
11
name_.setPadding(20, 5, 0, 5);
place_.setTextColor(Color.RED);
place_.setPadding(20, 5, 0, 5);
allrows.getString(cindex1));
11
System.out.println("ID : "+ ID + " || NAME " + NAME + "|| PLACE : "+ PLACE);
11
9
12
id_row.addView(id_);
0
Linear.addView(id_row);
12
name_.setText("NAME : "+NAME);
1
name_row.addView(name_);
12
Linear.addView(name_row);
12
3
place_row.addView(place_);
12
Linear.addView(place_row);
4
sep.setText("---------------------------------------------------------------");
12
Linear.addView(sep);
5
}
12
while(allrows.moveToNext());
12
7
mydb.close();
12
}catch(Exception e){
8
Toast.makeText(getApplicationContext(), "Error encountered.", Toast.LENGTH_LONG);
12
9
}
13
0
13
1
try{
13
2
mydb.execSQL("UPDATE " + TABLE + " SET NAME = 'MAX' WHERE PLACE = 'USA'");
13
mydb.close();
3
}catch(Exception e){
13
Toast.makeText(getApplicationContext(), "Error encountered", Toast.LENGTH_LONG);
4
}
13
13
6
// THIS FUNCTION DELETES VALUES FROM THE DATABASE ACCORDING TO THE CONDITION
13
try{
7
mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
13
mydb.execSQL("DELETE FROM " + TABLE + " WHERE PLACE = 'USA'");
8
mydb.close();
13
}catch(Exception e){
14
0
Toast.LENGTH_LONG);
}
14
1
14
2
14
try{
3
mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
14
14
mydb.close();
}catch(Exception e){
14
6
14
7
14
8
14
9
15
0
15
1
15
2
15
15
4
15
5
15
6
15
7
15
8
15
9
16
0
16
1
16
16
3
16
4
16
5
16
6
16
7
16
8
16
9
17
0
17
17
2
17
3
17
4
17
5
17
6
17
7
17
8
17
9
18
18
1
18
2
18
3
18
4
18
5
18
6
18
7
18
8
18
19
0
19
1
19
2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
5
<ScrollView
6
android:id="@+id/ScrollView01"
7
android:layout_width="fill_parent"
android:layout_height="wrap_content"
10
android:background="@drawable/android">
11
<LinearLayout
android:id="@+id/linear"
12
android:orientation="vertical"
13
android:layout_below="@+id/add_record"
14
android:layout_width="wrap_content"
15
android:layout_height="fill_parent">
16
</LinearLayout>
17
</ScrollView>
18
</LinearLayout>
19
2
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3
package="pac.SQLite"
4
android:versionCode="1"
5
android:versionName="1.0">
6
<activity android:name=".SQLiteExample"
10
11
12
<intent-filter>
</intent-filter>
</activity>
13
</application>
14
</manifest
15