SQLite request return nothing - android

this is insert code
for(int i = 0 ;pseudo.size() <= i; i++){
ContentValues values = new ContentValues();
String contact = (String) pseudo.get(i);
values.put("pseudo", contact);
values.put("adresse", "test");
values.put("image", "test");
values.put("ID", "5");
dbwrite.insert("Contact", null, values);
}
And this is read code
String[] retour = {
"pseudo",
"xmppadresse",
"image", "ID"
};
Cursor cursor = dbread.query("Contact", retour, null, null, null, null, "pseudo");
Log.d("debug", "populateWithInitialContacts: "+cursor.getCount());
while(cursor.moveToNext()) {
String pseudo = cursor.getString(cursor.getColumnIndexOrThrow("pseudo"));
Log.d("debug", "getcontact: "+pseudo);
listcontact.add(new Contact(pseudo));
}
cursor.close();
My request return nothing(It's my first time using database SQLite in Android)

for(int i = 0 ;pseudo.size() <= i; i++)
This is not how you iterate over lists. You probably want something like
for(int i = 0 ;i < pseudo.size(); i++)

Related

How to insert an array to SQLite?

I need to insert an array loaded by JSON into an SQLite database. Any help is appreciated.
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray sites = jsonObj.getJSONArray("Sites");
for (int i = 0; i < sites.length(); i++) {
JSONObject c = sites.getJSONObject(i);
String id = c.getString("id");
String nam = c.getString("names");
String loc = c.getString("location");
HashMap<String, String> sit = new HashMap<>();
sit.put("id", id);
sit.put("names", name);
sit.put("location", loc);
array_sites.add(sit);
Try this:
public void addData(ArrayList<String> list){
int size = list.size();
SQLiteDatabase db = getWritableDatabase();
try{
for (int i = 0; i < size ; i++){
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, list.get(i));
Log.d("Data Inserted ",""+ cv);
db.insertOrThrow(TABLE_NAME, null, cv);
}
db.close();
}catch (Exception e){
Log.e("Exception>>>>", e + " ");
}
Call from where you store your data
addData(array_sites);
I suggested for ArrayList<String>, You can use your own ArrayList<YourDataType>.

How to return all value from a function

I want to return all values of dbManagement.ORIGINATING_ADDRESS from this function but it just gives me last one value to blockedNumber. I know it'll give last one value but how can i get all value. kindly help
public String selectBlockedNumbers() {
Cursor cursor = dbManagement.selectBlockedNumbers();
String blockedNumber = null;
cursor.moveToFirst();
for(int i = 0; i < cursor.getCount(); i++) {
blockedNumber = cursor.getString(cursor.getColumnIndex(dbManagement.ORIGINATING_ADDRESS));
cursor.moveToNext();
}
Toast.makeText(this, blockedNumber, Toast.LENGTH_LONG).show();
return blockedNumber;
}
In
Cursor cursor = dbManagement.selectBlockedNumbers();
the function selectBlockedNumbers() consist of following query:
cursor = db.rawQuery("SELECT " + ORIGINATING_ADDRESS + " FROM " + TABLE_BLOCK_LIST, null);
When the for-loop runs for the last time, blockedNumbers will contain the last number retrieved. So, instead of returning a String, rather returns a List<String>. In that case your code should change into
public List<String> selectBlockedNumbers() {
Cursor cursor = dbManagement.selectBlockedNumbers();
List<String> blockedNumbers = new ArrayList<String>();
cursor.moveToFirst();
for(int i = 0; i < cursor.getCount(); i++) {
String blockedNumber = cursor.getString(cursor.getColumnIndex(dbManagement.ORIGINATING_ADDRESS));
blockedNumbers.add(blockedNumber);
cursor.moveToNext();
}
Toast.makeText(this, blockedNumber, Toast.LENGTH_LONG).show();
return blockedNumbers;
}

how to get String array in reverse order in Sqlite in android

public String[] getAllDescription() {
db = this.getReadableDatabase();
cursor = db.query(NOTIFICATIONTABLE, new String[]{"notification_id", "notification_title",
"notification_description", "notification_isread", "date"}, null, null, null,
null, null);
String[] result = new String[cursor.getCount()];
int i = 0;
if (cursor.moveToFirst()) {
do {
result[i] = cursor.getString(cursor.getColumnIndex("notification_description"));
i++;
} while (cursor.moveToNext());
}
if ((cursor != null) && !cursor.isClosed()) {
cursor.close();
db.close();
}
return result;
}
this is my code currently i am getting from 0 to n data in staring array i want to get in reverse array data from n to 0 so that i can Print please suggest me how i will get data
Try this method :
public ArrayList<String> getAllDescription()
{
ArrayList<String> array_list = new ArrayListString>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("SELECT * " + " FROM " + NOTIFICATIONTABLE+ " DESC;", null);
res.moveToFirst();
while (res.isAfterLast() == false)
{
array_list.add(hashmap);
res.moveToNext();
}
return array_list;
}
Or If you want to reverse arrayList then you can also do it By just a single line of code .
Collections.reverse("yourArrayList");
Hope it will help you.
i am getting from 0 to n data in staring array i want to get in
reverse array data from n to 0
Why not using ASC or DESC in db query to get result in sequence as want according to notification_description column.
do it as:
cursor = db.query(NOTIFICATIONTABLE,
new String[]{"notification_id",
"notification_title",
"notification_description",
"notification_isread",
"date"}, null, null, null,
null, "notification_description DESC");
You can insert the data from end of theString[]...
public String[] getAllDescription() {
db = this.getReadableDatabase();
cursor = db.query(NOTIFICATIONTABLE, new String[]{"notification_id", "notification_title",
"notification_description", "notification_isread", "date"}, null, null, null,
null, null);
String[] result = new String[cursor.getCount()];
int i = 0;
if (cursor.moveToFirst()) {
do {
///////////////////////
result[(cursor.getCount()-1)-i] = cursor.getString(cursor.getColumnIndex("notification_description"));
///////////////////////
i++;
} while (cursor.moveToNext());
}
if ((cursor != null) && !cursor.isClosed()) {
cursor.close();
db.close();
}
return result;
}

Problems with database

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

i cant create string array with cursor data in android

private static final String Memo_Name = "MemoName";
.
openDB();
String[] col = new String[] {Memo_Name};
Cursor c = db.query(Memo_Table, col, null, null, null, null, null);
String[] result = new String[c.getCount()-1];
if(c.moveToFirst()){
for (int i = 0; i < c.getCount(); i++){
result[i] = c.getString(c.getColumnIndex(Memo_Name));
c.moveToNext();
}//end of for
}
db.close();
ArrayIndexOutOfBoundsException on this line
result[i] = c.getString(c.getColumnIndex(Memo_Name));
glad I could help.
just so you could close the question -
remove the " -1 "

Categories

Resources