Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve Data As String From SQLite Database in Android Studio

I want to save name in the SQLite Database and later in another activity I want to retrieve that name as a String Value. But I am not being able to get data from the database. Help me please. My code is given below,

Database Helper:

    private static final String TAG = "DatabaseHelper";
    private static final String TABLE_NAME = "user";
    private static final String COL1 = "ID";
    private static final String COL2 = "name";

    public void onCreate(SQLiteDatabase db) {String createTable = 
    "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY 
    AUTOINCREMENT, " + COL2 +" TEXT)"; 
    db.execSQL(createTable);}

    public boolean addData(String item) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL2, item);

            Log.d(TAG, "addData: Adding " + item + " to " + TABLE_NAME);

            long result = db.insert(TABLE_NAME, null, contentValues);

            if (result == -1) {
                return false;
            } else {
                return true;
            }
        }

MainActivity:

    mDatabaseHelper=new DatabaseHelper(this);
    Username=(EditText)findViewById(R.id.user);
    saveBtn=(Button)findViewById(R.id.button);

    saveBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    String newEntry = Username.getText().toString();
                            AddData(newEntry);
                            Username.setText("");

    Intent intent = new Intent(MainActivity.this, HomeActivity.class);
                        startActivity(intent);
                    }
                });

            }
            public void AddData(String newEntry) {
                boolean insertData = mDatabaseHelper.addData(newEntry);
        }
        }

HomeActivity:

public class HomeActivity extends AppCompatActivity {

    DatabaseHelper mDatabaseHelper;
    String Username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        mDatabaseHelper = new DatabaseHelper(this);

    }

}

Here, in this home activity i want to get the username as a string but I don't know how to do that. Help me please...

like image 856
SA Sajib Avatar asked May 31 '26 00:05

SA Sajib


1 Answers

To retrieve data, you run a query that extracts the data into a Cursor. The Cursor will consist of 0-n rows and will have as many columns as defined in the query. You then move through the rows and use the appropriate Cursor get????? methods to get the actual data.

As an example you could add the following method to your DatabaseHelper :-

public String getName(long id) {

    String rv = "not found";
    SqliteDatabase db = this.getWritableDatabase();
    String whereclause = "ID=?";
    String[] whereargs = new String[]{String.valueOf(id)};
    Cursor csr = db.query(TABLE_NAME,null,whereclause,whereargs,null,null,null);
    if (csr.moveToFirst()) {
        rv = csr.getString(csr.getColumnIndex(COL2));
    }
    return rv;
}

The above can then be used by String name = mDatabaseHelper.getName(1);.

  • Note that this assumes that a row with an ID of 1 exists.
like image 160
MikeT Avatar answered Jun 02 '26 13:06

MikeT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!