Generating a file to store a receipt in android studios - java

Hello I am new to android studios. I am working on an airline reservation project. One of the requirements of the project is to generate a receipt that is stored on an external file every time a customer makes a payment. I have tried looking around trying to figure out how one might accomplish this with no success. In the app I am using SQLite as a database where every user is assigned a balance once they create an account.
Here is my DBHelper class:
package com.example.shashank.fffffffffffffffffffffffffff;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
public class DBHelper extends SQLiteOpenHelper {
public static final String DBNAME = "Login.db";
public static final String FLIGHTS = "FLIGHTS";
public static final String COLUMN_ID = "ID";
public static final String COLUMN_DESTINATION = "DESTINATION";
public static final String COLUMN_PRICE = "PRICE";
public static final String COLUMN_DEPARTURE_TIME = "DEPARTURE_TIME";
public static final String COLUMN_ARRIVAL_TIME = "ARRIVAL_TIME";
public static final String COLUMN_DURATION = "DURATION";
public static final String COLUMN_AVAILABLE_SEATS = "AVAILABLE_SEATS";
public static final String USERS = "users";
public static final String USERNAME = "username";
public static final String PASSWORD = "password";
public static final String EMAIL = "email";
public static final String BALANCE = "balance";
public static final String BOOKING = "booking";
public DBHelper(Context context) {
super(context, "Login.db", null, 1);
}
#Override
public void onCreate(SQLiteDatabase MyDB) {
String createTable1 = ("create Table " + USERS + "(" + USERNAME + " TEXT primary key, " + PASSWORD + " TEXT, " + EMAIL + " TEXT UNIQUE, " + BALANCE + " REAL, " + BOOKING + " INTEGER)");
MyDB.execSQL(createTable1);
MyDB.execSQL("CREATE TABLE " + FLIGHTS + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_DESTINATION + " TEXT, " + COLUMN_PRICE + " REAL, " + COLUMN_DEPARTURE_TIME + " TEXT, " + COLUMN_ARRIVAL_TIME + " TEXT, " + COLUMN_DURATION + " TEXT, " + COLUMN_AVAILABLE_SEATS + " INTEGER)");
ContentValues insertValues = new ContentValues();
insertValues.put(COLUMN_DESTINATION, "Cape Town");
insertValues.put(COLUMN_PRICE, 2000);
insertValues.put(COLUMN_DEPARTURE_TIME, "1200");
insertValues.put(COLUMN_ARRIVAL_TIME, "1400");
insertValues.put(COLUMN_DURATION, "2");
insertValues.put(COLUMN_AVAILABLE_SEATS, 10);
MyDB.insert(FLIGHTS, null, insertValues);
ContentValues insertValues2 = new ContentValues();
insertValues2.put(COLUMN_DESTINATION, "Johannesburg");
insertValues2.put(COLUMN_PRICE, 1000);
insertValues2.put(COLUMN_DEPARTURE_TIME, "1400");
insertValues2.put(COLUMN_ARRIVAL_TIME, "1600");
insertValues2.put(COLUMN_DURATION, "2");
insertValues2.put(COLUMN_AVAILABLE_SEATS, 22);
MyDB.insert(FLIGHTS, null, insertValues2);
ContentValues insertValues3 = new ContentValues();
insertValues3.put(COLUMN_DESTINATION, "Cape Town");
insertValues3.put(COLUMN_PRICE, 500);
insertValues3.put(COLUMN_DEPARTURE_TIME, "1200");
insertValues3.put(COLUMN_ARRIVAL_TIME, "1400");
insertValues3.put(COLUMN_DURATION, "2");
insertValues3.put(COLUMN_AVAILABLE_SEATS, 0);
MyDB.insert(FLIGHTS, null, insertValues3);
}
#Override
public void onUpgrade(SQLiteDatabase MyDB, int i, int i1) {
MyDB.execSQL("drop Table if exists " + USERS);
MyDB.execSQL("drop Table if exists " + FLIGHTS);
onCreate(MyDB);
}
public Boolean insertData(String username, String password, String email, Double balance){
SQLiteDatabase MyDB = this.getWritableDatabase();
ContentValues contentValues= new ContentValues();
contentValues.put(USERNAME, username);
contentValues.put(PASSWORD, password);
contentValues.put(EMAIL, email);
contentValues.put(BALANCE, balance);
long result = MyDB.insert(USERS, null, contentValues);
if(result==-1) return false;
else
return true;
}
public Boolean checkusername(String username) {
SQLiteDatabase MyDB = this.getWritableDatabase();
Cursor cursor = MyDB.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){
SQLiteDatabase MyDB = this.getWritableDatabase();
Cursor cursor = MyDB.rawQuery("Select * from " + USERS + " where " + USERNAME + " = ? and " + PASSWORD + " = ?", new String[] {username,password});
if(cursor.getCount()>0)
return true;
else
return false;
}
public List<FlightsModel> getEveryone(){
List<FlightsModel> returnList = new ArrayList<>();
String queryString = "SELECT * FROM " + FLIGHTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(queryString, null);
if(cursor.moveToFirst()){
do {
int id = cursor.getInt(0);
String destination = cursor.getString(1);
double price = cursor.getDouble(2);
String departure = cursor.getString(3);
String arrival = cursor.getString(4);
String duration = cursor.getString(5);
int space = cursor.getInt(6);
FlightsModel newFlight = new FlightsModel(id, destination, price, departure, arrival, duration, space);
returnList.add(newFlight);
}while (cursor.moveToNext());
}
else{
}
cursor.close();
db.close();
return returnList;
}
#SuppressLint("Range") // suppress Bug/issue with getColumnIndex
public FlightsModel getFlightById(int id) {
FlightsModel rv;
SQLiteDatabase db = this.getWritableDatabase();
// Uses the query convenience method rather than raw query
Cursor csr = db.query(FLIGHTS,null,COLUMN_ID+"=?",new String[]{String.valueOf(id)},null,null,null);
if (csr.moveToFirst()) {
rv = new FlightsModel(
csr.getInt(csr.getColumnIndex(COLUMN_ID)),
csr.getString(csr.getColumnIndex(COLUMN_DESTINATION)),
csr.getDouble(csr.getColumnIndex(COLUMN_PRICE)),
csr.getString(csr.getColumnIndex(COLUMN_DEPARTURE_TIME)),
csr.getString(csr.getColumnIndex(COLUMN_ARRIVAL_TIME)),
csr.getString(csr.getColumnIndex(COLUMN_DURATION)),
csr.getInt(csr.getColumnIndex(COLUMN_AVAILABLE_SEATS))
);
} else {
rv = new FlightsModel();
}
csr.close();
// No need to close the database (inefficient to keep opening and closing db)
return rv;
}
#SuppressLint("Range")
public UsersModel getPasswordByName(String name){
UsersModel rv;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cr = db.query(USERS, null, USERNAME+"=?", new String[]{name}, null, null, null);
if (cr.moveToFirst()) {
rv = new UsersModel(
cr.getString(cr.getColumnIndex(USERNAME)),
cr.getString(cr.getColumnIndex(PASSWORD)),
cr.getString(cr.getColumnIndex(EMAIL)),
cr.getDouble(cr.getColumnIndex(BALANCE)),
cr.getInt(cr.getColumnIndex(BOOKING))
);
} else rv = new UsersModel();
cr.close();
return rv;
}
public int setBookingByUserName(int bookingAmount, String userName) {
ContentValues cv = new ContentValues();
cv.put(BOOKING,bookingAmount);
return this.getWritableDatabase().update(USERS,cv,USERNAME+"=?",new String[]{userName});
}
public double makingPayment(double balance, String userName){
ContentValues cv = new ContentValues();
cv.put(BALANCE,balance);
return this.getWritableDatabase().update(USERS,cv,USERNAME+"=?",new String[]{userName});
}
public int setAvailableSeatsAfterPayment(int seats, int flightID){
ContentValues cv = new ContentValues();
cv.put(COLUMN_AVAILABLE_SEATS,seats);
return this.getWritableDatabase().update(FLIGHTS,cv,COLUMN_ID+"=?",new String[]{String.valueOf(flightID)});
}
public int cancelBooking(int bookingAmount, String userName){
ContentValues cv = new ContentValues();
cv.put(BOOKING,bookingAmount);
return this.getWritableDatabase().update(USERS,cv,USERNAME+"=?",new String[]{String.valueOf(userName)});
}
}
The making payment method is used to update the database with a new balance when a customer books for a flight.
Here is the booking activity:
package com.example.shashank.fffffffffffffffffffffffffff;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class BookingActivity extends AppCompatActivity {
TextView textView;
TextView departure, arrival, duration, price, seats;
DBHelper dbHelper; //<<<<< ADDED
Button book, accountBtn;
FlightsModel flightsModel; //<<<<< ADDED
UsersModel userModel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_booking);
dbHelper = new DBHelper(this); //<<<<< ADDED
textView = findViewById(R.id.textView);
departure = findViewById(R.id.departure);
arrival = findViewById(R.id.arrival);
duration = findViewById(R.id.duration);
price = findViewById(R.id.price);
seats = findViewById(R.id.seats);
book = findViewById(R.id.button2);
accountBtn = findViewById(R.id.accountBtn);
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
Intent nameIntent = getIntent();
String name = nameIntent.getStringExtra("userName");
flightsModel = dbHelper.getFlightById(intValue + 1);
intValue = intValue + 1;
textView.setText(flightsModel.getDestination());
departure.setText(flightsModel.getDeparture_time());
arrival.setText(flightsModel.getArrival_time());
duration.setText(flightsModel.getDuration());
price.setText("R" + Double.toString(flightsModel.getPrice()));
seats.setText(Integer.toString(flightsModel.getAvailable_space()));
book.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int intValue, seats;
Intent mIntent = getIntent();
intValue = mIntent.getIntExtra("intVariableName", 0);
intValue = intValue + 1;
dbHelper = new DBHelper(BookingActivity.this);
flightsModel = dbHelper.getFlightById(intValue);
double price;
price = flightsModel.getPrice();
userModel = dbHelper.getPasswordByName(name);
double balance;
balance = userModel.getBalance();
String name = nameIntent.getStringExtra("userName");
seats = flightsModel.getAvailable_space();
if(seats == 0){
Toast.makeText(BookingActivity.this, "Booking unsuccessful, no available seats", Toast.LENGTH_SHORT).show();
}else{
if(price <= balance){
dbHelper.setBookingByUserName(intValue, name);
Toast.makeText(BookingActivity.this, "Payment successful, booking has been made", Toast.LENGTH_SHORT).show();
double newBalance;
newBalance = balance - price;
dbHelper.makingPayment(newBalance, name);
seats = seats - 1;
dbHelper.setAvailableSeatsAfterPayment(seats, intValue);
}else{
Toast.makeText(BookingActivity.this, "Payment unsuccessful, not enough funds", Toast.LENGTH_SHORT).show();
}
}
}
});
accountBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(BookingActivity.this, AccountActivity.class);
intent.putExtra("userName", name);
startActivity(intent);
}
});
}
}
So once a customer makes his booking a receipt file needs to be generated, say for instance with the flight details, time, date and the price of the flight. Any help will be appreciated thank you.

