In this post, we will see How to Create Android App using SQLite Database.
How to create Android First App Hello World using Android Studio, check in following link:
https://www.comrevo.com/2019/09/Android-First-App-Hello-World-using-Android-Studio.html
To know how to install Android Studio in Linux, check following link:
https://www.comrevo.com/2019/01/how-to-install-android-studio-in-linux.html.
To find details about Architecture of Android Operating System, check following link:
https://www.comrevo.com/2018/10/architecture-of-android-operating-system.html.
Program (StudentDbHelper.java)
package com.example.sqlitedbapp; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class StudentDbHelper extends SQLiteOpenHelper { public StudentDbHelper(Context context) { super(context,"stud.db",null,1); } public void onCreate(SQLiteDatabase db){ db.execSQL("create table student (sid varchar(10),sname varchar(10),marks varchar(10))"); } public void onUpgrade(SQLiteDatabase db, int oldversion,int newversion){ db.execSQL("drop table if exists student"); onCreate(db); } } |
Program (MainActivity.java)
package com.example.sqlitedbapp; import androidx.appcompat.app.AppCompatActivity; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); StudentDbHelper dbHelper = new StudentDbHelper(this); //To insert values into database SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("sid", "1"); values.put("sname", "Parag"); values.put("marks", "80"); long row = db.insert("student", null, values); System.out.println("row number is" + row); //To retrieve values from database SQLiteDatabase db1 = dbHelper.getReadableDatabase(); String projection[] = {"sid", "sname", "marks"}; Cursor c = db.query("student", projection, null, null, null, null, null); c.moveToFirst(); System.out.println("Name is " + c.getString(1)); } } |
No comments:
Post a Comment