0

I am trying to make a project with android studio as in the video of : Source 1 and Source 2 and here is my code in my added java class :

    package com.example.asus.androidsqlitedatabase_1;
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;

    /**
     * Created by ASUS on 7/6/2017.
     */

    public class Databasehelperclass extends SQLiteOpenHelper {
        public static final String DATABASE_NAME = "Student.db";
        public static final String TABLE_NAME = "Student_table";
        public static final String COLUMN1= "ID";
        public static final String COLUMN2 = "NAME";
        public static final String COLUMN3 = "SURNAME";
        public static final String COLUMN4 = "MARKS";

        public Databasehelperclass(Context context) {
            super(context, DATABASE_NAME, null, 1);
            SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
        }

        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {
            sqLiteDatabase.execSQL("Create Table" + TABLE_NAME + 
   "(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,SURNAME TEXT,MARKS INTEGER)");}

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1){
            sqLiteDatabase.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
            onCreate(sqLiteDatabase);
        }
    }

and here is my code in MainActivity.java

    package com.example.asus.androidsqlitedatabase_1;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;

    public class MainActivity extends AppCompatActivity {
        Databasehelperclass mydb;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mydb = new Databasehelperclass(this);
        }
    }

But when I execute the app to the emulator using genymotion, the app stopped working and it cant be executed. Please anyone help me !!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Anthony Lauly
  • 319
  • 2
  • 5
  • 17

1 Answers1

1

Issue: Wrong spacing in query

Solution: query should be in this format:

sqLiteDatabase.execSQL("CREATE TABLE " + TABLE_NAME + "("
                + COLUMN1 + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + COLUMN2 + " TEXT, "
                + COLUMN3 + " TEXT, "
                + COLUMN4 + " INTEGER, "
                + ")";

PS: It is good practice to use Variable name instead of static column names in Query.

Hope it helps :)

Atlas_Gondal
  • 2,512
  • 2
  • 15
  • 25
  • Thank you so much for the solution – Anthony Lauly Jul 07 '17 at 23:53
  • You are welcome and don't forget to accept the answer. To mark an answer as accepted, click on the check mark beside the answer to toggle it from greyed out to filled in. [Accept someone's answer](https://stackoverflow.com/help/someone-answers) – Atlas_Gondal Jul 08 '17 at 06:09