dimanche 12 juin 2016

Redeem values in a column of a table in SQLite

I need some help in the following situation: I need to store the values of a table field in SQLite and set these values in a variable that will store them after this is done, then would generate a random value.

Below is my class where I am creating my 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 my class which describes the table name and column names and other information for ContentProvider:

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 am establishing the method for generating a value of the column random COLUMN_WORD:

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();
    }
}

Forgive my errors over the code, I'm new and I'm trying to learn!

Aucun commentaire:

Enregistrer un commentaire