Understanding SQLite Database and Implementation in Android Development

Slide Note
Embed
Share

Explore the fundamentals of SQLite database, its integration within Android applications, and the process of creating and managing databases and tables through practical examples. Learn how to insert values into SQLite database tables effectively.


Uploaded on Sep 25, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Ritu Meena Assistant Professor Shivaji Collage Course-B.Sc.(APS)Computer Science Semester - VI Paper- Android Programming

  2. SQLITE DATABASE 1. SQLite database is in built into every android device 2. SQLite database is a base class, which provide methods open(), close(), insert(), update(), delete(). 3. execSQL() method used to execute sql statements 4. Data/data/app-name/databases/filename default directory in which database is saved.

  3. Creating Database and Tables To Create and upgrade a database in your Android application you create a subclass of the SQLiteOpenHelper class. In the constructor of your subclass you call the super() method of SQLiteOpenHelper Create ->empty activity App folder ->java folder-> Right click on Package-> create new java class -> DatabseHelper.java

  4. SQLite Example DatbaseHelper.java file private static class DatabaseHelper extends SQLiteOpenHelper { Public static final String DATABASE_NAME = student.db ; Public static final String TABLE_NAME = student_table ; Public static final String COL_1 = Std_ID ; Public static final String COL_2 = Name ; public static final String COL_3 = Marks ; DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); SQLiteDatabase db = this.getWritableDatbase(); } //after call this method database is created

  5. @Override public void onCreate(SQLiteDatabase db) { db.execSQL( create table + TABLE_NAME + (Std_ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, MARKS INTEGER) ); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS +TABLE_NAME); onCreate(db); } }

  6. MainActivity.java file DatabaseHelper mydb; //create object of DatabaseHelper class Protected void onCreate(Bundle savedInstanceState){ Mydb = new DatabseHelper(this);

  7. Insert values to SQLite Database table using Android

  8. Continue with DatabaseHelper.java.(slide no5) public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS +TABLE_NAME); onCreate(db); } Public boolean insertData(String Std_ID, String name, String marks){ SQLiteDatabase db = this.getWritableDatbase(); //we can remove this line from slide no 4 ContentValues contentvalues = new ContentValues(); contentvalues.put(COL_2, name); contentvalues.put(COL_3, marks); long result = db.insert(TABLE_NAME,null,contentvalues); If (result == -1) return false; else return false }}

  9. In MainActivity.java file Create 3 EditText and a Button name adddata EditText editName = findViewById(R.id. editName); EditText editMarks = findViewById(R.id. editMarks); EditText editID= findViewById(R.id. editID); Public void gotoActivity(){ \\on Click method boolean isInserted = mydb.insertData(editName.getText().toString(), } editID.getText().toString(), editMarks.getText().toString()); If (isInserted == true) Toast.makeText(MainActivity.this, data Inserted ,Toast.LENGTH_LONG).show(); else Toast.makeText(MainActivity.this, data not Inserted ,Toast.LENGTH_LONG).show();

  10. References https://developer.android.com/training/data-storage/sqlite

More Related Content