-
Notifications
You must be signed in to change notification settings - Fork 6.1k
/
DatabaseHelper.java
112 lines (87 loc) · 3.46 KB
/
DatabaseHelper.java
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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
private SQLiteDatabase db;
static final String DATABASE_NAME = "College";
static final String STUDENTS_TABLE_NAME = "students";
static final int DATABASE_VERSION = 1;
static final String CREATE_DB_TABLE =
" CREATE TABLE IF NOT EXISTS " + STUDENTS_TABLE_NAME +
" (_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
" name TEXT NOT NULL, " +
" age TEXT NOT NULL);";
/**
* Helper class that actually creates and manages
* the provider's underlying data repository.
*/
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(CREATE_DB_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + STUDENTS_TABLE_NAME);
onCreate(db);
}
}
//user class
DatabaseHelper dbHelper = new DatabaseHelper(context);
db = dbHelper.getWritableDatabase();
public long insert(Uri uri, ContentValues values)
{
long rowID = db.insert( STUDENTS_TABLE_NAME, "", values);
/*
if (rowID > 0)
{
//added
}*/
return rowID;
}
//select
//projection coloum
String[] projection={"name"};
public Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder)
{
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(STUDENTS_TABLE_NAME);
//get all
// qb.setProjectionMap(STUDENTS_PROJECTION_MAP);
//get one
// qb.appendWhere( _ID + "=" + uri.getPathSegments().get(1));
Cursor c = qb.query(db, projection, selection, selectionArgs,null, null, sortOrder);
return c;
}
public int delete(Uri uri, String selection, String[] selectionArgs)
{
int count = db.delete(STUDENTS_TABLE_NAME, selection, selectionArgs);
return count;
}
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)
{
int count = 0;
count = db.update(STUDENTS_TABLE_NAME, values, selection, selectionArgs);
// count = db.update(STUDENTS_TABLE_NAME, values, _ID + " = " + uri.getPathSegments().get(1) +
// (!TextUtils.isEmpty(selection) ? " AND (" +selection + ')' : ""), selectionArgs);
return count;
}
// Add a new student record
ContentValues values = new ContentValues();
// insert value
values.put(StudentsProvider.NAME, "a");
values.put(StudentsProvider.Age,"b");
// define the play to insert the values in
insert( values);
//select
Cursor c = query(students, null, null, null, "name");
// move through all items
if (c.moveToFirst()) {
do{
// load the record name and age and id
Toast.makeText(this,
c.getString(c.getColumnIndex(StudentsProvider._ID)) +
", " + c.getString(c.getColumnIndex( StudentsProvider.NAME)) +
", " + c.getString(c.getColumnIndex( StudentsProvider.Age)),
Toast.LENGTH_SHORT).show();
} while (c.moveToNext());