Use of SQLite
Use of SQLite
Use of SQLite
Posted by Sushant on June 29, 2011, filed in: Android Development, Android Programming Tutorials
25
Hello there! If you are new here, you might want to subscribe to the RSS feed for
updates on this topic.
This is a sample program which shows usage of SQLite in android application for saving data. This application has
two buttons in the main menu one for saving information and another for showing all saved information. Last blog
published in this forum is How to handle bluetooth settings from your application.
Underlying Algorithm:
Basic description of algorithm in step by step form:
1.) Create a Project DatabaseSample.
2.) Replace the following code with res/layout/main.xml :
3.) Create a helper class DataManipulator.java that can create the database and encapsulate other SQL details. In
this DataManipulator class we will include an important inner class OpenHelper that provides a SQLiteOpenHelper.
1
2
3
4
5
6
package com.app.DatabaseSample;
import
import
import
import
import
android.content.Context;
android.database.Cursor;
android.database.sqlite.SQLiteDatabase;
android.database.sqlite.SQLiteOpenHelper;
android.database.sqlite.SQLiteStatement;
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.ArrayList;
import java.util.List;
public class DataManipulator
{
private static final String DATABASE_NAME = "mydatabase.db";
private static final int DATABASE_VERSION = 1;
static final String TABLE_NAME = "newtable";
private static Context context;
static SQLiteDatabase db;
private SQLiteStatement insertStmt;
private static final String INSERT = "insert into " + TABLE_NAME + " (name,number,sk
public DataManipulator(Context context) {
DataManipulator.context = context;
OpenHelper openHelper = new OpenHelper(DataManipulator.context);
DataManipulator.db = openHelper.getWritableDatabase();
this.insertStmt = DataManipulator.db.compileStatement(INSERT);
}
public long insert(String name,String number,String skypeId,String address) {
this.insertStmt.bindString(1, name);
this.insertStmt.bindString(2, number);
this.insertStmt.bindString(3, skypeId);
this.insertStmt.bindString(4, address);
return this.insertStmt.executeInsert();
}
public void deleteAll() {
db.delete(TABLE_NAME, null, null);
}
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
}
}
4
1
5
1
</LinearLayout>
6 </LinearLayout>
1
7
1
8
5.) Create a Activity SaveData.java to Save the information :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.app.DatabaseSample;
import
import
import
import
import
import
import
import
import
import
android.app.Activity;
android.view.View;
android.view.View.OnClickListener;
android.app.AlertDialog;
android.app.Dialog;
android.content.DialogInterface;
android.content.Intent;
android.os.Bundle;
android.widget.EditText;
android.widget.TextView;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
break;
}
}
protected final Dialog onCreateDialog(final int id) {
Dialog dialog = null;
switch(id) {
case DIALOG_ID:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Information saved successfully ! Add Another Info?").setCa
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
SaveData.this.finish();
}
}).setNegativeButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
dialog = alert;
break;
default:
}
return dialog;
}
}
1
2
3
4
5
6
7
8
1
2
package com.app.DatabaseSample;
import java.util.ArrayList;
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import
import
import
import
import
import
import
java.util.List;
android.app.ListActivity;
android.os.Bundle;
android.view.View;
android.widget.ArrayAdapter;
android.widget.ListView;
android.widget.TextView;
On Clicking Finish DatabaseSample code structure is generated with the necessary Android Packages being
imported along with DatabaseSample.java. DatabaseSample class will look like following :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.app.DatabaseSample;
import
import
import
import
import
android.app.Activity;
android.content.Intent;
android.os.Bundle;
android.view.View;
android.view.View.OnClickListener;
24
25
26
27
28
29
30
31
32
33
34