The following is an example that saves a file in the App's Files directory in a sub directory of receipts. The filename being composed of values e.g.
Fred-Cape Town-1200-1400
Stored at /data/user/0/the_package_name/files/receipts/Fred-Cape Town-1200-1400
The file itself, in the example containing :-
Fred
Cape Town
1200
1400
Some supportive methods added to DBHelper :-
public UsersModel getUsersModelByName(String userName) {
UsersModel rv = new UsersModel();
SQLiteDatabase db = this.getWritableDatabase();
Cursor csr = db.query(USERS,null,USERNAME+"=?", new String[]{userName},null,null,null);
if (csr.moveToFirst()) {
rv = new UsersModel(
csr.getString(csr.getColumnIndex(USERNAME)),
csr.getString(csr.getColumnIndex(PASSWORD)),
csr.getString(csr.getColumnIndex(EMAIL)),
csr.getDouble(csr.getColumnIndex(BALANCE)),
csr.getInt(csr.getColumnIndex(BOOKING))
);
}
csr.close();
return rv;
}
public boolean makeBooking(String userName, int bookingId) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(BOOKING,bookingId);
return db.update(USERS,cv,USERNAME + "=?", new String[]{userName}) > 0;
}
Now the makeBookReceiptFile method, also placed in the DBHelper class:-
public String makeBookReceiptFile(String userName, Context context) {
String rcpts_directory = "receipts";
String rv = "";
SQLiteDatabase db = this.getWritableDatabase();
UsersModel currentUser = getUsersModelByName(userName);
FlightsModel currentFlightsModel = new FlightsModel();
if (currentUser.booking > 0) {
currentFlightsModel = getFlightById(currentUser.booking);
if (currentFlightsModel.id < 1) {
rv = "INVALID - unable to extract booking for id " + currentUser.booking;
}
} else {
rv = "INVALID - unable to extract user who's name is " + userName;
}
if (rv.length() > 0) return rv;
String rcpt_filename =
currentUser.username + "-" +
currentFlightsModel.destination + "-" +
currentFlightsModel.departure + "-" +
currentFlightsModel.arrival
;
File rcpt = new File(context.getFilesDir().getPath() + File.separatorChar + rcpts_directory + File.separatorChar + rcpt_filename);
rcpt.getParentFile().mkdirs();
try {
FileWriter fw = new FileWriter(rcpt);
fw.write(userName +
"\n" + currentFlightsModel.destination+
"\n" + currentFlightsModel.departure +
"\n" + currentFlightsModel.arrival
);
fw.flush();
fw.close();
rv = rcpt.getAbsolutePath();
} catch (IOException e) {
e.printStackTrace();
rv = "IOERROR - " + e.getMessage();
}
return rv;
}
First it retrieves the UsersModel according to the UserName, the it retrieves the FlightsModel according to the booking id.
If either was not obtained then it returns a String starting with INVALID
It then obtains the Files directory via the Context.
Generates the path for the file.
Makes any missing directories.
Write data to the file, flushes and closes the file returning the path or if there was an io error the error message.
The above was tested using :-
db = new DBHelper(this);
db.insertData("Fred","password","fed#email",0.00);
UsersModel fred = db.getUsersModelByName("Fred");
db.makeBooking(fred.username,1);
Log.d("RESULTINFO",db.makeBookReceiptFile("Fred",this));
Resulting in (via Device Explorer) :-
If you have issues with the location of the file then you will have to read https://developer.android.com/training/data-storage/

