-1

I'm having problems creating sqlite database. I have searched some options but that didn't work out for me. Here is my code

MainActivity.java

public class MainActivity extends AppCompatActivity
{

    @Override
      protected void onCreate(Bundle savedInstanceState)
      {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SQLiteDatabase db = new MyDBHelper(this).getWritableDatabase();
      }
    }

Database Helper

public class MyDBHelper extends SQLiteOpenHelper
    {
    public static final String DATABASE_NAME = "Student.db";
    public static final String TABLE_NAME = "student_table";
    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 MyDBHelper(Context context)
    {
        super(context, DATABASE_NAME, null, 1);
        SQLiteDatabase db = getWritableDatabase();
    }

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

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

}

Thanks.

MikeT
  • 51,415
  • 16
  • 49
  • 68

3 Answers3

2

Your code is working fine but you need to insert data to your table then you can see the database.

MyDBHelper.java

public class MyDBHelper extends SQLiteOpenHelper
{
    public static final String DATABASE_NAME = "student";
    public static final String TABLE_NAME = "student_table";
    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 MyDBHelper(Context context)
    {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        String query = "CREATE TABLE " + TABLE_NAME +
            "(" +
            COL_1 + " INTEGER PRIMARY KEY," +
            COL_2 + " TEXT," +
            COL_3 + " TEXT," +
            COL_4 + " TEXT" +
            ")";
        Log.e("query", query);
        db.execSQL(query);
    }

    public void addValue()
    {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();

        Random random = new Random();
        int randomValue = random.nextInt();

        values.put(COL_1, randomValue);
        //values.put(COL_1, 1001);
        values.put(COL_2, "Mitesh");
        values.put(COL_3, "Machhoya");
        values.put(COL_4, "80");

        // Inserting Row
        db.insert(TABLE_NAME, null, values);
        db.close(); // Closing database connection
    }

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

}

MainActivity.java

public class MainActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MyDBHelper db = new MyDBHelper(this);
        db.addValue();
    }
}
Mitesh Machhoya
  • 404
  • 2
  • 8
  • I'm getting new errors in the log saying this android.database.sqlite.SQLiteConstraintException: PRIMARY KEY must be unique (code 19) – Mohommad Kumail Feb 06 '18 at 11:57
  • Obviously, it will give an error because you are inserting value to ID field duplicate, as you specified ID field is set with PRIMARY KEY so you need to give value UNIQUE value in ID field every time. 1001 is just for temporary you need to give with your requirement but it should be UNIQUE, – Mitesh Machhoya Feb 06 '18 at 12:36
0

Unless the phone is rooted then you probably won't be able to see the database as you won't have the permissions to see it.

I'd suggest trying the following and looking at the log :-

public class MainActivity extends AppCompatActivity {

    @Override
      protected void onCreate(Bundle savedInstanceState)
      {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SQLiteDatabase db = new MyDBHelper(this).getWritableDatabase();
        Cursor csr = db.query("sqlite_master",null,null,null,null,null,null);
        while (csr.moveToNext) {
            Log.d("TABLEINFO","Type is " + csr.getString(csr.getColumnIndex("type")) +
                " Name is " + csr.getString(csr.getColumnIndex("tbl_name")));
        }
        csr.close();
      }
  }

sqlite_master is a special table that defines the schema for the database. You would expect to see student_table listed in the output, thus verifying that the table has been created.

You may be interested in Are there any methods that assist with resolving common SQLite issues? which expands upon the above.

MikeT
  • 51,415
  • 16
  • 49
  • 68
0

In latest Android Studio, you can check your app's database through Device File Explorer. In this explorer you can now explore emulator and device's database. You can find it under data/data/your_app_name/databases/db_name.

enter image description here

Open Device File Explorer and find your app database using the path data/data/your_app_name/databases/db_name enter image description here

SANAT
  • 8,489
  • 55
  • 66
  • Do some database related things. First insert data to database then it will be available. – SANAT Feb 06 '18 at 12:25
  • The above process is working with the emulator and I am able to see the data base but not with a phone. – Mohommad Kumail Feb 06 '18 at 14:24
  • I tested it on device + emulator its working fine using Latest Android studio. Please accept the answer of you are satisfied with answer. – SANAT Feb 06 '18 at 19:37