Provide random item from database - java

I have f.e. a thousand names (texts) and I need to show them one by one in random order when I click a button. What is the best way to do that?

Use SQLite or Shared Preferences if you need to keep names even after you close the program. Here is the sample code.
ExternalDbOpenHelper dbOpenHelper;
SQLiteDatabase database;
dbOpenHelper = new ExternalDbOpenHelper(context, "DB");
database = dbOpenHelper.getWritableDatabase();
database.execSQL("CREATE TABLE IF NOT EXISTS "+"names"+
" ("+"ID"+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
"name"+" TEXT)");
ContentValues cv = new ContentValues();
cv.put("name", name_string);
database.insert("names", null, cv);
database.close();
then use Cursor to access values from database:
Cursor c = database.query("names", null, null, null, null, null, null);
c.moveToFirst();
String name;
do{
int nameCI = c.getColumnIndex("name");
name = c.getInt(nameCI);
}while (c.moveToNext());

get all the name from database and store in the array list .You can get the all the name on the onCreate() of the activity or on button click as you wish.
ArrayList<String> list= new ArrayList<String>();
list.add("A");
list.add("E");
list.add("I");
list.add("O");
list.add("U");
public void clickEvent(){
Collections.shuffle(list);
String name=list.get(0);//from index
textView.setText(name);
}

Related

Android Studio - SQLite Cursor not returning any data

I am trying to return the names of distinct values in the 'category' column of an SQLite database. The cursor does not appear to be returning any results.
I'm building an Android app using a pre-existing database of bird species. The aim is to allow the user to explore the database through the app. The problem I'm having is in trying to return the distinct categories of bird species that exist in the database. The database appears to be opening successfully - no SQLite exception is being thrown, - but after using the query, the '.moveToNext' method does not appear to be returning any data.
public ArrayList<String> getCategory(String[] name){
String TABLE_BIRDS = "main";
String[] COLUMN = name;
ArrayList<String>categories = new ArrayList<>();
if (name[0]!=null)
{
Log.d(LOG_TAG, name[0]);
} else {
Log.d(LOG_TAG, "name[0] has not been passed");
}
x = db.query(true, TABLE_BIRDS, new String[] { COLUMN[0] } , null, null, COLUMN[0], null, null, null );
if (x.moveToNext()) {
Log.i(LOG_TAG, x.getString(x.getColumnIndex("category")));
}
else {
Log.i(LOG_TAG, "The cursor is not returning any data");
}
//Simple cursor loop
if (x.moveToNext()){
String category = new String();
category = x.getString(x.getColumnIndex(category));
categories.add(category);
Log.i("cursor loop", category);
}
return categories;
In the above code, the log messages are showing that: getCategory is receiving the expected string "category" before the query, but right after the query, the if/else loop is reporting that "The cursor is not returning any data".
What I expected is that the query would return six Strings, the cursor loop would add them to the ArrayList 'categories', and this ArrayList would be returned.
Please any help would be appreciated.
Here is a simpler version of your method:
public ArrayList<String> getCategory(String[] name) {
String TABLE_BIRDS = "main";
ArrayList<String> categories = new ArrayList<>();
if (name[0] != null) {
Log.d(LOG_TAG, name[0]);
} else {
Log.d(LOG_TAG, "name[0] has not been passed");
}
Cursor x = db.query(true, TABLE_BIRDS, new String[]{name[0]}, null, null, null, null, null, null);
while (x.moveToNext()) {
String category = x.getString(0);
categories.add(category);
Log.i("cursor loop", category);
}
if (x.getCount() == 0) {
Log.i(LOG_TAG, "The cursor is not returning any data");
}
x.close();
return categories;
}
I guess name[0] contains the string 'category' which is the column that you want to query.
Since you pass true as the 1st argument in the method query() this means that you will get distinct values, so no need for the group by argument.
You don't need the variable COLUMN since you have what you need in the variable name.Also you don't need getColumnIndex() since the cursor returns only 1 column.
Edit
Instead of:
Cursor x = db.query(true, TABLE_BIRDS, new String[]{name[0]}, null, null, null, null, null, null);
try rawQuery():
Cursor x = db.rawQuery("SELECT DISTINCT " + name[0] + " FROM " + TABLE_BIRDS, null);
Assuming the cursor is not null, you should
iterate over the results with a while/for loop.
e.g :while(x.moveToNext()) {
//your logic
}
It is always useful when debugging this kind of issues to install an SQLite DB browser then check the cursor's query against your DB to see if you have any data.

Save email from first table to second table

I am creating an application. I need the email of the existing user, which is already saved in the first table, and save it into the second table. Which queries or functions do I need to achieve this? Any suggestions?
First, you need to fetch the user's email by passing on the valid row id:
public Cursor getRecord(long id) throws SQLException {
Cursor cursor = this.database.query(true, databaseTable, new String[] {columnId, columnName, columnEmail}, columnId + "=" + id, null, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
Please note, you may have different columns so change the String[] array with your designated columns. Now, we can create another function to save that email in another table like so:
public long insertExistingUser(String name, String email) {
ContentValues contentValues = new ContentValues();
contentValues.put(columnName, name);
contentValues.put(columnEmail, email);
return this.database.insert(otherDatabaseTable, null, contentValues);
}
This will insert the other user's information into the other table. In order for this to work in your application:
DatabaseAdapter db = new DatabaseAdapter(this);
db.open();
Cursor cursor = db.getRecord(current_user_id);
if (db.insertExistingUser(cursor.getString(1), cursor.getString(2)) > 0)
Toast.makeText(this, "Old user's info was inserted!", Toast.LENGTH_SHORT).show();
db.close();
The cursor.getString(1) requires a number that indicates what column it is. Usually, 0 is the id column which you use to get the user's email.

Android SQLlite Get List of Columns from Empty Table

Using Android SQL, I'm trying to get a list of the columns from an SQL table which is empty.
Here is what I have tried so far:
SQLiteDatabase db = getReadableDatabase();
ArrayList<String> output = new ArrayList<String>();
Cursor ti = db.rawQuery("PRAGMA table_info("+myTable+")", null);
if ( ti.moveToFirst() ) {
do {
output.add(ti.getString(1));
} while (ti.moveToNext());
}
ti.close();
db.close();
return output;
The resulting list Appears just the word INTEGER, while the database has many columns. How can I make this work?
this is i use to retrieve column name in string:
SQLiteDatabase mDataBase;
(some code here...)
mDataBase = getReadableDatabase();
Cursor dbCursor = mDataBase.query(TABLE_NAME, null, null, null, null, null, null);
String[] columnNames = dbCursor.getColumnNames();
or this :
Cursor c = db.rawQuery("SELECT * FROM table WHERE 0", null);
try {
String[] columnNames = c.columnNames();
} finally {
c.close();
}
The column name is returned in the column named name:
...
int nameColIdx = ti.getColumnIndexOrThrow("name");
...
output.add(ti.getString(nameColIdx));
...

Why is my getColumnIndex() returning -1?

Ok, so here is my database create statement:
create table entry (
_id integer primary key autoincrement,
date integer not null,
checknum integer,
payee text not null,
amount integer not null,
category text,
memo text,
tag text
);
After the datbase is created, and I make a call like:
mChecknum = cursor.getColumnIndex("checknum");
mChecknum is -1. I have pulled the database from the device and used SQLite Browser on it, and the checknum field is there.
Block around statement in question:
mDbHelper.open();
Cursor cursor = mDbHelper.fetchAll();
startManagingCursor(cursor);
COL_DATE = cursor.getColumnIndex("date");
Log.v("Main:123", "COL_DATE: " + String.valueOf(COL_DATE) );
COL_CHECKNUM = cursor.getColumnIndex("checknum");
Log.v("Main:125", "COL_CHECKNUM: " + String.valueOf(COL_CHECKNUM) );
COL_PAYEE = cursor.getColumnIndex("payee");
COL_DATE returns 1 and COL_PAYEE returns 2. Why is COL_CHECKNUM being ignored/passed over?
In my situation getColumnIndex was returning error on the column which had name which contained the "." symbol i.e.:
int colIndex = cursor.getColumnIndex("Item No.");
and despite the column "Item No." was inside the cursor (the debugger Watch showed it) trying to get the column index failed.
In my fetch/fetchAll database functions, I forgot to update the query with the new column.
For example, before i added the new column, my fetchAll code looked like:
public Cursor fetchAll() {
return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_DATE,
KEY_PAYEE, KEY_AMOUNT, KEY_CATEGORY, KEY_MEMO, KEY_TAG},
null, null, null, null, KEY_DATE + " desc");
}
After adding the new column to the database, my fetchAll function looks like:
public Cursor fetchAll() {
return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_DATE,
KEY_CHECKNUM, KEY_PAYEE, KEY_AMOUNT, KEY_CATEGORY, KEY_MEMO, KEY_TAG},
null, null, null, null, KEY_DATE + " desc");
}