Related

How to correctly display user's info among multiple users?

The command to retrieve user data doesn't display correctly, it shows the information of the last registered user.
MainActivity/login
public class MainActivity extends AppCompatActivity {
Button btnRegister, btnLogin;
EditText edtAccount, edtPassword;
Database db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnRegister = (Button) findViewById(R.id.buttonSignup);
btnRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, RegisterActivity.class);
startActivity(i);
}
});
edtAccount = (EditText) findViewById(R.id.editTextAccount);
edtPassword = (EditText) findViewById(R.id.editTextPassword);
btnLogin = (Button) findViewById(R.id.buttonLogin);
db=new Database(this);
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String account = edtAccount.getText().toString();
String password = edtPassword.getText().toString();
if (account.equals("") || password.equals("")){
Toast.makeText(MainActivity.this,
"Account and password are empty",Toast.LENGTH_SHORT).show();
}
else {
Boolean result = db.checkUserNamePassword(account, password);
if(result==true) {
Intent intent = new Intent(MainActivity.this, TestActivity.class);
intent.putExtra("account", account);
startActivity(intent);
}
else{
Boolean userCheckResult = db.checkUserName(account);
if(userCheckResult == true){
Toast.makeText(MainActivity.this,
"invalid password!", Toast.LENGTH_SHORT).show();
edtPassword.setError("invalid password!");
} else{
Toast.makeText(MainActivity.this,
"Account does not exist", Toast.LENGTH_SHORT).show();
edtAccount.setError("Account does not exist!");
}
}
}
}
});
}
}
Database
public class Database extends SQLiteOpenHelper {
public static final String TB_USER = "USER";
public static final String TB_PERSONAL = "PERSONAL";
public static final String TB_GROUPSCHEDULE = "GROUPSCHEDULE";
public static String TB_USER_ACCOUNT = "ACCOUNT";
public static String TB_USER_PASSWORD = "PASSWORD";
public static String TB_USER_ID = "USERID";
public static String TB_USER_FIRSTNAME = "USERFIRSTNAME";
public static String TB_USER_LASTNAME = "USERLASTNAME";
public static String TB_USER_PHONENUMBER = "PHONENUMBER";
public static String TB_USER_EMAIL = "EMAIL";
public static String TB_PERSONAL_ID = "PERSONALID";
public static String TB_PERSONAL_TIME = "PERSONALTIME";
public static String TB_PERSONAL_EVENT = "PERSONALEVENT";
public static String TB_GROUP_ID = "GROUPID";
public static String TB_GROUP_MEMBERS = "GROUPMEMBERS";
public static String TB_GROUP_LEADER = "GROUPLEADER";
public static String TB_GROUP_EVENT = "GROUPEVENT";
public static String TB_GROUP_TIME = "GROUPTIME";
public Database(Context context) {
super(context, "Mysche", null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
String tbUSER = " CREATE TABLE " + TB_USER + " ( "
+ TB_USER_ACCOUNT + " TEXT , "
+ TB_USER_PASSWORD + " TEXT , "
+ TB_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ TB_USER_FIRSTNAME + " TEXT, "
+ TB_USER_LASTNAME + " TEXT, "
+ TB_USER_EMAIL + " TEXT, "
+ TB_USER_PHONENUMBER + " TEXT )";
String tbPERSONAL = " CREATE TABLE " + TB_PERSONAL + " ( "
+ TB_PERSONAL_ID + " TEXT PRIMARY KEY , "
+ TB_PERSONAL_EVENT + " TEXT, "
+ TB_PERSONAL_TIME + " DATE )";
String tbGROUP = " CREATE TABLE " + TB_GROUPSCHEDULE + " ( "
+ TB_GROUP_ID + "INTEGER PRIMARY KEY , "
+ TB_GROUP_LEADER + " TEXT, "
+ TB_GROUP_MEMBERS + " TEXT, "
+ TB_GROUP_EVENT + " TEXT, "
+ TB_GROUP_TIME + " TEXT )";
db.execSQL(tbUSER);
db.execSQL(tbPERSONAL);
db.execSQL(tbGROUP);
}
#Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + TB_USER);
db.execSQL("DROP TABLE IF EXISTS " + TB_PERSONAL);
db.execSQL("DROP TABLE IF EXISTS " + TB_GROUPSCHEDULE);
onCreate(db);
}
public Boolean insertData(String ACCOUNT, String PASSWORD, String USERFIRSTNAME, String USERLASTNAME, String PHONENUMBER, String EMAIL) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("ACCOUNT", ACCOUNT);
contentValues.put("PASSWORD", PASSWORD);
contentValues.put("USERFIRSTNAME", USERFIRSTNAME);
contentValues.put("USERLASTNAME", USERLASTNAME);
contentValues.put("PHONENUMBER", PHONENUMBER);
contentValues.put("EMAIL", EMAIL);
long result = db.insert("USER", null, contentValues);
if (result == -1) {
return false;
} else {
return true;
}
}
public Boolean checkUserName(String ACCOUNT) {
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM USER WHERE ACCOUNT = ?"
, new String[]{ACCOUNT});
if (cursor.getCount() > 0) {
return true;
} else {
return false;
}
}
public Boolean checkUserNamePassword(String ACCOUNT, String PASSWORD) {
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("select * from USER where ACCOUNT = ? and PASSWORD = ?"
, new String[]{ACCOUNT, PASSWORD});
if (cursor.getCount() > 0) {
return true;
} else {
return false;
}
}
public Cursor getInfo() {
SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery(" SELECT * FROM " + TB_USER
+ " WHERE " + TB_USER_ACCOUNT, null);
return cursor;
}
}
TestActivity
public class TestActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Database database = new Database(this);
TextView textViewName = findViewById(R.id.textViewUserName);
TextView textViewEmail = findViewById(R.id.textViewUserEmail);
TextView textViewPhone = findViewById(R.id.textViewPhone);
TextView textViewAccount = findViewById(R.id.textViewAccount);
String account = getIntent().getStringExtra("account");
textViewPhone.setText(account);
Cursor cursor = database.getInfo();
while (cursor.moveToNext()){
textViewName.setText(cursor.getString(4 ) + " " +(cursor.getString(3)));
textViewPhone.setText(cursor.getString(5));
textViewEmail.setText(cursor.getString(6));
textViewAccount.setText(account);
}
}
}
I think I need to change this command:
public Cursor getInfo() {
SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery(" SELECT * FROM " + TB_USER
+ " WHERE " + TB_USER_ACCOUNT, null);
return cursor;
}
to:
public Cursor getInfo() {
SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery(" SELECT * FROM " + TB_USER
+ " WHERE " + TB_USER_ACCOUNT + "=" + loggingAccount();, null);
return cursor;
}
But I don't know how to create and insert that loggingAccount() function. I was able to display username by getIntent() in TestActivity.
String account = getIntent().getStringExtra("account");
textViewPhone.setText(account);
Or if anyone knows another way please guide me.
You could create an overloaded method getInfo(String acct) and invoke as follows:
Cursor cursor = database.getInfo(account);
and define it as such:
public Cursor getInfo(String account) {
SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery(" SELECT * FROM " + TB_USER
+ " WHERE " + TB_USER_ACCOUNT + " = '" + account + "'", null);
return cursor;
}
An overloaded method means methods within a class can have the same name (e.g. getInfo) but differ in method signature (parameters). And of course their implementation can differ.
Since your query is formed using data from user input you should use prepared statements. Check out PreparedStatement for more discussion on that.

