0

I am new to Android SQLite database & while executing this.getWritableDatabase() my app stops. Is there any solution to this problem? My database is about getting students' information and saving their marks in the database.

public class DatabaseHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME="Dictionary.db";
    public static final String TABLE_NAME="Dictionary_data";
    public static final String COL_1="ID";
    public static final String COL_2="Name";
    public static final String COL_3="SURNAME";
    public static final String COL_4="MARKS";

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE"+ TABLE_NAME+ "(ID INTEGER PRIMARY KEY AUTO INCREMENT,NAME TEXT,SURENAME TEXT,MARKS INTEGER)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS"+TABLE_NAME);
        onCreate(db);
    }

    public boolean insert_data(String name,String surname,String marks) {
        SQLiteDatabase db =  this.getWritableDatabase();
        ContentValues contentValues= new ContentValues();
        contentValues.put(COL_2,name);
        contentValues.put(COL_3,surname);
        contentValues.put(COL_4,marks);
        long result = db.insert(TABLE_NAME,null,contentValues);

        if(result==-1)
            return false;
        else
            return true;
    }
}

And my mainActivity class:

public class MainActivity extends AppCompatActivity {

    DatabaseHelper myDB;
    EditText editName,editSurname,editMark;
    Button btn_add_data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myDB = new DatabaseHelper(this.getApplicationContext());

        editName = (EditText)findViewById(R.id.namespace);
        editSurname = (EditText)findViewById(R.id.surnamespace);
        editMark = (EditText)findViewById(R.id.markspace);
        btn_add_data = (Button)findViewById(R.id.add_button);
        addData();
    }

    public void addData(){
        btn_add_data.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean id_inserted = myDB.insert_data(editName.getText().toString(),editSurname.getText().toString(),editMark.getText().toString());

                    if(id_inserted)
                        Toast.makeText(MainActivity.this,"Data inserted",Toast.LENGTH_LONG).show();
                    else
                        Toast.makeText(MainActivity.this,"Data not inserted",Toast.LENGTH_LONG).show();
                }
            }
        );
    }
}
Michael Dodd
  • 9,326
  • 12
  • 48
  • 62
  • 1
    Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Sep 13 '17 at 15:00

1 Answers1

1

there is error in create table

db.execSQL("CREATE TABLE"+ TABLE_NAME+ "(ID INTEGER PRIMARY KEY AUTO INCREMENT,NAME TEXT,SURENAME TEXT,MARKS INTEGER)");

please add space after TABLE

db.execSQL("CREATE TABLE "+ TABLE_NAME+ " (ID INTEGER PRIMARY KEY AUTO INCREMENT,NAME TEXT,SURENAME TEXT,MARKS INTEGER)");

also add log cat for more details,i am not able to comment as it's my new account.

MJM
  • 4,870
  • 4
  • 22
  • 49
  • `CREATE TABLE "+ TABLE_NAME+ " (ID INTEGER PRIMARY KEY AUTO INCREMENT,NAME TEXT,SURENAME TEXT,MARKS INTEGER)` will also result in an error as the correct keyword is `AUTOINCREMENT` (i.e. without a space between AUTO and INCREMENT). The error will be along the lines of *SQL syntax error: CREATE TABLE mytable (ID INTEGER PRIMARY KEY AUTO INCREMENT,NAME TEXT,SURENAME TEXT,MARKS INTEGER) [ near "AUTO": syntax error ]* – MikeT Sep 14 '17 at 01:45