· 7 years ago · Sep 15, 2018, 01:50 AM
1Creating tables in sqlite database on android
2public class DatabaseMarks {
3
4 public static final String KEY_STUID = "stuid";
5 public static final String KEY_SUB1 = "subject_one";
6 public static final String KEY_SUB2 = "subject_two";
7 public static final String KEY_SUB3= "subject_three";
8 public static final String KEY_MARKS1= "marks_one";
9 public static final String KEY_MARKS2 = "marks_two";
10 public static final String KEY_MARKS3 = "marks_three";
11
12 private static final String DATABASE_NAME = "Student";
13 private static final String DATABASE_MARKSTABLE = "StudentMarks";
14 private static final int DATABASE_VERSION = 1;
15
16 private DbHelper ourHelper;
17 private final Context ourContext;
18 private SQLiteDatabase ourDatabase;
19
20 private static class DbHelper extends SQLiteOpenHelper{
21
22 public DbHelper(Context context) {
23 super(context, DATABASE_NAME, null, DATABASE_VERSION);
24 // TODO Auto-generated constructor stub
25 }
26
27 @Override
28 public void onCreate(SQLiteDatabase db) {
29 // TODO Auto-generated method stub
30 db.execSQL(" CREATE TABLE " + DATABASE_MARKSTABLE + " (" +
31 KEY_STUID + " TEXT PRIMARY KEY, " +
32 KEY_SUB1 + " TEXT NOT NULL, " +
33 KEY_SUB2 + " TEXT NOT NULL, " +
34 KEY_SUB3 + " TEXT NOT NULL, " +
35 KEY_MARKS1 + " INTEGER NOT NULL, " +
36 KEY_MARKS2 + " INTEGER NOT NULL, " +
37 KEY_MARKS3 + " INTEGER NOT NULL);"
38 );
39 }
40
41 @Override
42 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
43 // TODO Auto-generated method stub
44
45 db.execSQL("DROP TABLE IF EXISTS " + DATABASE_MARKSTABLE);
46 onCreate(db);
47 }
48
49 }
50 public DatabaseMarks(Context c){
51 ourContext = c;
52 }
53 public DatabaseMarks open()throws SQLException{
54 ourHelper = new DbHelper(ourContext);
55 ourDatabase = ourHelper.getWritableDatabase();
56 return this;
57 }
58 public void close(){
59 ourHelper.close();
60 }
61 public long createInsert(String stuid, String subject1, String subject2,
62 String subject3, String marks1, String marks2, String marks3) {
63 // TODO Auto-generated method stub
64 ContentValues cv = new ContentValues();
65 cv.put(KEY_STUID, stuid);
66 cv.put(KEY_SUB1, subject1);
67 cv.put(KEY_SUB2, subject2);
68 cv.put(KEY_SUB3, subject3);
69 cv.put(KEY_MARKS1, marks1);
70 cv.put(KEY_MARKS2, marks2);
71 cv.put(KEY_MARKS3, marks3);
72 return ourDatabase.insert(DATABASE_MARKSTABLE, null, cv);
73
74 }
75
76public class DatabaseMarks {
77
78 public static final String KEY_STUID = "stuid";
79 public static final String KEY_SUB1 = "subject_one";
80 public static final String KEY_SUB2 = "subject_two";
81 public static final String KEY_SUB3= "subject_three";
82 public static final String KEY_MARKS1= "marks_one";
83 public static final String KEY_MARKS2 = "marks_two";
84 public static final String KEY_MARKS3 = "marks_three";
85
86 private static final String DATABASE_NAME = "Student";
87 private static final String DATABASE_MARKSTABLE = "StudentMarks";
88 private static final int DATABASE_VERSION = 1;
89
90 private DbHelper ourHelper;
91 private final Context ourContext;
92 private SQLiteDatabase ourDatabase;
93
94 private static class DbHelper extends SQLiteOpenHelper{
95
96 public DbHelper(Context context) {
97 super(context, DATABASE_NAME, null, DATABASE_VERSION);
98 // TODO Auto-generated constructor stub
99 }
100
101 @Override
102 public void onCreate(SQLiteDatabase db) {
103 // TODO Auto-generated method stub
104 db.execSQL(" CREATE TABLE " + DATABASE_MARKSTABLE + " (" +
105 KEY_STUID + " TEXT PRIMARY KEY, " +
106 KEY_SUB1 + " TEXT NOT NULL, " +
107 KEY_SUB2 + " TEXT NOT NULL, " +
108 KEY_SUB3 + " TEXT NOT NULL, " +
109 KEY_MARKS1 + " INTEGER NOT NULL, " +
110 KEY_MARKS2 + " INTEGER NOT NULL, " +
111 KEY_MARKS3 + " INTEGER NOT NULL);"
112 );
113 }
114
115 @Override
116 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
117 // TODO Auto-generated method stub
118
119 db.execSQL("DROP TABLE IF EXISTS " + DATABASE_MARKSTABLE);
120 onCreate(db);
121 }
122
123 }
124 public DatabaseMarks(Context c){
125 ourContext = c;
126 }
127 public DatabaseMarks open()throws SQLException{
128 ourHelper = new DbHelper(ourContext);
129 ourDatabase = ourHelper.getWritableDatabase();
130 return this;
131 }
132 public void close(){
133 ourHelper.close();
134 }
135 public long createInsert(String stuid, String subject1, String subject2,
136 String subject3, String marks1, String marks2, String marks3) {
137 // TODO Auto-generated method stub
138 ContentValues cv = new ContentValues();
139 cv.put(KEY_STUID, stuid);
140 cv.put(KEY_SUB1, subject1);
141 cv.put(KEY_SUB2, subject2);
142 cv.put(KEY_SUB3, subject3);
143 cv.put(KEY_MARKS1, marks1);
144 cv.put(KEY_MARKS2, marks2);
145 cv.put(KEY_MARKS3, marks3);
146 return ourDatabase.insert(DATABASE_MARKSTABLE, null, cv);
147
148 }
149
150public class DatabaseMarks {
151
152 public static final String KEY_STUID = "stuid";
153 public static final String KEY_SUB1 = "subject_one";
154 public static final String KEY_SUB2 = "subject_two";
155 public static final String KEY_SUB3= "subject_three";
156 public static final String KEY_MARKS1= "marks_one";
157 public static final String KEY_MARKS2 = "marks_two";
158 public static final String KEY_MARKS3 = "marks_three";
159
160 private static final String DATABASE_NAME = "Student";
161 private static final String DATABASE_MARKSTABLE = "StudentMarks";
162 private static final int DATABASE_VERSION = 1;
163
164 private DbHelper ourHelper;
165 private final Context ourContext;
166 private SQLiteDatabase ourDatabase;
167
168 private static class DbHelper extends SQLiteOpenHelper{
169
170 public DbHelper(Context context) {
171 super(context, DATABASE_NAME, null, DATABASE_VERSION);
172 // TODO Auto-generated constructor stub
173 }
174
175 @Override
176 public void onCreate(SQLiteDatabase db) {
177 // TODO Auto-generated method stub
178 db.execSQL(" CREATE TABLE " + DATABASE_MARKSTABLE + " (" +
179 KEY_STUID + " TEXT PRIMARY KEY, " +
180 KEY_SUB1 + " TEXT NOT NULL, " +
181 KEY_SUB2 + " TEXT NOT NULL, " +
182 KEY_SUB3 + " TEXT NOT NULL, " +
183 KEY_MARKS1 + " INTEGER NOT NULL, " +
184 KEY_MARKS2 + " INTEGER NOT NULL, " +
185 KEY_MARKS3 + " INTEGER NOT NULL);"
186 );
187 }
188
189 @Override
190 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
191 // TODO Auto-generated method stub
192
193 db.execSQL("DROP TABLE IF EXISTS " + DATABASE_MARKSTABLE);
194 onCreate(db);
195 }
196
197 }
198 public DatabaseMarks(Context c){
199 ourContext = c;
200 }
201 public DatabaseMarks open()throws SQLException{
202 ourHelper = new DbHelper(ourContext);
203 ourDatabase = ourHelper.getWritableDatabase();
204 return this;
205 }
206 public void close(){
207 ourHelper.close();
208 }
209 public long createInsert(String stuid, String subject1, String subject2,
210 String subject3, String marks1, String marks2, String marks3) {
211 // TODO Auto-generated method stub
212 ContentValues cv = new ContentValues();
213 cv.put(KEY_STUID, stuid);
214 cv.put(KEY_SUB1, subject1);
215 cv.put(KEY_SUB2, subject2);
216 cv.put(KEY_SUB3, subject3);
217 cv.put(KEY_MARKS1, marks1);
218 cv.put(KEY_MARKS2, marks2);
219 cv.put(KEY_MARKS3, marks3);
220 return ourDatabase.insert(DATABASE_MARKSTABLE, null, cv);
221
222 }
223
224private static class DatabaseHelper extends SQLiteOpenHelper {
225
226 DatabaseHelper(Context context) {
227 super(context, "NOPO", null, 2);
228 }
229
230 @Override
231 public void onCreate(SQLiteDatabase db) {
232 db.execSQL("CREATE TABLE " + "log" + " ("
233 + KEY_TIME +" INTEGER primary key, "
234 + KEY_TEXT +" TEXT not null);");
235 }
236
237 @Override
238 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
239 Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
240 + newVersion + ", which will destroy all old data");
241 db.execSQL("DROP TABLE IF EXISTS log");
242 onCreate(db);
243 }
244}