Insert if not exists?

I'm prompting a user to add an item to an SQLiteDb. When they click add, I want to check if that item already exists... if it doesn't then I want to insert it.
I make the call
mDbHelper.createNote(inputLine.getText().toString().trim(), mTable[i]);
Which calls...
public long createNote(String value, String table) {
ContentValues initialValues = new ContentValues();
initialValues.put(table, value);
return mDb.insert(table, null, initialValues);
}
Thats works but it doesn't check if the item already exists, so it still inserts duplicates. So I tried
return mDb.insertWithOnConflict(table, null, initialValues, SQLiteDatabase.CONFLICT_FAIL);
However, it doesn't appear to recognize insertWIthOnConflict or SQLiteDatabase.CONFLICT_FAIL...
How can I get this to work?
Edit: it's 1 table, 2 rows. table name = note, rows = _id, note.
Use the REPLACE command
In such situation I perfrom such checking:
if (!checkRecordExist(DATABASE_TABLE, new String[] {KEY_1, KEY_2}, new String[] {value_1, value_2}))
database.insert(DATABASE_TABLE, null, updateValues);
where
private boolean checkRecordExist(String tableName, String[] keys, String [] values) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < keys.length; i++) {
sb.append(keys[i])
.append("=\"")
.append(values[i])
.append("\" ");
if (i<keys.length-1) sb.append("AND ");
}
Cursor cursor = database.query(tableName, null, sb.toString(), null, null, null, null);
boolean exists = (cursor.getCount() > 0);
cursor.close();
return exists;
}
Add a Primary Key. If you try to insert another row with the same key, the insert will fail.
Have you tried using a primary key that isn't set to auto-increment? That might be why the REPLACE command isn't working

Categories