In this article, we will discuss how to retrieve a single data from an SQL database and display it in a TextView in Android Studio. This is a common task when working with databases in Android app development, and it is essential for entry-level users to understand the process.
Step 1: Set up the Database
The first step is to set up the database in Android Studio. You can use the built-in SQLite database or an external database like MySQL. For simplicity, we will use SQLite in this example.
To set up the database, follow these steps:
- Create a new project in Android Studio.
- In the project structure, navigate to the "app" folder.
- Right-click on the "app" folder and select "New" -> "Folder" -> "Assets Folder".
- Open the newly created "assets" folder and right-click inside it. Select "New" -> "File" and name it "database.db".
- Now, we need to create a table in the database. Open the "database.db" file and execute the following SQL query:
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
email TEXT
);
Step 2: Create the Database Helper Class
The next step is to create a Database Helper class that will handle all the database operations. This class will extend the SQLiteOpenHelper class and provide methods to create, upgrade, and access the database.
Create a new Java class called "DatabaseHelper" and add the following code:
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "database.db";
private static final int DATABASE_VERSION = 1;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// No need to implement since we already created the table in Step 1
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// No need to implement for this example
}
public String getSingleData() {
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT name FROM users WHERE id = 1";
Cursor cursor = db.rawQuery(query, null);
String data = "";
if (cursor.moveToFirst()) {
data = cursor.getString(0);
}
cursor.close();
db.close();
return data;
}
}
Step 3: Display the Data in a TextView
Now that we have the Database Helper class ready, we can use it to retrieve the data and display it in a TextView. Follow these steps:
- In your activity layout XML file, add a TextView element with an id:
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Data will be displayed here"
android:textSize="18sp" />
- In your activity Java file, add the following code to retrieve the data and display it in the TextView:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
DatabaseHelper databaseHelper = new DatabaseHelper(this);
String data = databaseHelper.getSingleData();
textView.setText(data);
}
}
That's it! Now, when you run the app, the data from the database will be retrieved and displayed in the TextView.
In this article, we have learned how to retrieve a single data from an SQL database and display it in a TextView in Android Studio. We covered the steps to set up the database, create the Database Helper class, and display the data in a TextView. By following these steps, entry-level users can easily retrieve data from a database and display it in their Android app.
References
| Source | Link |
|---|---|
| Android Developers - SQLiteOpenHelper | https://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper |
| Android Developers - SQLiteDatabase | https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase |