Search code examples
android-studioandroid-sqlite

Insert values manually into sqlite tables with code androidstudio


I want to add values to a table in SQLite android. all I can find on the internet is how to add values using button(by creating a user interface to add the values). But I want to do this manually with code and directly, is it possible and how? Thanks.

Below is the DATABASE I'm working on!

PS: the values are texts of 200 words at least for each column

DATABASEHELPER.JAVA

  public class DatabaseHelper extends SQLiteOpenHelper{
        public static final String DATABASE_NAME="URTsqlite.db";
        public static final String TABLE_NAME="Anatomy";
        public static final String COL1="OrganName";
        public static final String COL2="Overview";
        public static final String COL3="Externalfeatures";
        public static final String COL4="Internalfeatures";
        public static final String COL5="role";

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

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL( " create table " +TABLE_NAME+"(OrganName PRIMARY KEY,Overview TEXT,Externalfeatures TEXT,Internalfeatures TEXT,role TEXT)");

        }

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

    }

    public boolean insertData (String OrganName ,String Overview ,String Externalfeatures ,String Internalfeatures,String role)
    {
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues contentValues=new ContentValues();
        contentValues.put(COL1,OrganName);
        contentValues.put(COL2,Overview);
        contentValues.put(COL3,Externalfeatures);
        contentValues.put(COL4,Internalfeatures);
        contentValues.put(COL5,role);
        long result=db.insert(TABLE_NAME,null,contentValues);
        if(result==-1)
            return false;
        else
            return true;

    }

}

Solution

  • is it possible and how?

    Yes it is. You simply call your insertData method with the values you want to add.

    e.g. say you had an Activity and wanted to add a single row then you could have the following in the Activity's onCreate method :-

    public class MyActivity extends AppCompatActivity {

    DatabaseHelper myDB;
    
    @Override
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myactivity_main);
        myDB = new DatabaseHelper(this);
        myDB.insertData("hammond","overview blah","external features blah","internal features blah","makes music");
        ......... other code
    }
    

    Thus everytime the activity's onCreate method is run an attempt will be made to add a row (however as OrganName is implicitly UNQIUE, as it's the PRIMARY KEY, then once added it will no longer be added again, the conflict (duplicate) will be caught by the SQLiteDatabase insert method and thus allow processing to continue).

    Saying that typically a pre-existing (externally created) database would be supplied as an asset, this being copied when the App is first run after it is installed.