I know its simple and I am just new to SQLite and Android Studio, but I am trying to create a database table inventory each item having an ID, item name, and quantity. Then view that database table in RecyclerView. Right now I am getting the error message:
E/SQLiteDatabase: Error inserting item=Apples _id=1 quantity=5
android.database.sqlite.SQLiteException: table inventory has no column named quantity
(code 1 SQLITE_ERROR): , while compiling: INSERT INTO inventory(item,_id,quantity) VALUES
(?,?,?)
Here is my current code:
IventoryDB.java
private static final class InventoryTable {
private static final String TABLE = "inventory";
private static final String COL_ID = "_id";
private static final String COL_ITEM = "item";
private static final String COL_QTY = "quantity";
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + InventoryTable.TABLE + " (" +
InventoryTable.COL_ID + " integer primary key autoincrement, " +
InventoryTable.COL_ITEM + " text, " +
InventoryTable.COL_QTY + "text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion) {
db.execSQL("drop table if exists " + InventoryDB.InventoryTable.TABLE);
onCreate(db);
}
public void addItem(int id, String item, String qty) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(InventoryDB.InventoryTable.COL_ID, id);
values.put(InventoryDB.InventoryTable.COL_ITEM, item);
values.put(InventoryDB.InventoryTable.COL_QTY, qty);
db.insert(InventoryDB.InventoryTable.TABLE, null, values);
}
Inventory.java
private AlertDialog.Builder dialogBuilder;
private AlertDialog dialog;
private InventoryDB DB;
private int id;
public void addItem(View view) {
createNewAddItemDialog();
}
public void createNewAddItemDialog(){
dialogBuilder = new AlertDialog.Builder(this);
final View addItemPopupView = getLayoutInflater().inflate(R.layout.popup_additem, null);
addItemName = (EditText) addItemPopupView.findViewById(R.id.additempopup_itemname);
addItemQuantity = (EditText)addItemPopupView.findViewById(R.id.additempopup_itemquantity);
addItemBtn = (Button) addItemPopupView.findViewById(R.id.additempopup_add);
cancelAddBtn = (Button) addItemPopupView.findViewById(R.id.additempopup_cancel);
dialogBuilder.setView(addItemPopupView);
dialog = dialogBuilder.create();
dialog.show();
addItemBtn.setOnClickListener(v -> {
String itemName = addItemName.getText().toString();
String itemQuantity = addItemQuantity.getText().toString();
if (itemName.equals("") || itemQuantity.equals("")) {
//addError.setText("Please ensure both fields are filled out!");
} else {
Boolean itemExists = DB.checkItemExists(itemName);
if (itemExists){
//addError.setText("Item already exists!");
} else {
DB.addItem(id, itemName, itemQuantity);
dialog.dismiss();
id += 1;
}
}
});
cancelAddBtn.setOnClickListener(v -> dialog.dismiss());
}
popup_additem.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Item"
android:textSize="25dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.25" />
<EditText
android:id="@+id/additempopup_itemname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Item Name"
android:inputType="text"
android:minHeight="48dp"
android:text=""
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.361" />
<EditText
android:id="@+id/additempopup_itemquantity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Item Quantity"
android:inputType="number"
android:minHeight="48dp"
android:text=""
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/additempopup_itemname"
app:layout_constraintVertical_bias="0.066" />
<Button
android:id="@+id/additempopup_add"
android:layout_width="150dp"
android:layout_height="50dp"
android:text="Add"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.8"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/additempopup_itemquantity"
app:layout_constraintVertical_bias="0.105" />
<Button
android:id="@+id/additempopup_cancel"
android:layout_width="150dp"
android:layout_height="50dp"
android:text="Cancel"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/additempopup_add"
app:layout_constraintHorizontal_bias="0.8"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/additempopup_itemquantity"
app:layout_constraintVertical_bias="0.105" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is a long shot, but I think that you're missing a blank space in the last sentence of the method onCreate(SQLiteDatabase db)
... it seems that you're creating a column named: quantitytext
instead of quantity text
...