-1

I'm trying to insert data into a SQLite database but app is crashing for some reason I know nothing about. I just learned "How to create database and table, how to insert data(but it doesn't seem to)" Please don't ask me what Android Studio is saying about the crash cause I can't run app on my laptop with the help of "Android Simulator or whatever it is called".

Below is my code of DatabaseHelper the extends SQLiteOpenHelper

package com.famiddle.genius.businessmanager;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

// DECLARING NAME FOR THE DATABASE AND THE TABLE
public static final String DATABASE_NAME = "imported_items";
public static final String TABLE_NAME = "stock";

// DECLARING NAME FOR THE COLUMNS
public static final String COL_1 = "ID";
public static final String COL_2 = "ITEMS_NAME";
public static final String COL_3 = "QUANTITY";
public static final String COL_4 = "COST";
public static final String COL_5 = "TOTAL_COST";

// DEFAULT CONSTRUCTOR
public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

// IT CREATES TABLE
@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, ITEMS_NAME TEXT, QUANTITY INTEGER, COST INTEGER, TOTAL_COST INTEGER");
}

// IT DROPS TABLE, IF IT ALREADY EXISTS
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db); //I DON'T KNOW WHY IT IS HERE, PLEASE TELL ME MORE ABOUT 
//   IT IN COMMENTS
}

// IT INSERTS DATA OR VALUES IN TABLE
public boolean insertData(String itemsName, int quantity, int cost, int totalCost) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_2, itemsName);
    contentValues.put(COL_3, quantity);
    contentValues.put(COL_4, cost);
    contentValues.put(COL_5, totalCost);
    long result = db.insert(TABLE_NAME, null, contentValues);
    //db.close();
    if (result == -1){
        return false;
    }else {return true;}
   // return result != -1;
}
}

And this is the code for stock fragment, where database instance is being called and used

public class Stock extends Fragment {
DatabaseHelper dataBase;
ArrayList<String> items;

SharedPreferences prefs;
SharedPreferences.Editor editor;

 public Stock() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.stock, container, false);

    prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
    editor = prefs.edit();
    if(prefs.getBoolean("has_not_created?", true)) {
        // INITIALIZING ARRAY LIST THAT CONTAINS ITEMS NAME
        items = new ArrayList<>();
        items.add("One");
        items.add("Two");
        items.add("Three");
        items.add("Four");
        items.add("Five");
        items.add("Six");

        // INITIALIZING DATABASE
        dataBase = new DatabaseHelper(getContext());
        // CREATING A LOOP FOR INSERTING ITEMS NAMES ONE BY ONE
        for (int i = 0; i < items.size(); i++) {
             Boolean isInserted = dataBase.insertData(items.get(i), 0, 0, 0);
            Toast.makeText(getActivity(), items.get(i) + " has been inserted into database" + isInserted , Toast.LENGTH_SHORT).show();
        }
        // PUTTING 'FALSE' IN SHARED PREFERENCE OBJECT
        // THEN IT WILL NOT ALLOW THE CODE TO RUN
        editor.putBoolean("has_not_created?", false);
        editor.apply();
    }

Can someone please tell me what's wrong with my code? I've learned it from Android tutorial.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Arpit Chhabra
  • 193
  • 1
  • 12
  • 2
    Post your crash logs. – Henry Dec 14 '17 at 14:18
  • I can't run app on my laptop with the help of android simulator. – Arpit Chhabra Dec 14 '17 at 14:30
  • You can see the logs in the logcat view even if you run your ap on simulator or actual device – Ravi Dec 14 '17 at 14:48
  • If you are talking about running a app in my phone while it is connected to the laptop running android studio then I'm afraid I can't run it that way. I chose android emulator to run my app for debugging purposes but later don't know what happened AS says some dialog to uninstall app data etc etc but it never works. – Arpit Chhabra Dec 14 '17 at 14:57
  • Do tell me if there's an error in my code. It is literally giving me headache. – Arpit Chhabra Dec 14 '17 at 14:59
  • 1
    You said that your app is crashing, when you are run the app and the app crashes, it gives a stack trace which helps the developer understand the reason for crash. You can find the stack trace in Logcat which you can find at the bottom of AndroidStudio. Please re-run the app on emulator/device, let the app crash, copy the stack trace from logcat and post it here to help us identify the issue in your code. (Error stack trace will be red in color in LogCat.) – Saran Sankaran Dec 14 '17 at 15:20

1 Answers1

0

Your error is at your onCreate().

You're opening the parenthesis, but not closing it anywhere. So it should be ::

 db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, ITEMS_NAME TEXT, QUANTITY INTEGER, COST INTEGER, TOTAL_COST INTEGER)");
n_r
  • 589
  • 7
  • 17