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();
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…