I want to get data in the first column of all row from my database and convert to String[]
...
List<String> item1 = new ArrayList<String>();
// c is a cursor which pointed from a database
for(int i=0;i<=nombre_row;i++)
{
c.moveToPosition(i);
item1.add(c.getString(0));
}
String[] strarray = new String[item1.size()];
item1.toArray(strarray );
I've tried to comment step by step, and found that the problem is in the Loop for....
Please help... thanks in advance for all answer.
public String[] getData()
{
Cursor cursor = this.db.query(/your query/);
if(cursor.getCount() >0)
{
String[] str = new String[cursor.getCount()];
int i = 0;
while (cursor.moveToNext())
{
str[i] = cursor.getString(cursor.getColumnIndex(Columname));
i++;
}
return str;
}
else
{
return new String[] {};
}
}
I got it work :
c.moveToFirst();
do
{
item1.add(c.getString(0).toString());
//i++;
}while(c.moveToNext());
Related
Here I want to store data in ArrayList visible Sections please help me...
Here's my code.
public DataSingleton() {
try {
db = openOrCreateDatabase("/sdcard/Download/Nanhikali_db.db", Context.MODE_PRIVATE, null);
Cursor c = db.rawQuery("select label from nanhikali where level = 1", null);
if (c.moveToFirst()) {
while (c.moveToNext()) {
String lb = c.getString(c.getColumnIndex("label"));
visibleSections.add(lb);
visibleSections = new ArrayList<>();
}
}
}
catch(Exception e)
{
}
Your column string maybe wrong:
Try like this.
if(c.moveToFirst()){
while(c.moveToNext()){
String lb = c.getString(c.getColumnIndex("label"));
visibleSections.add(lb);
}
}
Not "lebel".
You are re assigning the array list every time your loop iterates.
remove visibleSections = new ArrayList<>(); from your loop.
and use While Loop as
while(!cursor.isAfterLast())
{
//get All data here then
cursor.moveToNext();
}
I am trying to populate my AutoCompleteTextView from a column of values in my database.
The query I am running in my database is:
// GET MEMOS
public ArrayList<String> autoCompleteMemo(String table)
{
SQLiteDatabase db = this.getReadableDatabase();
ArrayList<String> memoList = new ArrayList<String>();
String SQL_GET_MEMOS = "SELECT memo FROM " + table;
Cursor cursor = db.rawQuery(SQL_GET_MEMOS, null);
cursor.moveToFirst();
if (!cursor.isAfterLast()) {
do {
memoList.add(cursor.getString(0));
}
while (cursor.moveToNext());
}
cursor.close();
db.close();
return memoList;
}
And here is how I am attempting to set the values:
memoList = new String[db.autoCompleteMemo(table).size()];
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, memoList);
etMemo.setAdapter(adapter);
For some reason, this does not appear to be working. Am i converting from ArrayList to String[] properly?
Thanks
Also,
If i do something similar to this
String[] memoList = getResources().getStringArray(R.array.memoList);
and populate that in Strings.xml it works fine.
I changed my DB method to the following and it now works
public String[] autoCompleteMemo(String table) {
SQLiteDatabase db = this.getReadableDatabase();
String SQL_GET_MEMOS = "SELECT memo FROM " + table;
Cursor cursor = db.rawQuery(SQL_GET_MEMOS, null);
if (cursor.getCount() > 0) {
String[] str = new String[cursor.getCount()];
int i = 0;
while (cursor.moveToNext()) {
str[i] = cursor.getString(cursor.getColumnIndex("memo"));
i++;
}
return str;
}
else {
return new String[] {};
}
}
I have a problem with getting Shop from database:
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
getShop function in ShopHandler.java
Shop getShop(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_SHOPS, new String[] { KEY_ID,
KEY_SHOP }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Shop shop = new Shop(Integer.parseInt(cursor.getString(0)), cursor.getString(1)); //first problem place
return shop;
}
and part of procedure in MainClass:
public void AddToList(View view)
{
EditText search = (EditText)findViewById(R.id.editTextsearch);
String SearchProduct = search.getText().toString();
int l = 0;
int p = 0;
ShopHandler sh = new ShopHandler(this);
String[] Shops = new String[sh.getShopCount()];
float[] prices = new float[sh.getShopCount()];
for(int i=0;i<=sh.getShopCount();i++)
{
Shop shop = sh.getShop(i); //hear is second problemplace
ShopName = shop.getShop();
ProductHandler ph = new ProductHandler(this);
for(int j=0;j<ph.getProductsCount();j++)
{
if(ph.getProduct(j).getName()==SearchProduct)
{
Shops[++l]=ShopName;
prices[++p]=ph.getProduct(j).price;
}
}
}
The cursor is empty. Check moveTo...() return value before accessing row data:
Shop shop = null;
if (cursor.moveToFirst()) {
shop = new Shop(Integer.parseInt(cursor.getString(0)), cursor.getString(1)); //first problem place
}
cursor.close();
return shop;
This causes the exception. Why the cursor is empty is likely because of the off-by-one here:
for(int i=0;i<=sh.getShopCount();i++)
Either start counting from 1 or change <= to <, depending on your data.
This is most likely the issue:
for(int i=0;i<=sh.getShopCount();i++)
should be for(int i=0;i < sh.getShopCount();i++)
You will be off-by-one if you keep your first loop to stop at sh.getShopCount()
How to return a list in a column e.g like all emails in column...here my code
public String reTurn() throws SQLException
{
String emails=null;
Cursor mCursor = db.rawQuery("SELECT EmailNO FROM Details_Customer " ,null);
mCursor.moveToFirst();
if(mCursor.getCount() > 0){
emails= mCursor.getString(mCursor.getColumnIndex(DBAdapter.COLUMN_EMAIL));
//password = mCursor.getString(11);
}
return emails;
}
But it only returning one email, I want it to return all the emails in a database
Try this:
public List<String> reTurn() throws SQLException
{
List<String> emails = new ArrayList<String>();
Cursor mCursor = db.rawQuery("SELECT EmailNO FROM Details_Customer ", null);
int index = mCursor.getColumnIndex(DBAdapter.COLUMN_EMAIL);
while(mCursor.moveToNext()) {
emails.add(mCursor.getString(index));
//password = mCursor.getString(11);
}
return emails;
}
Added from comment
From this:
email.putExtra(Intent.EXTRA_EMAIL, new String[]{ dbUser.reTurned});
I guess that your are trying to put the array of email addresses into an Intent called email. Here is a better approach:
public String[] reTurn() throws SQLException
{
Cursor mCursor = db.rawQuery("SELECT EmailNO FROM Details_Customer ", null);
String[] emails = new String[mCursor.getCount()];
int i = 0;
int index = mCursor.getColumnIndex(DBAdapter.COLUMN_EMAIL);
while(mCursor.moveToNext()) {
emails[i++] = mCursor.getString(index);
//password = mCursor.getString(11);
}
return emails;
}
And to put this in an Intent:
email.putExtra(Intent.EXTRA_EMAIL, dbUser.reTurned());
Lastly, in your new Activity read the email array with:
String[] emails = getIntent().getStringArrayExtra(Intent.EXTRA_EMAIL);
Hope that helps.
The problem with your code is inside the if statement.
emails = mCursor.getString(...);
That statement is only being executed once, so you only get one email.
Instead try something like:
String[] emails = new String[mCursor.getCount()];
for(int i=0; i<emails.length; i++){
emails[i] = mCursor.getString(...);
mCursor.moveToNext();
}
You can use the code as follow to get all mail address :
mCursor.moveToFirst();
ArrayList<String> mails = new ArrayList<String>();
if(mCursor.getCount() > 0){
while(!mCursor.isAfterLast()){
mails.add(mCursor.getString(mCursor.getColumnIndex(DBAdapter.COLUMN_EMAIL)));
mCursor.moveToNext();
}
}
They're all available through the Cursor because it's essentially an iterator. See the Cursor API. So you can pass the cursor as you would an array and iterate over it as needed.
If truly necessary you can copy the results into an array:
int n = mCursor.getCount();
String [] emails = new String[n];
int emailColIndex = mCursor.getColumnIndex(DBAdapter.COLUMN_EMAIL);
mCursor.moveToFirst();
for (int i = 0; i < n; i++) {
emails[i] = mCursor.getString(emailColIndex);
mCursor.moveToNext();
}
// A good place to close the cursor.
I have an application where I want to use the values returned from an SQL query against a database in my application. I want to use attach these values to a uri that links to a script on remote host. I am doing a call to the method to retrieve the values, after which I will now extract them. The issue is that I am facing a challenge in going about this. I know a bit of php and what I want in android is done like this in php:
....(preceding code)...$row['email'];
and I can now assign a variable e.g. $email=$row['email'];
How can I do this in android?
I don't want a cursor returned, just the values of the columns as strings.
Below is my code (I will need help to retrieve the password column)
public String[] selectAll()
{
Cursor cursor = db.query(DB_TABLE_NAME, new String[] { "email","password"},null, null, null, null,null);
if(cursor.getCount() >0)
{
String[] str = new String[cursor.getCount()];
int i = 0;
while (cursor.moveToNext())
{
str[i] = cursor.getString(cursor.getColumnIndex("email"));
i++;
}
return str;
}
else
{
return new String[] {};
}
}
I need help on inserting the "password" column so that it will be returned with the email.
try this way
String[][] str = new String[cursor.getCount()][cursor.getColumnCount()];
while (cursor.moveToNext())
{
for(int j=0;j<cursor.getColumnCount();j++)
str[i][j] = cursor.getString(j); /// may be this column start with the 1 not with the 0
i++;
}
you need to store this in two dimenstion array for row wise and column wise
but if this will give only one result everytime
String[] str = new String[cursor.getColumnCount()]; // here you have pass the total column value
while (cursor.moveToNext())
{
str[0] = cursor.getString(cursor.getColumnIndex("email"));
str[1] = cursor.getString(cursor.getColumnIndex("password"));
}
You should change your design like this
public Cursor selectAll() {
Cursor cursor = db.query(DB_TABLE_NAME, new String[] { "email","password"},null, null, null, null,null);
return cursor;
}
and use the selectAll function like this
Cursor cursor=selectAll();
String[] mail = new String[cursor.getCount()];
String[] pass = new String[cursor.getCount()];
int i=0;
while (cursor.moveToNext())
{
mail[i] = cursor.getString(cursor.getColumnIndex("email"));
pass[i] = cursor.getString(cursor.getColumnIndex("password"));
i++;
}