Update method not saving changes Sqlite Database (Android Studio)

I'm trying to complete my update method on my database, but I'm not getting the results.
My app doesn't show any error, I can access the object I want to edit, I'm able to change the data, but the changes are not saved.
I've tried this code in another app and worked ok, but now I'm not able to save my changes when I edit the data I want to alter.
Can anyone please give a hand?
My Database:
package com.myapplication.umdocededaisy;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MyDatabase extends SQLiteOpenHelper {
List<MateriaPrima> listaProduto = new ArrayList<>();
private final Context context;
private static final String DATABASE_NAME = "BancoDoceDaisy.db";
private static final int DATABASE_VERSION = 4;
//Estruturas das Tabelas do banco de dados:
//Tabela dos produtos - materia prima:
private static final String TABLE_PRODUTO = "materia_prima";
private static final String COLUMN_CODIGO = "codigo";
private static final String COLUMN_PRODUTO = "produto";
private static final String COLUMN_VALOR = "valor";
private static final String COLUMN_QTD = "quantidade";
private static final String COLUMN_TIPO = "tipo";
//------------------------------------------------------
MyDatabase(Context context) {
super(context, DATABASE_NAME,null, DATABASE_VERSION);
this.context = context;
}
#Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE "+ TABLE_PRODUTO +
" (" + COLUMN_CODIGO + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_PRODUTO + " TEXT, " +
COLUMN_VALOR + " FLOAT, " +
COLUMN_QTD + " FLOAT, " +
COLUMN_TIPO + " TEXT); ";
db.execSQL(query);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUTO);
onCreate(db);
}
void addMateriaPrima(MateriaPrima materiaPrima) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN_PRODUTO, materiaPrima.getProduto());
cv.put(COLUMN_VALOR, materiaPrima.getValor());
cv.put(COLUMN_QTD, materiaPrima.getQuantidade());
cv.put(COLUMN_TIPO, materiaPrima.getTipo());
long result = db.insert(TABLE_PRODUTO, null, cv);
if (result == -1) {
Toast.makeText(context, R.string.strFailed, Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(context, R.string.strAddSucess, Toast.LENGTH_SHORT).show();
}
db.close();
}
Cursor readAllData(){
String query = "SELECT * FROM " + TABLE_PRODUTO;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = null;
if(db != null){
cursor = db.rawQuery(query,null);
}
return cursor;
}
public List<MateriaPrima> buscaProduto() {
String columns[] = {COLUMN_CODIGO, COLUMN_PRODUTO, COLUMN_VALOR, COLUMN_QTD, COLUMN_TIPO};
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(TABLE_PRODUTO, columns, null, null, null,null, null);
while (cursor.moveToNext()) {
int index1 = cursor.getColumnIndex(COLUMN_CODIGO);
int codigo = cursor.getInt(index1);
int index2 = cursor.getColumnIndex(COLUMN_PRODUTO);
String produto = cursor.getString(index2);
int index3 = cursor.getColumnIndex(COLUMN_VALOR);
float valor = cursor.getFloat(index3);
int index4 = cursor.getColumnIndex(COLUMN_QTD);
float quantidade = cursor.getFloat(index4);
int index5 = cursor.getColumnIndex(COLUMN_TIPO);
String tipo = cursor.getString(index5);
MateriaPrima produtos = new MateriaPrima(codigo, produto, valor, quantidade, tipo);
listaProduto.add(produtos);
}
return listaProduto;
}
void updateData(MateriaPrima materiaPrima) {
SQLiteDatabase db = getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN_PRODUTO, materiaPrima.getProduto());
cv.put(COLUMN_VALOR, materiaPrima.getValor());
cv.put(COLUMN_QTD, materiaPrima.getQuantidade());
cv.put(COLUMN_TIPO, materiaPrima.getTipo());
long result = db.update(TABLE_PRODUTO, cv, " codigo=?", new String[]{String.valueOf(materiaPrima.getCodigo())});
if (result == -1) {
Toast.makeText(context, R.string.strFailed, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, R.string.strSucess, Toast.LENGTH_SHORT).show();
}
db.close();
}
void deleteOneRow(String materiaPrima) {
SQLiteDatabase db = this.getWritableDatabase();
long result = db.delete(TABLE_PRODUTO, COLUMN_CODIGO + " codigo", new String[]{String.valueOf(materiaPrima)});
if (result == -1) {
Toast.makeText(context, R.string.strFailed, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, R.string.strSucess, Toast.LENGTH_SHORT).show();
}
db.close();
}
void deleteAllData() {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_PRODUTO);
db.close();
}
}
My Update Activity
package com.myapplication.umdocededaisy;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class UpdateActivity extends AppCompatActivity {
EditText editCodigo2, editProduto2, editValor2, editQuantidade2, editTipo2;
Button btnUpdate, btnDelete;
String codigo, produto, valor, quantidade, tipo;
InputMethodManager inputManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update);
//Declarações objetos:
editCodigo2 = findViewById(R.id.editCodigo2);
editProduto2 = findViewById(R.id.editProduto2);
editValor2 = findViewById(R.id.editValor2);
editQuantidade2 = findViewById(R.id.editQuantidade2);
editTipo2 = findViewById(R.id.editTipo2);
btnUpdate = findViewById(R.id.btnUpdate);
btnDelete = findViewById(R.id.btnDelete);
inputManager = (InputMethodManager) getSystemService (Context.INPUT_METHOD_SERVICE);
getAndSetIntentData();
ActionBar ab = getSupportActionBar();
if(ab != null){
ab.setTitle(produto);
}
btnUpdate.setOnClickListener(v -> {
MateriaPrima materiaPrima = new MateriaPrima();
MyDatabase myDB = new MyDatabase(UpdateActivity.this);
produto = editProduto2.getText().toString().trim();
valor = editValor2.getText().toString().trim();
quantidade = editQuantidade2.getText().toString().trim();
tipo = editTipo2.getText().toString().trim();
myDB.updateData(materiaPrima);
inputManager.hideSoftInputFromWindow(btnUpdate.getWindowToken(), 0);
recreate();
});
btnDelete.setOnClickListener(v -> confirmDeleteDialog());
}
void getAndSetIntentData() {
if (getIntent().hasExtra("codigo") && getIntent().hasExtra("produto") && getIntent().hasExtra("valor") &&
getIntent().hasExtra("quantidade") && getIntent().hasExtra("tipo")){
//Getting data:
codigo = getIntent().getStringExtra("codigo");
produto = getIntent().getStringExtra("produto");
valor = getIntent().getStringExtra("valor");
quantidade = getIntent().getStringExtra("quantidade");
tipo = getIntent().getStringExtra("tipo");
//Setting data:
editCodigo2.setText(codigo);
editProduto2.setText(produto);
editValor2.setText(valor);
editQuantidade2.setText(quantidade);
editTipo2.setText(tipo);
}else{
Toast.makeText(this, R.string.strData0, Toast.LENGTH_SHORT).show();
}
}
void confirmDeleteDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.strMsgDelete) + " " + produto);
builder.setMessage(getString(R.string.strMsgDelete ) + " " + produto + " ?");
builder.setPositiveButton(getString(R.string.strYes), (dialog, which) -> {
MyDatabase myDB = new MyDatabase(UpdateActivity.this);
myDB.deleteOneRow(codigo);
finish();
});
builder.setNegativeButton(getString(R.string.strNo), (dialog, which) -> {
});
builder.create().show();
}
}
Maybe you think that the table is not updated because you see the toast with the message that the update failed.
But, the condition that you check in order to show the correct message is not correct.
The method update() returns either the number of the rows affected (updated) or 0 if the update failed.
It never returns -1.
Change your code like this:
if (result == 0) {
Toast.makeText(context, R.string.strFailed, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, R.string.strSucess, Toast.LENGTH_SHORT).show();
}
Also, in the listener of btnUpdate you define a MateriaPrima object which you pass to updateData() without setting its properties.
Where do you get the Codigo property from?
Change to this:
btnUpdate.setOnClickListener(v -> {
MateriaPrima materiaPrima = new MateriaPrima();
MyDatabase myDB = new MyDatabase(UpdateActivity.this);
materiaPrima.setCodigo(?????); // you must set this property
materiaPrima.setProduto(editProduto2.getText().toString().trim());
materiaPrima.setValor(editValor2.getText().toString().trim());
materiaPrima.setQuantidade(editQuantidade2.getText().toString().trim());
materiaPrima.setTipo(editTipo2.getText().toString().trim());
myDB.updateData(materiaPrima);
inputManager.hideSoftInputFromWindow(btnUpdate.getWindowToken(), 0);
recreate();
});
I was able to correct the error by rewriting my update method:
void updateData(String row_id, String produto, String valor, String quantidade, String tipo){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN_PRODUTO, produto);
cv.put(COLUMN_VALOR, valor);
cv.put(COLUMN_QTD, quantidade);
cv.put(COLUMN_TIPO, tipo);
long result = db.update(TABLE_PRODUTO, cv, "codigo=?", new String[]{row_id});
if(result == -1){
Toast.makeText(context, R.string.strFailed, Toast.LENGTH_SHORT).show();
} else{
Toast.makeText(context, R.string.strSucess, Toast.LENGTH_SHORT).show();
}
db.close();
}
Thanks for all the help

Android Studio SQLite table duplicates everytime I click view button

I have added 2 entries in the database and if I click the view button for the first time, it normally shows the 2 entries. However, if I click the view button again, there will be 4 entries and then 6, 8, 10 etc. Even though there are 4,6,8,10,12 entries if I view it, I used a getCount command and it showed 2 which is correct. So the database is correct so the bug here is somewhere in the viewbtn but I don't know what it is. How do I fix this?
MainActivity.java
viewbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
res = myDb.getAllData();
if(res.getCount()==0){ //when there is no data show message..error message is depend on "showMessage"method
//show message
showMessage("Oops!","No entry, add an entry to see the list.");
return;
}
while(res.moveToNext()) {
// read and collect data in database in column
buffer.append("\n\n");
buffer.append("Date: " + res.getString(0) + "\n");
buffer.append("Time: " + res.getString(1) + "\n");
buffer.append("Name: " + res.getString(2) + "\n");
buffer.append("Surname: " + res.getString(3) + "\n");
buffer.append("ID: " + res.getString(4) + "\n");
buffer.append("--------------------------------------------------------------");
}
//show all data
showMessage("Entries",buffer.toString()); //show all data in list.list is depnd on "showMessage"method
}
});
DatabaseHelper.java
package com.example.AppDraft3;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DatabaseHelper extends SQLiteOpenHelper {
private Context context;
public static final String DATABASE_NAME="Attendance.db";
public static final String TABLE_NAME="Attendance_table";
public static final String COL_1="DATE";
public static final String COL_2="TIME";
public static final String COL_3="NAME";
public static final String COL_4="SURNAME";
public static final String COL_5="ID";
private SQLiteDatabase db;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
this.context = context;
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME +" (DATE TEXT,TIME TEXT,NAME TEXT,SURNAME TEXT,ID INTEGER)");
}
#Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
}
public Boolean verifyData(String TABLE_NAME, String id){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + TABLE_NAME + " WHERE id = '" + id + "'";
Cursor res = db.rawQuery(query, null);
if (res.moveToFirst()){
res.close();
return true;
} else {
res.close();
return false;
}
}
public long insertData(String name,String surname,String id){
if (verifyData(TABLE_NAME, id)){
return 0;
}
// set the format to sql date time
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
Date date = new Date();
Date time = new Date();
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1, dateFormat.format(date));
contentValues.put(COL_2, timeFormat.format(time));
contentValues.put(COL_3, name);
contentValues.put(COL_4, surname);
contentValues.put(COL_5, id);
long result=db.insert(TABLE_NAME, null, contentValues);
return result;
}
public boolean verifyExist(String id){
if (verifyData(TABLE_NAME, id)){
return true;
} else {
return false;
}
}
public Cursor getAllData(){
//get all data
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select*from "+TABLE_NAME, null);
return res;
}
public Integer deleteData(){
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME,null,null);
}
public Boolean singleDeleteData(String id){
SQLiteDatabase db = this.getWritableDatabase();
Cursor res =db.rawQuery("Select * from " +TABLE_NAME+ " where id = ?", new String[] {id});
if (res.getCount() > 0) {
long resultDel = db.delete(TABLE_NAME, "id=?", new String[] {id});
if (resultDel == -1) {
return false;
} else {
return true;
}
} else {
return false;
}
}
}
The buffer needs to be empties otherwise you are just appending to it. I am assuming this is a StringBuffer
public void onClick(View v) {
res = myDb.getAllData();
buffer = new StringBuffer();
:
:
}

Why my page automatic close when I login page in SQLite?

