The main goal of my program is select a station name and click the button.After click the button I am trying to increase the passenger value. I can get the value of passengers correctly but when I am trying to update it it doesn't update the value.I tried every possible solution and couldn't fix it.What should I do or am i doing wrong something?
Here is my DB helper class
public class DB_Helper extends SQLiteOpenHelper {
create_station parameter_helper;
ContentValues values=new ContentValues();
public static final String DBNAME="users.db";
public DB_Helper(@Nullable Context context) {
super(context, DBNAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("create table users(username TEXT primary key,password TEXT,admin TEXT)");
sqLiteDatabase.execSQL("create table station(station_name TEXT primary key,latitude TEXT,longitude TEXT,passengers INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("drop table if exists users");
sqLiteDatabase.execSQL("drop table if exists station");
}
public Boolean insertData(String username,String password,String admin){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("username",username);
values.put("password",password);
values.put("admin",admin);
long result=db.insert("users",null,values);
if (result==-1)
return false;
else
return true;
}
public Boolean insert_station_Data(String station_name,String latitude,String longitude,int num_passenger){
SQLiteDatabase db=this.getWritableDatabase();
values.put("station_name",station_name);
values.put("latitude",latitude);
values.put("longitude",longitude);
values.put("passengers",num_passenger);
long result=db.insert("station",null,values);
if (result==-1)
return false;
else
return true;
}
public ArrayList<String> get_Latitude(){
ArrayList<String> station_Latitude = new ArrayList<>();
SQLiteDatabase db=this.getReadableDatabase();
Cursor cursor=db.rawQuery("select latitude from station ",null);
while(cursor.moveToNext()) {
String data=cursor.getString(cursor.getColumnIndexOrThrow("latitude"));
station_Latitude.add(data);
}
cursor.close();
return station_Latitude;
}
public ArrayList<String> get_Longitude(){
ArrayList<String> station_Longitude = new ArrayList<>();
SQLiteDatabase db=this.getReadableDatabase();
Cursor cursor=db.rawQuery("select longitude from station ",null);
while(cursor.moveToNext()) {
String data=cursor.getString(cursor.getColumnIndexOrThrow("longitude"));
station_Longitude.add(data);
}
cursor.close();
return station_Longitude;
}
public ArrayList<String> get_Station_Name(){
ArrayList<String> station_name = new ArrayList<>();
SQLiteDatabase db=this.getReadableDatabase();
Cursor cursor=db.rawQuery("select station_name from station ORDER BY rowid",null);
while(cursor.moveToNext()) {
String data=cursor.getString(cursor.getColumnIndexOrThrow("station_name"));
station_name.add(data);
}
cursor.close();
return station_name;
}
public void user_add_passenger(String inbound_station){
SQLiteDatabase db=this.getReadableDatabase();
SQLiteDatabase db2=this.getWritableDatabase();
ContentValues values = new ContentValues();
int num_of_passenger=0;
Cursor cursor=db.rawQuery("select passengers from station where station_name=?",new String[]{inbound_station});
while(cursor.moveToNext()) {
num_of_passenger=cursor.getInt(cursor.getColumnIndexOrThrow("passengers"));
System.out.println("Giriyoor");
}
num_of_passenger =1;
values.put("station_name",String.valueOf(num_of_passenger));
System.out.println("Passenger_str=" num_of_passenger);
System.out.println(db2.update("station", values, "passengers = ?", new String[]{String.valueOf(num_of_passenger)}));
}
public Boolean checkusername(String username){
SQLiteDatabase db=this.getWritableDatabase();
Cursor cursor=db.rawQuery("select * from users where username=?",new String[]{username});
if (cursor.getCount()>0)
return true;
else
return false;
}
public Boolean checkusernamepassword(String username,String password,String admin){
SQLiteDatabase db=this.getWritableDatabase();
Cursor cursor=db.rawQuery("select * from users where username=? and password=? and admin=?",new String[]{username,password,admin});
if (cursor.getCount()>0){
return true;
}
else{
return false;
}
}
The function which doesn't upgrade the variable: I am trying do query firstly for getting passenger value.After that I am trying to increase the num of passenger value and updating passenger.There is no error but it doesn't update it.Even I look similar problem here SQLiteDatabase update not working? I still couldn't fix the problem.
public void user_add_passenger(String inbound_station){
SQLiteDatabase db=this.getReadableDatabase();
SQLiteDatabase db2=this.getWritableDatabase();
ContentValues values = new ContentValues();
int num_of_passenger=0;
Cursor cursor=db.rawQuery("select passengers from station where station_name=?",new String[]{inbound_station});
while(cursor.moveToNext()) {
num_of_passenger=cursor.getInt(cursor.getColumnIndexOrThrow("passengers"));
System.out.println("Giriyoor");
}
num_of_passenger =1;
values.put("station_name",String.valueOf(num_of_passenger));
System.out.println("Passenger_str=" num_of_passenger);
System.out.println(db2.update("station", values, "passengers = ?", new String[]{String.valueOf(num_of_passenger)}));
}
CodePudding user response:
check you are passing right column name and value inside ContentValue.
values.put("station_name",String.valueOf(num_of_passenger));
I think it should be;
values.put("passengers",String.valueOf(num_of_passenger));
and update query
should be like;
db2.update("station", values, "station_name=?", new String[]{String.valueOf(inbound_station)});