There is a database with two lines. There is a button that, when clicked, all data from the database columns is displayed in TextView. How can I display only one line in this TextView?

Main Activity:

public class MainActivity extends AppCompatActivity { private DatabaseHelper mDatabaseHelper; private SQLiteDatabase mSqLiteDatabase; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mDatabaseHelper = new DatabaseHelper(this, "mydatabase.db", null, 1); mSqLiteDatabase = mDatabaseHelper.getWritableDatabase(); ContentValues values = new ContentValues(); // Задайте значения для каждого столбца values.put(DatabaseHelper.NAME_COLUMN, "ПЕТР"); values.put(DatabaseHelper.PHONE_COLUMN, "495455321213"); values.put(DatabaseHelper.AGE_COLUMN, "7"); // Вставляем данные в таблицу // mSqLiteDatabase.insert("students", null, values); } public void onClick(View view) { Cursor cursor = mSqLiteDatabase.query("students", new String[]{DatabaseHelper.NAME_COLUMN, DatabaseHelper.PHONE_COLUMN, DatabaseHelper.AGE_COLUMN}, null, null, null, null, null); while (cursor.moveToNext()) { int phone = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.PHONE_COLUMN)); String name = cursor.getString(cursor .getColumnIndex(DatabaseHelper.NAME_COLUMN)); TextView infoTextView2 = (TextView) findViewById(R.id.textView2); infoTextView2.append(" " +cursor.getString(0)+cursor.getString(1)); } cursor.close(); } 

DatabaseHelper:

  public class DatabaseHelper extends SQLiteOpenHelper implements BaseColumns { // названия столбцов public static final String NAME_COLUMN = "name"; public static final String PHONE_COLUMN = "phone"; public static final String AGE_COLUMN = "age"; // имя базы данных private static final String DATABASE_NAME = "mydatabase.db"; // версия базы данных private static final int DATABASE_VERSION = 1; // имя таблицы private static final String DATABASE_TABLE = "students"; private static final String DATABASE_CREATE_SCRIPT = "create table " + DATABASE_TABLE + " (" + BaseColumns._ID + " integer primary key autoincrement, " + NAME_COLUMN + " text not null, " + PHONE_COLUMN + " integer, " + AGE_COLUMN + " integer);"; DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); } public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler) { super(context, name, factory, version, errorHandler); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(DATABASE_CREATE_SCRIPT); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // Запишем в журнал Log.w("SQLite", "Обновляемся с версии " + oldVersion + " на версию " + newVersion); // Удаляем старую таблицу и создаём новую db.execSQL("DROP TABLE IF IT EXISTS " + DATABASE_TABLE); // Создаём новую таблицу onCreate(db); } 

    1 answer 1

    remove the loop, or immediately make a request for only one line.

      public void onClick(View view) { Cursor cursor = mSqLiteDatabase.query("students", new String[]{DatabaseHelper.NAME_COLUMN, DatabaseHelper.PHONE_COLUMN, DatabaseHelper.AGE_COLUMN}, null, null, null, null, null); cursor.moveToNext(); int phone = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.PHONE_COLUMN)); String name = cursor.getString(cursor .getColumnIndex(DatabaseHelper.NAME_COLUMN)); TextView infoTextView2 = (TextView) findViewById(R.id.textView2); infoTextView2.append(" " +cursor.getString(0)+cursor.getString(1)); cursor.close(); } 
    • I got the first line. And what if I need to get exactly the second line? - Lev Naumenko
    • one
      run cursor.moveToNext (); twice - this will be the second line. Or make the right request. - Valera Kvip
    • and in request their quantity cannot be reduced? Why look for all the lines from the database if you only need 496? So it is something special, and this feature must be specified in the request and extract one line, not 500. - Valera Kvip
    • Can you show in your answer how this can be implemented? @ValeraKvip - Leo Naumenko
    • I do not know what your database is, and what is the feature of the 496 line. - Valera Kvip