My problems : What I suppose to do?
1. Below is my error in logcat
2. Why my data doesn't enter in my SQLite database when I open it in SQLite browser?
3. Almost all the method I make a research and use, but all of them cannot use it.
My connection SQLite database : Database.java
package com.example.projectvote;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import static androidx.constraintlayout.widget.Constraints.TAG;
public class Database extends SQLiteOpenHelper {
private static final String TAG = "Database";
public static final String DbName = "SignUp.db";
public static final String TbName = "SignUp";
public static final String Col1 = "Num";
public static final String Col2 = "ID";
public static final String Col3 = "Password";
public static final String TbName1 = "candidate";
public static final String Col4 = "Nom";
public static final String Col5 = "Candidates";
public Database(Context context) {
super(context, DbName, null, 1);
}
#Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("CREATE TABLE SignUp (Num INT PRIMARY KEY AUTOINCREMENT, ID TEXT, Password TEXT)");
sqLiteDatabase.execSQL("CREATE TABLE candidate (Nom INT PRIMARY KEY AUTOINCREMENT, Candidates TEXT)");
}
#Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL(" DROP TABLE IF EXISTS " + TbName);
sqLiteDatabase.execSQL(" DROP TABLE IF EXISTS " + TbName1);
onCreate(sqLiteDatabase);
}
public long addUser(String ID, String password)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("ID", ID);
cv.put("password", password);
long res = db.insert("SignUp", null, cv);
db.close();
return res;
}
public boolean checkUser(String ID, String password)
{
String[] columns = { Col2 };
SQLiteDatabase db = getReadableDatabase();
String selection = Col2 + "=?" + "and" + Col3 + "=?";
String[] selectionArgs = { ID, password };
Cursor c = db.query(TbName, columns, selection, selectionArgs, null, null, null);
int count = c.getCount();
c.close();
db.close();
if (count > 0)
return true;
else
return false;
}
public Cursor readData(SQLiteDatabase db) {
String[] cols = { Col5 };
Cursor c = db.query(TbName1, cols, null, null, null, null, null);
return c;
}
public boolean addCandidate(String Candidates)
{
SQLiteDatabase db = this. getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(Col5, Candidates);
Log.d(TAG, "addCandidate : Adding " + Candidates + " to " + TbName1);
long res = db.insert(TbName1, null, cv);
if(res == -1)
{
return false;
}
else
{
return true;
}
}
public Cursor getItemID(String Candidates)
{
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT " + Col4 + " FROM " + TbName1 + " WHERE " + Col5 + " = " + Candidates + "'";
Cursor data = db.rawQuery(query, null);
return data;
}
public void updateCandidate(String newCandidate, int ID, String oldCandidate)
{
SQLiteDatabase db = this.getWritableDatabase();
String query = "UPDATE " + TbName1 + " SET " + Col5 + "= '" + newCandidate + "' WHERE " + Col5 + "= '" + oldCandidate + "'";
Log.d(TAG, "updateCandidate: query: " + query);
Log.d(TAG, "updateCandidate: Setting Candidate to " + newCandidate);
db.execSQL(query);
}
public void deleteCandidate(int ID, String Candidate)
{
SQLiteDatabase db = this.getWritableDatabase();
String query = "DELETE FROM " + TbName1 + " WHERE " + Col5 + "= '" + Candidate + "'";
Log.d(TAG, "deleteCandidate: query: " + query);
Log.d(TAG, "deleteCandidate: Deleting: " + Candidate + " from database ");
db.execSQL(query);
}
public Cursor getListContent1() {
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + TbName1;
Cursor data = db.rawQuery(query, null);
return data;
}
}
My login java : Login.java
package com.example.projectvote;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class Login extends AppCompatActivity
{
EditText ID, password;
Database db;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
db = new Database(this);
ID = findViewById(R.id.etID);
password = findViewById(R.id.etPassword);
}
public void OnLog(View view)
{
String Id = ID.getText().toString().trim();
String Password = password.getText().toString().trim();
Boolean res = db.checkUser(Id, Password);
if (res == true)
{
startActivity(new Intent(getApplicationContext(), Home.class));
}
else if ((ID.equals("Admin") && Password.equals("Admin2019")))
{
startActivity(new Intent(getApplicationContext(), Admin.class));
}
else
{
Toast.makeText(Login.this, "Sorry, Login Error", Toast.LENGTH_SHORT).show();
}
}
public void OnReg(View view) {
startActivity(new Intent(getApplicationContext(), SignUp.class));
}
}
Update your queries in your onCreate() of Database.java like following
sqLiteDatabase.execSQL("CREATE TABLE SignUp (Num INTEGER PRIMARY KEY AUTOINCREMENT, ID TEXT, Password TEXT)");
sqLiteDatabase.execSQL("CREATE TABLE candidate (Num INTEGER PRIMARY KEY AUTOINCREMENT, Candidates TEXT)");

How to Figure out the MAX and MIN of Groups that have been Added together

How do would you get the MAX value of a column that has been grouped and add together by _id.
So something like this:
I then want to display the MAX value of the four groups, as well as the MIN value of the four groups.
Something like this Scratch High = 702 / Scratch Low = 325
Is this possible with the built in Math functions of SQLite or would I need to write specific code to accomplish this? The actual number of groups will be more than 4, it will depend on how often the bowler actually bowls a series.
I haven't written any code for this as of yet, I am attempting to figure out if this is even possible before attempting to do so. Any suggestions would be welcome.
My attempt to integrate into my Project:
DatabaseHelper.java
public static final String DERIVEDCOL_MAXSCORE = "max_score";
public static final String DERIVEDCOl_MINSCORE = "min_score";
public Cursor getMaxMinScoresAllAndroid() {
SQLiteDatabase db = this.getWritableDatabase();
String tmptbl = "summed_scores";
String tmptblcol = "sum_score";
String crttmptbl = "CREATE TEMP TABLE IF NOT EXISTS " + tmptbl + "(" +
tmptblcol + " INTEGER" +
")";
String empttmptbl = "DELETE FROM " + tmptbl;
db.execSQL(crttmptbl);
db.execSQL(empttmptbl);
String[] columns = new String[]{"sum(score) AS " + tmptblcol};
Cursor csr = db.query(Game.TABLE_NAME,columns,null,null,Game.COLUMN_BOWLER_ID,null,null);
DatabaseUtils.dumpCursor(csr);
while (csr.moveToNext()) {
ContentValues cv = new ContentValues();
cv.put(tmptblcol,csr.getInt(csr.getColumnIndex(tmptblcol)));
db.insert(tmptbl,null,cv);
}
csr.close();
columns = new String[]{"max(" +
tmptblcol +
") AS " + DERIVEDCOL_MAXSCORE,
"min(" +
tmptblcol +
") AS " + DERIVEDCOl_MINSCORE};
return csr = db.query(tmptbl,columns,null,null,null,null,null);
}
public MaxMin getMaxAndminScoresAll() {
MaxMin rv = new MaxMin(0,0);
Cursor csr = getMaxMinScoresAllAndroid();
if (csr.moveToFirst()) {
rv.setMin(csr.getInt(csr.getColumnIndex(DERIVEDCOl_MINSCORE)));
rv.setMax(csr.getInt(csr.getColumnIndex(DERIVEDCOL_MAXSCORE)));
}
csr.close();
return rv;
}
BowlerProfileViewActivity.java
public class BowlerProfileViewActivity extends AppCompatActivity {
Bowler bowler;
private DatabaseHelper db;
private static final String PREFS_NAME = "prefs";
private static final String PREF_BLUE_THEME = "blue_theme";
private static final String PREF_GREEN_THEME = "green_theme";
private static final String PREF_ORANGE_THEME = "purple_theme";
private static final String PREF_RED_THEME = "red_theme";
private static final String PREF_YELLOW_THEME = "yellow_theme";
#Override
protected void onResume() {
super.onResume();
db = new DatabaseHelper(this);
//mAdapter.notifyDatasetChanged(db.getAllLeagues());
}
#Override
protected void onCreate(Bundle savedInstanceState) {
//Use Chosen Theme
SharedPreferences preferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
boolean useBlueTheme = preferences.getBoolean(PREF_BLUE_THEME, false);
if (useBlueTheme) {
setTheme(R.style.AppTheme_Blue_NoActionBar);
}
boolean useGreenTheme = preferences.getBoolean(PREF_GREEN_THEME, false);
if (useGreenTheme) {
setTheme(R.style.AppTheme_Green_NoActionBar);
}
boolean useOrangeTheme = preferences.getBoolean(PREF_ORANGE_THEME, false);
if (useOrangeTheme) {
setTheme(R.style.AppTheme_Orange_NoActionBar);
}
boolean useRedTheme = preferences.getBoolean(PREF_RED_THEME, false);
if (useRedTheme) {
setTheme(R.style.AppTheme_Red_NoActionBar);
}
boolean useYellowTheme = preferences.getBoolean(PREF_YELLOW_THEME, false);
if (useYellowTheme) {
setTheme(R.style.AppTheme_Yellow_NoActionBar);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bowler_profile_view);
Intent intent = getIntent();
Boolean shouldUpdate = getIntent().getExtras().getBoolean("shouldUpdate");
String savedLeagueId = intent.getStringExtra("leagueId");
String savedBowlerId = String.valueOf(getIntent().getIntExtra("bowlerId",2));
int bowlerId = Integer.valueOf(savedBowlerId);
getBowlerProfile(savedLeagueId, bowlerId);
// Get The min and max score
MaxMin bowlerMaxMin = db.getMaxAndminScoresAll();
Log.d("SCORES",
"\n\tMaximum Score is " + String.valueOf(bowlerMaxMin.getMax()) +
"\n\tMinimum Score is " + String.valueOf(bowlerMaxMin.getMin()));
}
public void getBowlerProfile(String savedLeagueId, int savedBowlerId) {
String bn, ba, bh;
SQLiteOpenHelper database = new DatabaseHelper(this);
SQLiteDatabase db = database.getReadableDatabase();
Cursor viewBowlerProfile = db.query( Bowler.TABLE_NAME,
new String[]{Bowler.COLUMN_ID, Bowler.COLUMN_LEAGUE_ID, Bowler.COLUMN_NAME, Bowler.COLUMN_BOWLER_AVERAGE, Bowler.COLUMN_BOWLER_HANDICAP, Bowler.COLUMN_TIMESTAMP},
Bowler.COLUMN_ID + "=?",
new String[]{String.valueOf(savedBowlerId)}, null, null, null, null);
if (viewBowlerProfile.moveToFirst()) {
//Prepare League Object
bowler = new Bowler(
viewBowlerProfile.getInt(viewBowlerProfile.getColumnIndex(Bowler.COLUMN_ID)),
viewBowlerProfile.getString(viewBowlerProfile.getColumnIndex(Bowler.COLUMN_LEAGUE_ID)),
bn = viewBowlerProfile.getString(viewBowlerProfile.getColumnIndex(Bowler.COLUMN_NAME)),
ba = viewBowlerProfile.getString(viewBowlerProfile.getColumnIndex(Bowler.COLUMN_BOWLER_AVERAGE)),
bh = viewBowlerProfile.getString(viewBowlerProfile.getColumnIndex(Bowler.COLUMN_BOWLER_HANDICAP)),
viewBowlerProfile.getString(viewBowlerProfile.getColumnIndex(Bowler.COLUMN_TIMESTAMP)));
final TextView bowlerName = (TextView) findViewById(R.id.tvBowlerName);
final TextView bowlerAverage = (TextView) findViewById(R.id.tvBowlerAverageValue);
final TextView bowlerHandicap = (TextView) findViewById(R.id.tvBowlerHandicapValue);
bowlerName.setText(String.valueOf(bn));
bowlerAverage.setText(String.valueOf(ba));
bowlerHandicap.setText(String.valueOf(bh));
//Close Database Connection
viewBowlerProfile.close();
}
//View League Profile Cancel Button
final Button cancel_button = (Button) findViewById(R.id.bCancel);
cancel_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("SAVEDLEAGUEID_VAL", ">>" + String.valueOf(savedLeagueId) + "<<");
Intent intent = new Intent(getApplicationContext(), BowlerActivity.class);
intent.putExtra("leagueId", savedLeagueId);
startActivity(intent);
finish();
overridePendingTransition(0, 0);
/*Intent intent = new Intent(getApplicationContext(), BowlerActivity.class);
intent.putExtra("leagueId", savedLeagueId);
Log.d("LEAGUEID VALUE","value of leagueId = " + String.valueOf(savedLeagueId));
startActivity(intent);
finish();
overridePendingTransition(0, 0);*/
}
});
//Edit League Profile Cancel Button
final Button edit_button = (Button) findViewById(R.id.bEdit);
edit_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int bowlerId = bowler.getId();
Intent intent = new Intent(getApplicationContext(), BowlerProfileEditActivity.class);
intent.putExtra("bowlerId", bowlerId);
intent.putExtra("leagueId", savedLeagueId);
startActivity(intent);
finish();
overridePendingTransition(0, 0);
}
});
}
}
Logcat
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'ca.rvogl.tpbcui.utils.MaxMin ca.rvogl.tpbcui.database.DatabaseHelper.getMaxAndminScoresAll()' on a null object reference
at ca.rvogl.tpbcui.views.BowlerProfileViewActivity.onCreate(BowlerProfileViewActivity.java:79)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
Attempting to group by bowlerId and seriesId
public Cursor getMaxMinScoresAllAndroid(String bowlerId) {
SQLiteDatabase db = this.getWritableDatabase();
String tmptbl = "summed_scores";
String tmptblcol = "sum_score";
String tmpBowlerIdCol = "bowler_id";
String tmpSeriesIdCol = "series_id";
String crttmptbl = "CREATE TEMP TABLE IF NOT EXISTS " + tmptbl + "(" +
tmptblcol + " INTEGER," +
tmpBowlerIdCol + " TEXT," +
tmpSeriesIdCol + " TEXT)";
String empttmptbl = "DELETE FROM " + tmptbl;
db.execSQL(crttmptbl);
db.execSQL(empttmptbl);
String[] columns = new String[]{"sum(score) AS "};
Cursor csr = db.query(Game.TABLE_NAME,columns,null,null,Game.COLUMN_BOWLER_ID + " = '" + bowlerId + "'", Game.COLUMN_SERIES_ID,null,null);
DatabaseUtils.dumpCursor(csr);
while (csr.moveToNext()) {
ContentValues cv = new ContentValues();
cv.put(tmptblcol,csr.getInt(csr.getColumnIndex(tmptblcol)));
cv.put(tmpBowlerIdCol,csr.getInt(csr.getColumnIndex(tmpBowlerIdCol)));
cv.put(tmpSeriesIdCol,csr.getInt(csr.getColumnIndex(tmpSeriesIdCol)));
db.insert(tmptbl,null,cv);
}
csr.close();
columns = new String[]{"max(" +
tmptblcol +
") AS " + DERIVEDCOL_MAXSCORE,
"min(" +
tmptblcol +
") AS " + DERIVEDCOl_MINSCORE};
return csr = db.query(tmptbl,columns,null,null,null,null,null);
}
public MaxMin getMaxAndminScoresAll(String bowlerId) {
MaxMin rv = new MaxMin(0,0);
Cursor csr = getMaxMinScoresAllAndroid(bowlerId);
if (csr.moveToFirst()) {
rv.setMin(csr.getInt(csr.getColumnIndex(DERIVEDCOl_MINSCORE)));
rv.setMax(csr.getInt(csr.getColumnIndex(DERIVEDCOL_MAXSCORE)));
}
csr.close();
return rv;
}
You could do this (assuming the table is named myscores and the columns are _id and score) using :-
WITH cte1 AS
(
sum(score) AS sum_score
FROM myscores
GROUP BY _id
)
SELECT max(sum_score) AS min_score, min(sum_score) FROM cte1;
Using this would result in the following :-
Notes
AS is used to rename the output columns
without renaming the columns they would be named max(sum_score) and min(sum_score) respectively.
SQL As Understood By SQLite - Aggregate Functions
This utilises the SQLite aggregate functions max and min and the GROUP BY clause to aggregate the columns according to the _id column.
This also utilises a Common Table Expression (an intermediate/temporary table).
SQL As Understood By SQLite - WITH clause
Incorporating into an Android App (see note at end)
The following is an example App demonstrates how this can be incorporated fro Android :-
First a simple class for the Minimum and maximum vales (as used in the alternative getMaxAndMinScores method)
MaxMin.java (optional)
public class MaxMin {
private int min;
private int max;
public MaxMin(int min, int max) {
this.min = min;
this.max = max;
}
public int getMin() {
return min;
}
public void setMin(int min) {
this.min = min;
}
public int getMax() {
return max;
}
public void setMax(int max) {
this.max = max;
}
}
DBHelper.java
The subclass of SQLiteOpenHelper (just the one table name myscores)
public class DBHelper extends SQLiteOpenHelper {
public static final String DBNAME = "mydb";
public static final int DBVERSION = 1;
public static final String TB_SCORE = "myscores";
public static final String COL_SCORE = "score";
public static final String COL_ID = BaseColumns._ID;
public static final String DERIVEDCOL_MAXSCORE = "max_score";
public static final String DERIVEDCOl_MINSCORE = "min_score";
private static final String crt_myscores_sql = "CREATE TABLE IF NOT EXISTS " + TB_SCORE + "(" +
COL_ID + " INTEGER," +
COL_SCORE + " INTEGER" +
")";
public DBHelper(Context context) {
super(context, DBNAME, null, DBVERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(crt_myscores_sql);
}
#Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
}
public long addScore(long id, int score) {
ContentValues cv = new ContentValues();
cv.put(COL_ID,id);
cv.put(COL_SCORE,score);
return this.getWritableDatabase().insert(TB_SCORE,null,cv);
}
public Cursor getMaxMinScores() {
String sum_score = "sum_score";
String cte1 = "cte1";
String rawqry = " WITH " + cte1 +
" AS " +
"(" +
"SELECT sum(" +
COL_SCORE +
") AS " + sum_score +
" FROM " + TB_SCORE + " GROUP BY " + COL_ID +
") " +
"SELECT " +
" max(" +
sum_score +
") AS " + DERIVEDCOL_MAXSCORE +
"," +
" min(" +
sum_score +
") AS " + DERIVEDCOl_MINSCORE +
" FROM " + cte1 + ";";
return this.getWritableDatabase().rawQuery(rawqry,null);
}
public MaxMin getMaxAndMinScores() {
MaxMin rv = new MaxMin(0,0);
Cursor csr = getMaxMinScores();
if (csr.moveToFirst()) {
rv.setMin(csr.getInt(csr.getColumnIndex(DERIVEDCOl_MINSCORE)));
rv.setMax(csr.getInt(csr.getColumnIndex(DERIVEDCOL_MAXSCORE)));
}
csr.close();
return rv;
}
}
MainActivity.java
The activity that a) adds some rows and then b) gets the maximum and minimum scores (twice using alternative methods) :-
public class MainActivity extends AppCompatActivity {
DBHelper mDBHlpr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDBHlpr = new DBHelper(this);
// Add Some scores
mDBHlpr.addScore(1,112);
mDBHlpr.addScore(1,123);
mDBHlpr.addScore(1,144);
mDBHlpr.addScore(2,212);
mDBHlpr.addScore(2,190);
mDBHlpr.addScore(2,300);
mDBHlpr.addScore(3,234);
mDBHlpr.addScore(3,134);
mDBHlpr.addScore(3,122);
mDBHlpr.addScore(4,100);
mDBHlpr.addScore(4,111);
mDBHlpr.addScore(4,114);
// Get The min and max scores example 1
Cursor csr = mDBHlpr.getMaxMinScores();
if (csr.moveToFirst()) {
int max_score = csr.getInt(csr.getColumnIndex(DBHelper.DERIVEDCOL_MAXSCORE));
int min_score = csr.getInt(csr.getColumnIndex(DBHelper.DERIVEDCOl_MINSCORE));
Log.d("SCORES",
"\n\tMaximum Score is " + String.valueOf(max_score) +
"\n\tMinimum Score is " + String.valueOf(min_score)
);
}
//Alternative utilising the MaxMin object
MaxMin mymaxmin = mDBHlpr.getMaxAndMinScores();
Log.d("SCORES",
"\n\tMaximum Score is " + String.valueOf(mymaxmin.getMax()) +
"\n\tMinimum Score is " + String.valueOf(mymaxmin.getMin())
);
}
}
IMPORTANT
The WITH clause was introduced in SQL 3.8.3, some older versions of Android (below lollipop (but can be device independent)) will not
support the WITH clause.
The following methods, as equivalents of getMaxMinScores and getMaxAndMinScores could be used for any Android version :-
public Cursor getMaxMinScoresAllAndroid() {
SQLiteDatabase db = this.getWritableDatabase();
String tmptbl = "summed_scores";
String tmptblcol = "sum_score";
String crttmptbl = "CREATE TEMP TABLE IF NOT EXISTS " + tmptbl + "(" +
tmptblcol + " INTEGER" +
")";
String empttmptbl = "DELETE FROM " + tmptbl;
db.execSQL(crttmptbl);
db.execSQL(empttmptbl);
String[] columns = new String[]{"sum(score) AS " + tmptblcol};
Cursor csr = db.query(TB_SCORE,columns,null,null,COL_ID,null,null);
DatabaseUtils.dumpCursor(csr);
while (csr.moveToNext()) {
ContentValues cv = new ContentValues();
cv.put(tmptblcol,csr.getInt(csr.getColumnIndex(tmptblcol)));
db.insert(tmptbl,null,cv);
}
csr.close();
columns = new String[]{"max(" +
tmptblcol +
") AS " + DERIVEDCOL_MAXSCORE,
"min(" +
tmptblcol +
") AS " + DERIVEDCOl_MINSCORE};
return csr = db.query(tmptbl,columns,null,null,null,null,null);
}
public MaxMin getMaxAndminScoresAllAndroid() {
MaxMin rv = new MaxMin(0,0);
Cursor csr = getMaxMinScoresAllAndroid();
if (csr.moveToFirst()) {
rv.setMin(csr.getInt(csr.getColumnIndex(DERIVEDCOl_MINSCORE)));
rv.setMax(csr.getInt(csr.getColumnIndex(DERIVEDCOL_MAXSCORE)));
}
csr.close();
return rv;
}
These utilise an intermediate temporary table so bypass the restriction of using WITH

Categories

Resources