1

I am trying to create a table in the Android SQLite database. But execSQLite method does not work in the on create method. It gives me the message-

Non-static method 'execSQL' cannot be a reference from a static context

But the method I am calling from is a non-static method. The method is given below.

  public void onCreate(SQLiteDatabase sqLiteDatabase) {
            SQLiteDatabase.execSQL();
        }

Here is my full java code:

package com.sarsinetvarneshon.basicdb;

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

/**
 * Created by Sarsinet Varneshon on 23-Apr-18.
 */

public class EmployeeDatabaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "db_employee";
    public static final int VERSION = 1;
    public static final String EMPLOYEE_TABLE_NAME = "tbl_employee";

    public static final String EMPLOYEE_COL_ID = "id";
    public static final String EMPLOYEE_COL_NAME = "name";
    public static final String EMPLOYEE_COL_AGE = "age";
    public static final String EMPLOYEE_COL_DEPT = "dept";

    public static final String CREATE_TABLE_EMPLOYEE = " CREATE TABLE "+ EMPLOYEE_TABLE_NAME+
            "("+EMPLOYEE_COL_ID+ " INTEGER PRIMARY KEY, "+
            EMPLOYEE_COL_NAME+ " TEXT, "+
            EMPLOYEE_COL_AGE+ " INT, "+
            EMPLOYEE_COL_DEPT+ " TEXT)";




    public EmployeeDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        SQLiteDatabase.execSQL();

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}
Sohid Ullah
  • 75
  • 1
  • 1
  • 7

3 Answers3

1

Change the line:

  SQLiteDatabase.execSQL();

To this line:

  sqLiteDatabase.execSQL();

You are using a Class Name instead of the Variable.

Xenolion
  • 12,035
  • 7
  • 33
  • 48
  • wow. It works. But What is the use of SQLiteDatabase.execSQL() ? – Sohid Ullah Apr 23 '18 at 19:49
  • Okay accept my answer by clicking a tick beside it. `SQLiteDatabase.execSQL()` does not mean anything. But in java some methods are static means that you do not have to create its instance just the class itself example `Integer.parseInt()` so `execSQL()` is not a static method to be called that way it needs a variable not a class name.@SohidUllah – Xenolion Apr 23 '18 at 19:52
  • 1
    @SohidUllah https://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-a-class – m0skit0 Apr 24 '18 at 19:08
1

You need to call sqLiteDatabase.execSQL(); not SqLiteDatabase.execSQL();

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
MGLabs
  • 91
  • 6
0

Because execSQL() method is not a static method, it needs to be called using an object from SQLiteDatabase, and it cannot be called using the class itself.