Sie sind auf Seite 1von 20

SQLiteExample.

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

// THIS IS THE SQLITE DATABASE FILE NAME.

15

private static String TABLE = "MY_TABLE";

16

@Override

17

public void onCreate(Bundle savedInstanceState) {

18

19

// THIS IS THE TABLE NAME

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

// DROPPING THE TABLE.

24
createTable();
25

26

TextView t0 = new TextView(this);

t0.setText("This tutorial covers CREATION, INSERTION, UPDATION AND DELETION USING


SQLITE DATABASES.

Creating table complete........");

27
Linear.addView(t0);
28
Toast.makeText(getApplicationContext(), "Creating table complete.",

29

30

31

Toast.LENGTH_SHORT).show();

insertIntoTable();

TextView t1 = new TextView(this);

t1.setText("Insert into table complete........");


32
Linear.addView(t1);
33
Toast.makeText(getApplicationContext(), "Insert into table complete",
34

35

36

Toast.LENGTH_SHORT).show();

TextView t2 = new TextView(this);

t2.setText("Showing table values............");

Linear.addView(t2);
37
showTableValues();
38
Toast.makeText(getApplicationContext(), "Showing table values",
39

40

Toast.LENGTH_SHORT).show();

updateTable();

TextView t3 = new TextView(this);


41
t3.setText("Updating table values............");
42
Linear.addView(t3);

43

Toast.makeText(getApplicationContext(), "Updating table values",


Toast.LENGTH_SHORT).show();

44
TextView t4 = new TextView(this);
45
t4.setText("Showing table values after updation..........");
46

47

Linear.addView(t4);

Toast.makeText(getApplicationContext(), "Showing table values after updation.",


Toast.LENGTH_SHORT).show();

48
showTableValues();
49
deleteValues();
50

51

TextView t5 = new TextView(this);

t5.setText("Deleting table values..........");

52

Linear.addView(t5);

53

Toast.makeText(getApplicationContext(), "Deleting table values",


Toast.LENGTH_SHORT).show();

54
TextView t6 = new TextView(this);
55

56

t6.setText("Showing table values after deletion.........");

Linear.addView(t6);

Toast.makeText(getApplicationContext(), "Showing table values after deletion.",

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

// THIS FUNCTION SETS COLOR AND PADDING FOR THE TEXTVIEWS

67

public void setColor(TextView t){

t.setTextColor(Color.BLACK);
68
t.setPadding(20, 5, 0, 5);
69
t.setTextSize(1, 15);
70
}

71

72

73

// CREATE TABLE IF NOT EXISTS

public void createTable(){

try{

74

mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);


75
mydb.execSQL("CREATE TABLE IF NOT EXISTS "+ TABLE +" (ID INTEGER PRIMARY KEY,
76

NAME TEXT, PLACE TEXT);");

mydb.close();

77

}catch(Exception e){

78

Toast.makeText(getApplicationContext(), "Error in creating table", Toast.LENGTH_LONG);

79
}
80
}
81

// THIS FUNCTION INSERTS DATA TO THE DATABASE


82
public void insertIntoTable(){
83

84

try{

mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);

mydb.execSQL("INSERT INTO " + TABLE + "(NAME, PLACE)

85

VALUES('CODERZHEAVEN','GREAT INDIA')");

mydb.execSQL("INSERT INTO " + TABLE + "(NAME, PLACE) VALUES('ANTHONY','USA')");

86

mydb.execSQL("INSERT INTO " + TABLE + "(NAME, PLACE) VALUES('SHUING','JAPAN')");

87

mydb.execSQL("INSERT INTO " + TABLE + "(NAME, PLACE) VALUES('JAMES','INDIA')");

88

mydb.execSQL("INSERT INTO " + TABLE + "(NAME, PLACE) VALUES('SOORYA','INDIA')");


89
mydb.execSQL("INSERT INTO " + TABLE + "(NAME, PLACE) VALUES('MALIK','INDIA')");
90
mydb.close();
91
}catch(Exception e){
92
Toast.makeText(getApplicationContext(), "Error in inserting into table",
93

Toast.LENGTH_LONG);

94
}
95

// THIS FUNCTION SHOWS DATA FROM THE DATABASE


96
public void showTableValues(){
97
try{
98

mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);

99

10
0

10

Cursor allrows = mydb.rawQuery("SELECT * FROM "+ TABLE, null);

System.out.println("COUNT : " + allrows.getCount());

Integer cindex = allrows.getColumnIndex("NAME");

Integer cindex1 = allrows.getColumnIndex("PLACE");

10
2

10

TextView t = new TextView(this);

t.setText("========================================");

//Linear.removeAllViews();

10

Linear.addView(t);

10

if(allrows.moveToFirst()){

5
do{
10
6

10
7

10

LinearLayout id_row = new LinearLayout(this);

LinearLayout name_row = new LinearLayout(this);

LinearLayout place_row= new LinearLayout(this);

final TextView id_ = new TextView(this);

10

final TextView name_ = new TextView(this);

9
final TextView place_ = new TextView(this);
11

final TextView sep = new TextView(this);

11
1

String ID = allrows.getString(0);

11

String NAME= allrows.getString(1);

String PLACE= allrows.getString(2);

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

System.out.println("NAME " + allrows.getString(cindex) + " PLACE : "+

allrows.getString(cindex1));
11
System.out.println("ID : "+ ID + " || NAME " + NAME + "|| PLACE : "+ PLACE);

11
9

id_.setText("ID : " + ID);

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

place_.setText("PLACE : " + PLACE);

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

// THIS FUNCTION UPDATES THE DATABASE ACCORDING TO THE CONDITION

public void updateTable(){

try{

13

mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);

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

public void deleteValues(){

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){

Toast.makeText(getApplicationContext(), "Error encountered while deleting.",

14
0

Toast.LENGTH_LONG);

}
14
1

14
2

14

// THIS FUNTION DROPS A TABLE

public void dropTable(){

try{

3
mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
14

mydb.execSQL("DROP TABLE " + TABLE);

14

mydb.close();

}catch(Exception e){

14
6

Toast.makeText(getApplicationContext(), "Error encountered while dropping.",


Toast.LENGTH_LONG);

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

Now the main.xml file

<?xml version="1.0" encoding="utf-8"?>

<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

The mainfest.xml file.


?
1

<?xml version="1.0" encoding="utf-8"?>

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

<application android:icon="@drawable/icon" android:label="@string/app_name">

<activity android:name=".SQLiteExample"

10

11

12

android:label="SQLite Example Demo">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>
13
</application>
14
</manifest
15

Das könnte Ihnen auch gefallen