/
SurveyDBHelper.java
107 lines (92 loc) · 3.48 KB
/
SurveyDBHelper.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
107
package com.edocent.surveyapp.database;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by ankursrivastava on 4/2/16.
*/
public class SurveyDBHelper extends SQLiteOpenHelper {
static final String DB_NAME="surveydb";
static final int DB_VERSION=2;
/*Database table and column names*/
public static final String SURVEY_TABLE="survey";
public static final String SURVEY_TABLE_NAME_COLUMN="name";
public static final String SURVEY_TABLE_EMAIL_COLUMN="email";
public static final String SURVEY_TABLE_AGE_COLUMN="age";
public static final String SURVEY_TABLE_LICENSE_COLUMN="license";
public SurveyDBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
/**
* Called when the database is created for the first time. This is where the
* creation of tables and the initial population of the tables should happen.
*
* @param db The database.
*/
@Override
public void onCreate(SQLiteDatabase db) {
onUpgrade(db, 0, DB_VERSION);
}
/**
* Called when the database needs to be upgraded. The implementation
* should use this method to drop tables, add tables, or do anything else it
* needs to upgrade to the new schema version.
* <p/>
* <p>
* The SQLite ALTER TABLE documentation can be found
* <a href="http://sqlite.org/lang_altertable.html">here</a>. If you add new columns
* you can use ALTER TABLE to insert them into a live table. If you rename or remove columns
* you can use ALTER TABLE to rename the old table, then create the new table and then
* populate the new table with the contents of the old table.
* </p><p>
* This method executes within a transaction. If an exception is thrown, all changes
* will automatically be rolled back.
* </p>
*
* @param db The database.
* @param oldVersion The old database version.
* @param newVersion The new database version.
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(oldVersion < 1){
db.execSQL("CREATE TABLE "+SURVEY_TABLE+" (_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+SURVEY_TABLE_NAME_COLUMN+" TEXT, "
+SURVEY_TABLE_EMAIL_COLUMN+" TEXT, "
+SURVEY_TABLE_AGE_COLUMN+" INTEGER);"
);
}
if(oldVersion < 2){
//Perform Updates
db.execSQL("ALTER TABLE "+SURVEY_TABLE+" ADD COLUMN "+SURVEY_TABLE_LICENSE_COLUMN+" TEXT");
}
}
/*
* query(String table,
String[] columns,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String orderBy,
String limit)
* */
public Cursor getSurveyData(SQLiteDatabase db){
// Define a projection that specifies which columns from the database you will actually use after this query.
String[] projection = {
"_id",
SURVEY_TABLE_NAME_COLUMN,
SURVEY_TABLE_EMAIL_COLUMN,
SURVEY_TABLE_AGE_COLUMN
};
// How you want the results sorted in the resulting Cursor
String sortOrder = "_id" + " DESC";
return db.query(
SURVEY_TABLE,
projection,
null, null, null, null,
sortOrder
);
}
}