I am developing an android application that saves logged in user information in SQLite. But I am facing strange problem. In my DBHelper class create table function is created fine, insert function doesn't throw any exception but when I try to retrieve data from my SQL Lite DB it throws exception
no such column: officer (Sqlite code 1):, while compiling: Select * from User where userId=officer, (OS error - 2:No such file or directory)
I tried changing the database name and version many times but nothing worked. I tried to see database file from my android device to make sure if my table created fine but when I searched in Android/data folder of my phone all apps package names shows up there but my application's package name is not there.
public class DBHelper extends SQLiteOpenHelper {
// Database Name
private static final String DATABASE_NAME = "CMDB.db";
private static final int DATABASE_VERSION = 2;
// Contacts table name
private static final String TABLE_USER = "User";
public static DBHelper dbHelper;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public static DBHelper getInstance(Context context) {
if (dbHelper == null) {
dbHelper = new DBHelper(context.getApplicationContext());
}
return dbHelper;
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
createTables(sqLiteDatabase);
Toast.makeText(App.context,"onCreate",Toast.LENGTH_SHORT).show();
}
private void createTables(SQLiteDatabase sqLiteDatabase) {
try {
sqLiteDatabase.execSQL(" CREATE TABLE " + TABLE_USER + " (" +
"userId" + " TEXT PRIMARY KEY," + "userImage" + " TEXT NOT NULL," +
"userRole" + " TEXT NOT NULL," + "token" + " TEXT NOT NULL," + "userName" + " TEXT NOT NULL," + "userEmail" + " TEXT NOT NULL," + "userAddress" + " TEXT NOT NULL," + "mobile" + " TEXT NOT NULL);"
);
Toast.makeText(App.context,"createTables",Toast.LENGTH_SHORT).show();
}catch (Exception ex){
Toast.makeText(App.context,ex.getMessage(),Toast.LENGTH_SHORT).show();
ex.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
dropTables(sqLiteDatabase);
// Create tables again
onCreate(sqLiteDatabase);
}
private void dropTables(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_USER);
}
public void saveUserToDB(User user) {
try {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("userId", user.userID);
values.put("userImage", user.userImage);
values.put("userRole", user.userRole);
values.put("token", user.token);
values.put("UserName", user.UserName);
values.put("userEmail", user.userEmail);
values.put("userAddress", user.userAddress);
values.put("mobile", user.mobile);
db.insert(TABLE_USER, null, values);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public User getUserFromDB(String userid) {
User user = new User();
try {
SQLiteDatabase db = this.getReadableDatabase();
String query = "Select * from " + TABLE_USER + " where userId=" +userid+ "";
Cursor cursor = db.rawQuery(query, null);
if (cursor != null) {
cursor.moveToFirst();
user.userID = cursor.getString(0);
user.userImage = cursor.getString(1);
user.userRole = cursor.getString(2);
user.token = cursor.getString(3);
user.UserName = cursor.getString(4);
user.userEmail = cursor.getString(5);
user.userAddress = cursor.getString(6);
user.mobile = cursor.getString(7);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return user;
}
public void updateUser(User user, String userId) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
Gson gson = new Gson();
String userJson = gson.toJson(user);
contentValues.put("user", userJson);
db.update(TABLE_USER, contentValues, "userId=" + userId, null);
}
public void updateUser(User user) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("userId", user.userID);
cv.put("userImage", user.userImage);
cv.put("userRole", user.userRole);
cv.put("token", user.token);
cv.put("UserName", user.UserName);
cv.put("userEmail", user.userEmail);
cv.put("userAddress", user.userAddress);
cv.put("mobile", user.mobile);
db.update(TABLE_USER, cv, "userId=" + user.userID, null);
}}
I expect the SQLite DB to be created with a user table and the mentioned columns. Also need to see the .db file in Android Device.
According to the exception you mentioned:
"no such column: officer (Sqlite code 1): , while compiling: Select * from User where userId=officer, (OS error - 2:No such file or directory)"
You have an error in the value for userId
, i.e. officer
.
Try adding a single quote before and after it, like below:
String query = "Select * from " + TABLE_USER + " where userId='" + userid + "'";