John Okoroafor John Okoroafor - 1 month ago 9
Android Question

Having trouble adding items to SQLite database Android Studio

Don't really know how to phrase my question, but i created my databasehandler class, but in my activity when trying to insert an item, it shows that the column wasn't found. using log, i noticed that when inserting, it doesn't reach the oncreate function in the databasehandler class so i think this may be an issue? Here's my code:

public class OrderActivity extends AppCompatActivity {

private List<MyMenuItem> itemList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DatabaseHandler db=DatabaseHandler.getInstance(getApplicationContext());



db.addItem(new MyMenuItem(),2);
RecyclerView rv = (RecyclerView) findViewById(R.id.rv_recycler_view);
itemList=new ArrayList<>();
itemList=db.getAllItems();
rv.setHasFixedSize(true);
OrderAdapter adapter = new OrderAdapter(getApplicationContext(),itemList);

rv.setAdapter(adapter);
LinearLayoutManager llm = new LinearLayoutManager(this);
rv.setLayoutManager(llm);


}


My database class:

public class DatabaseHandler extends SQLiteOpenHelper {

private static DatabaseHandler _instance;

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "StocksManager";

// Contacts table name
private static final String TABLE_STOCK= "stock";

// Contacts Table Columns names
private static final String KEY_ID = "_id";
private static final String KEY_NAME = "Name";
private static final String KEY_PRICE= "Price";
private static final String KEY_IMAGE_URL="ImageURL";
private static final String KEY_DETAIL="Detail";
private static final String KEY_DISCOUNT="Discount";
private static final String KEY_QTY="Quantity";


public static synchronized DatabaseHandler getInstance(Context context)
{
if(_instance==null)
{
_instance=new DatabaseHandler(context);
}
return _instance;
}

public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
final String CREATE_STOCK_TABLE = "CREATE TABLE " + TABLE_STOCK
+ "("
+ KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_NAME + " TEXT,"
+ KEY_PRICE + "INTEGER"
+ KEY_IMAGE_URL + "STRING"
+ KEY_DETAIL +" TEXT"
+ KEY_DISCOUNT + " INTEGER"
+ KEY_QTY+ " INTEGER"
+")";
db.execSQL(CREATE_STOCK_TABLE);
// Log.d("Oncreate", "table created");
}


Logcat error:

10-19 16:12:39.367 5563-5563/com.frimondi.restaurant.restaurant E/SQLiteLog: (1) table stock has no column named Discount
10-19 16:12:39.379 5563-5563/com.frimondi.restaurant.restaurant E/SQLiteDatabase: Error inserting Discount=0 Detail= _id=0 Price=1 ImageURL= Quantity=2 Name= android.database.sqlite.SQLiteException: table stock has no column named Discount (code 1): , while compiling: INSERT INTO stock(Discount,Detail,_id,Price,ImageURL,Quantity,Name) VALUES (?,?,?,?,?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498) at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
at com.frimondi.restaurant.restaurant.DatabaseHandler.addItem(DatabaseHandler.java:106)

Answer

SQLiteOpenHelper onCreate() is invoked only once when the database file did not exist. When is SQLiteOpenHelper onCreate() / onUpgrade() run?

You have missing whitespace and commas in some of your columns: you need space between column name and its type, and commas between column specifications. After adding those, you can uninstall your app to make onCreate() execute again.

Comments