How to select by the database the value of the column that I used the method random ()

0

I made a project in Android that is a little game of the gallows, where I can write in the SQLite database the word and a tip. In my DatabaseHelper class I created the normal database, and made some selects from the column where the words are stored and sent as Arraylist to a class where I randomize those words. How can I link the tips to random words?

DatabaseHelper class, where I create the bank

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.content.Context;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.List;

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;

    // Criamos aqui uma classe chamada DatabaseHelper, que serve para
    // manipular a abertura e manutenção do banco de dados em si.

    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) {
    }

    // MÉTODO FEITO POR UM LIST DE STRING
    public List<String> Word(){
        List<String> dadosPalavra = 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"));
                dadosPalavra.add(palavra);
            }while (cursor.moveToNext());
        }

        return dadosPalavra;
        //aqui dados terá todos os valores do banco
    }

    // MÉTODO FEITO POR UM LIST DE STRING
    public String Tip(){
        String dadosDica = new String();
        SQLiteDatabase db = getReadableDatabase();
        String selectQuery = "SELECT dica FROM palavrasforca";
        Cursor cursor = db.rawQuery(selectQuery, null);

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

        return dadosDica;
        //aqui dados terá todos os valores do banco
    }

}

And the class where I make the random choice of words:

import android.content.Context;
import java.util.List;
import java.util.Random;

public class Palavras {

    AddressBookDatabaseHelper dbHelper;

    public Palavras(Context context) {
        dbHelper = new AddressBookDatabaseHelper(context.getApplicationContext());
    }

    public Palavras() {  }

    public String sorteio() {

        List<String> lista_palavras = dbHelper.Word();
        String palavraSorteada = lista_palavras.get((int)(random()*lista_palavras.size()));


        return palavraSorteada;
    }

    public String dica(){
        String tipSorteada = dbHelper.Tip();
        String dicaSorteada = tipSorteada;

        return dicaSorteada;
    }

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

        return r.nextDouble();
    }

}
    
asked by anonymous 15.06.2016 / 02:32

1 answer

0

Good morning,

In the Tip () method you can pass the word and use the where in sql to fetch from the database.

public String Tip(String palavra){
        String dadosDica = new String();
        SQLiteDatabase db = getReadableDatabase();
        String selectQuery = "SELECT dica FROM palavrasforca WHERE palavra=?";
        Cursor cursor = db.rawQuery(selectQuery, new String[] { palavra });

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

        return dadosDica;
        //aqui dados terá todos os valores do banco
    }

If you want to load in memory you can use Map to associate,

Map works as a dictionary, you store the key and value

Map<String, String> mapPalavras = new HashMap<String, String>();
        mapaNomes.put("Palavra1", "Dica Palavra 1");
        mapaNomes.put("Palavra2", "Dica Palavra 2");
        mapaNomes.put("Palavra3", "Dica Palavra 3");


String dica = mapPalavras.get("Palavra1") //retorna "Dica Palavra 1"

Just when calling Word (), you get the word and the hint and store it in a Map as the example ..

    
15.06.2016 / 13:57