Skip to content Skip to sidebar Skip to footer

Sqlite With Android Studio?

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 AppC

Solution 1:

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

MyDBHelper.java

publicclassMyDBHelperextendsSQLiteOpenHelper
{
    publicstaticfinalStringDATABASE_NAME="student";
    publicstaticfinalStringTABLE_NAME="student_table";
    publicstaticfinalStringCOL_1="ID";
    publicstaticfinalStringCOL_2="NAME";
    publicstaticfinalStringCOL_3="SURNAME";
    publicstaticfinalStringCOL_4="MARKS";

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

    @OverridepublicvoidonCreate(SQLiteDatabase db)
    {
        Stringquery="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);
    }

    publicvoidaddValue()
    {
        SQLiteDatabasedb=this.getWritableDatabase();

        ContentValuesvalues=newContentValues();

        Randomrandom=newRandom();
        intrandomValue= 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
    }

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

}

MainActivity.java

publicclassMainActivityextendsAppCompatActivity
{
    @OverrideprotectedvoidonCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MyDBHelperdb=newMyDBHelper(this);
        db.addValue();
    }
}

Solution 2:

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 :-

publicclassMainActivityextendsAppCompatActivity {

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState)
      {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SQLiteDatabasedb=newMyDBHelper(this).getWritableDatabase();
        Cursorcsr= 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.

Solution 3:

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

Post a Comment for "Sqlite With Android Studio?"