I am making an app where I input a name and a number, and upon clicking SAVE, the name is saved in an SQLITE database named infinitecounter.db under the COUNTER_NAME column. After that, it saves the number in the CURRENT_CLICKS column. However, when I launch my app, and upon clicking the save button. the app crashes.
I then exported the database from the files and opened it using SQLite browser and this is what I saw: SQLITE browser
Here is my DATABASE code:
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;
import androidx.annotation.Nullable;
class database1 extends SQLiteOpenHelper {
private Context context;
private static final String DATABASE_NAME= "infinitecounter.db";
private static final int DATABASE_VERSION= 1;
private static final String TABLE_NAME = "infinitecounter list";
private static final String COLUMN_ID = "id";
private static final String COUNTER_NAME = "title";
private static final String CURRENT_CLICKS = ("Current Clicks"); // fix later
public database1(@Nullable Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
String query =
"CREATE TABLE " + TABLE_NAME +
"(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COUNTER_NAME + " TEXT, " +
CURRENT_CLICKS + " INTEGER);";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
void addinfincounter(String title, int current_clicks){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COUNTER_NAME, title);
cv.put(CURRENT_CLICKS, current_clicks);
long results = db.insert(TABLE_NAME, null, cv);
if(results == -1){
Toast.makeText(context, "FAILED!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "SUCCESS!", Toast.LENGTH_SHORT).show();
}
}
}
Here is the java page where I activate the addinfincounter method
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;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
public class infincounter_setup extends AppCompatActivity {
EditText name, clicks;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_infincounter_setup);
}
public void cancel(View V){
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
}
public void save(View B) {
name = findViewById(R.id.infincounter_name);
clicks = findViewById(R.id.clicks);
if (name.length() == 0 || name == null) {
Toast.makeText(this, "PLEASE ASSIGN A NAME", Toast.LENGTH_LONG).show();
} else{
database1 myDB = new database1(infincounter_setup.this);
myDB.addinfincounter(name.getText().toString(), Integer.parseInt(clicks.getText().toString()));
}
}
}