0

I'm to pass a circle object into a sqLiteDatabase on a button click and when I click it just crashes the app. No matter what I do I just can't make this thing work. I searched everywhere, tried to debug it and read every single error line and still couldn't figure it out. when I tap a button in my main activity it should create a new circle object and pass it to a insert method for the DB. I have a MainActivity, Circle class, ShapesDatabase.

one of the Error lines says:

Caused by: android.database.sqlite.SQLiteException: no such table: Circles (code 1): , while compiling: insert into Circles(x, y, radius, color) values(5, 8, 6.500000, 'Black')

this doesn't make any sense to me because I DID create a Circle table. everything else there is just not clear.

This is my shapes database:

public class ShapesDatabase extends SQLiteOpenHelper {
private SQLiteDatabase sqLiteDatabase;

public ShapesDatabase(Context context) {
    super(context, "Circles", null, 1);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL("create table Circles(id integer primary key autoincrement, x integer, y integer, radius numeric, color text)");

}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    sqLiteDatabase.execSQL("drop table if exists Circles");
    onCreate(sqLiteDatabase);
}

public void open() {
    sqLiteDatabase = getWritableDatabase();
}

public void close() {
    super.close();
}

public void insert (Circle circle){
    open();

    String sql = String.format("insert into Circles(x, y, radius, color) values(%d, %d, %f, '%s')", circle.getX(), circle.getY(), circle.getRadius(), circle.getColor());
    sqLiteDatabase.execSQL(sql);

    close();
}
}

This is the main activity:

public class MainActivity extends AppCompatActivity {
private ShapesDatabase shapesDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    shapesDatabase = new ShapesDatabase(this);
}

public void insert(View view) {
    Circle circle = new Circle(0, 5, 8, 6.5, "Black");
    shapesDatabase.insert(circle);
}
}
Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78

0 Answers0