· 8 years ago · Aug 14, 2018, 08:44 AM
1Android sqlite database column error
2package com.StudentTracker;
3import java.sql.Blob;
4
5import android.content.ContentValues;
6import android.content.Context;
7import android.database.Cursor;
8import android.database.SQLException;
9import android.database.sqlite.SQLiteDatabase;
10import android.database.sqlite.SQLiteOpenHelper;
11import android.util.Log;
12
13
14public class DBAdapter {
15
16 public static final String KEY_ROWID = "id";
17 public static final String KEY_STUDENTNAME = "studentname";
18 public static final String KEY_DOB = "dob";
19 public static final String KEY_ADDRESS1 = "address1";
20 public static final String KEY_ADDRESS2 = "address2";
21 public static final String KEY_TOWN = "town";
22 public static final String KEY_POSTCODE = "postcode";
23 public static final String KEY_PHONE = "phone";
24 public static final String KEY_STUDENT_PIC = "studentpic";
25
26 private static final String TAG = "DBAdapter";
27
28
29
30 private static final String DATABASE_NAME = "studentDB2";
31 private static final String DATABASE_TABLE = "tblstudents";
32 private static final int DATABASE_VERSION = 1;
33
34 private static final String DATABASE_CREATE =
35 "create table tblstudents (id integer primary key autoincrement, "
36 + "studentname text not null, dob text not null, "
37 + "address1 text not null, address2 text not null, "
38 + "town text not null, postcode text not null, "
39 + " phone text not null);";
40
41 private final Context context;
42
43 private DatabaseHelper DBHelper;
44 private SQLiteDatabase db;
45
46 public DBAdapter(Context ctx)
47 {
48 this.context = ctx;
49 DBHelper = new DatabaseHelper(context);
50 }
51
52 private static class DatabaseHelper extends SQLiteOpenHelper
53 {
54 DatabaseHelper(Context context)
55 {
56 super(context, DATABASE_NAME, null, DATABASE_VERSION);
57 }
58
59 @Override
60 public void onCreate(SQLiteDatabase db)
61 {
62 db.execSQL(DATABASE_CREATE);
63 }
64
65 @Override
66 public void onUpgrade(SQLiteDatabase db, int oldVersion,
67 int newVersion)
68 {
69 Log.w(TAG, "Upgrading database from version " + oldVersion
70 + " to "
71 + newVersion + ", which will destroy all old data");
72 db.execSQL("DROP TABLE IF EXISTS studentDB2");
73 onCreate(db);
74 }
75 }
76
77 //---opens the database---
78 public DBAdapter open() throws SQLException
79 {
80 db = DBHelper.getWritableDatabase();
81 return this;
82 }
83
84 //---closes the database---
85 public void close()
86 {
87 DBHelper.close();
88 }
89
90 //---insert a title into the database---
91 public long insertStudent(String studentname, String dob, String address1, String address2, String town, String postcode, String phone)
92 {
93 ContentValues initialValues = new ContentValues();
94 initialValues.put(KEY_STUDENTNAME, studentname);
95 initialValues.put(KEY_DOB, dob);
96 initialValues.put(KEY_ADDRESS1, address1);
97 initialValues.put(KEY_ADDRESS2, address2);
98 initialValues.put(KEY_TOWN, town);
99 initialValues.put(KEY_POSTCODE, postcode);
100 initialValues.put(KEY_PHONE, phone);
101 //initialValues.put(KEY_STUDENT_PIC, studentpic);
102
103 return db.insert(DATABASE_TABLE, null, initialValues);
104 }
105
106 //---deletes a particular title---
107 public boolean deleteTitle(long rowId)
108 {
109 return db.delete(DATABASE_TABLE, KEY_ROWID +
110 "=" + rowId, null) > 0;
111 }
112
113 //---retrieves all the titles---
114 public Cursor getAllStudents()
115 {
116 return db.query(DATABASE_TABLE, new String[] {
117 KEY_ROWID,
118 KEY_STUDENTNAME,
119 KEY_DOB,
120 KEY_ADDRESS1,
121 KEY_ADDRESS2,
122 KEY_TOWN,
123 KEY_POSTCODE,
124 KEY_PHONE
125 },
126 null,
127 null,
128 null,
129 null,
130 null);
131 }
132
133 //---retrieves a particular title---
134 public Cursor getStudent(long rowId) throws SQLException
135 {
136 Cursor mCursor =
137 db.query(true, DATABASE_TABLE, new String[] {
138 KEY_ROWID,
139 KEY_STUDENTNAME,
140 KEY_DOB,
141 KEY_ADDRESS1,
142 KEY_ADDRESS2,
143 KEY_TOWN,
144 KEY_POSTCODE,
145 KEY_PHONE
146 },
147 KEY_ROWID + "=" + rowId,
148 null,
149 null,
150 null,
151 null,
152 null);
153 if (mCursor != null) {
154 mCursor.moveToFirst();
155 }
156 return mCursor;
157 }
158
159 //---updates a title---
160 public boolean updateStudent(long rowId, String studentname,
161 String dob, String address1, String address2, String town,
162 String postcode, String phone)
163 {
164 ContentValues args = new ContentValues();
165 args.put(KEY_STUDENTNAME, studentname);
166 args.put(KEY_DOB, dob);
167 args.put(KEY_ADDRESS1, address1);
168 args.put(KEY_ADDRESS2, address2);
169 args.put(KEY_TOWN, town);
170 args.put(KEY_POSTCODE, postcode);
171 args.put(KEY_PHONE, phone);
172 //args.put(KEY_STUDENT_PIC, studentpic);
173
174 return db.update(DATABASE_TABLE, args,
175 KEY_ROWID + "=" + rowId, null) > 0;
176 }
177
178
179
180
181}
182
183db.execSQL("DROP TABLE IF EXISTS tblstudents");
184
185private static final int DATABASE_VERSION = 2;