Read values from a table in SQLite

4

I need help in the following situation: I need to get the values of a column in the bank and write these values into a variable, and then this variable will randomly generate one of those saved data.

Below the class where I create the bank:

import android.database.sqlite.SQLiteDatabase;
import android.content.Context;
import android.database.sqlite.SQLiteOpenHelper;
import dominio.android.forca.data.DataBaseDescription.Contact;

class AddressBookDatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "Forca.db";
    private static final int DATABASE_VERSION = 1;

    // constructor
    public AddressBookDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // creates the contacts table when the database is created
    @Override
    public void onCreate(SQLiteDatabase db) {
        // SQL for creating the contacts table
        final String CREATE_CONTACTS_TABLE =
                "CREATE TABLE " + Contact.TABLE_NAME + "(" +
                        Contact._ID + " integer primary key, " +
                        Contact.COLUMN_WORD + " TEXT, " +
                        Contact.COLUMN_TIP + " TEXT);";
        db.execSQL(CREATE_CONTACTS_TABLE); // create the contacts table
    }

    // normally defines how to upgrade the database when the schema changes
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }
}

Below is the class that describes the table and column name, and other ContentProvider information:

import android.content.ContentUris;
import android.net.Uri;
import android.provider.BaseColumns;

public class DataBaseDescription {
    // ContentProvider's name: typically the package name
    public static final String AUTHORITY =
            "dominio.android.forca.data";

    // base URI used to interact with the ContentProvider
    private static final Uri BASE_CONTENT_URI = Uri.parse("content://" + AUTHORITY);

    // nested class defines contents of the contacts table
    public static final class Contact implements BaseColumns {
        public static final String TABLE_NAME = "palavrasforca"; // table's name

        // Uri for the contacts table
        public static final Uri CONTENT_URI =
                BASE_CONTENT_URI.buildUpon().appendPath(TABLE_NAME).build();

        // column names for contacts table's columns
        public static final String COLUMN_WORD = "palavra";
        public static final String COLUMN_TIP = "dica";


        // creates a Uri for a specific contact
        public static Uri buildContactUri(long id) {
            return ContentUris.withAppendedId(CONTENT_URI, id);
        }


    }
}

And now, the class I'm setting up is a method to randomly generate the value of the COLUMN_WORD column of the table. That's the problem. I do not know how to retrieve these values and play within public String[] palavras = new String[] {};

import java.util.Random;

public class Palavras{

    public String[] palavras = new String[] {};


    public Palavras() {
    }

    public String sorteio() {
        String palavraSorteada = palavras[(int)(random()*palavras.length)];

        return palavraSorteada;
    }

    public static double random() {
        Random r = new Random();

        return r.nextDouble();
    }
}

I'm sorry for the errors in the above codes, I'm starting to learn how to program on Android.

    
asked by anonymous 13.06.2016 / 06:22

1 answer

2

You can get the random value in SQL itself for example:

private void Exemplo(){
    SQLiteDatabase db = getReadableDatabase();
    String selectQuery = "SELECT dica, palavra FROM palavrasforca ORDER BY random() LIMIT 1";
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
      String dica = cursor.getString(cursor.getColumnIndex("dica"));
      String palavra = cursor.getString(cursor.getColumnIndex("palavra "));
   }
}

or if you want to load into memory

   private String[] Exemplo(){
            List<String> dados = new ArrayList()
            SQLiteDatabase db = getReadableDatabase();
            String selectQuery = "SELECT palavra FROM palavrasforca";
            Cursor cursor = db.rawQuery(selectQuery, null);

            if (cursor.moveToFirst()) {
              do{
                  String palavra = cursor.getString(cursor.getColumnIndex("palavra"));
                  dados.add(palavra) 
              }while (cursor.moveToNext())
            }
            //aqui dados terá todos os valores do banco
            //converte o list para array
            return dados.toArray(new String[dados.size()]);

        }

Put the code in the AddressBookDatabaseHelper class

I hope I have helped!

    
13.06.2016 / 22:27