i want to query for data in sqlite, but in the case where 2item have the same name.. still it return only result... what should i add in this code?
public String getItemNameRbPrice(long lprice) throws SQLException{
String[] column = new String[]{Pro_ID, Pro_Name, Pro_Price, Pro_Description, Pro_Date};
Cursor c = ourDatabase.query(TABLE_NAME, column, Pro_Price + "=" + lprice, null, null, null, null);
if(c != null){
c.moveToFirst();
String name = c.getString(1);
Log.v(name,name + ("zz"));
return name;
}
return null;
}
Try sending List<String>
public List<String> getItemNameRbPrice(long lprice) throws SQLException{
String[] column = new String[]{Pro_ID, Pro_Name, Pro_Price, Pro_Description, Pro_Date};
Cursor c = ourDatabase.query(TABLE_NAME, column, Pro_Price + "=" + lprice, null, null, null, null);
List<String> lst = new ArrayList<String>();
if (cursor.moveToFirst()) {
do {
String name = c.getString(1);
lst.add(name);
Log.v(name,name + ("zz"));
} while (cursor.moveToNext());
}
return lst;
}
Related
Here is my query. I want to get the string value and return the single value to the activity class from where I have called the getSelectedMerchantCode function. My code is not working. Its returning "error"
public String getSelectedMerchantCode(String merchantname){
String selection = "Error";
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("SELECT merchantCode FROM " + "merchantList"+ " WHERE " + "merchantName" + "='" + merchantname + "'", null);
if(c.getCount() == 1){
c.moveToFirst();
selection = c.getString(c.getColumnIndex("merchantCode"));
return selection;
}
c.close();
db.close();
return null;
}
Try this way..
public List<String> getMyItems(String name) {
List<String> stringList = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT Item_Name FROM " + USER_TABLE_NAME + " WHERE Item_Name= " + name;
Cursor c = db.rawQuery(selectQuery, null);
if (c != null) {
c.moveToFirst();
while (c.isAfterLast() == false) {
String name = (c.getString(c.getColumnIndex("Item_Name")));
stringList.add(name);
c.moveToNext();
}
}
return stringList;
}
I tried this query and the cursor alway return to NULL, in the table "members" exist 1 record, Thank you.
sql = "SELECT nick, pass FROM miembros LIMIT 1";
cursor = write.rawQuery(sql, null);
if (cursor != null){
if(cursor.moveToFirst()) {
nickDB = cursor.getString(0);
passDB = cursor.getString(1);
resp[0] = nickDB;
resp[1] = passDB;
}
// Cierra el cursor
if(!cursor.isClosed()) cursor.close();
}else{
Log.e("CURSOR","CURSOR NULL");
}
return resp;
}
this is working code. compare and try. Change stuff as needed
public void getItem() {
String selectQuery;
SQLiteDatabase db;
String myPath = DATABASE_PATH + DATABASE_NAME;
db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
selectQuery = "SELECT * FROM TABLE";
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
cursor.getInt(cursor.getColumnIndex("id"));
while (cursor.moveToNext()) {
cursor.getInt(cursor.getColumnIndex("id"));
}
}
}
I am trying to query all the users data that belong to him uniquely. through FK_ID of the user in the notes table.
// Listing all notes
public Cursor listNotes() {
SQLiteDatabase db = help.getReadableDatabase();
Cursor c = db.query(help.NOTE_TABLE, new String[]{help.COLUMN_TITLE,help.COLUMN_BODY, help.COLUMN_DATE}, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
// Count how many Notes user has
public int NoteCount() {
String countQuery = "SELECT * FROM " + help.NOTE_TABLE;
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
// Updating single contact
public void editNote(long id, String title, String body) {
ContentValues edit = new ContentValues();
edit.put(help.COLUMN_TITLE, title);
edit.put(help.COLUMN_BODY, body);
open();
db.update(help.NOTE_TABLE, edit, help.NOTES_ID + "=" + id, null);
close();
}
// Deleting single note
public void deleteNote(long id) {
open();
db.delete(help.NOTE_TABLE, help.NOTES_ID + "=" + id, null);
close();
}
}
I have a tab that will then return all the users data uniquely to him. This tab is a fragment.the Method populateList() will be called onCreateView
public void populateList(){
Cursor cursor = control.listNotes();
getActivity().startManagingCursor(cursor);
//Mapping the fields cursor to text views
String[] fields = new String[]{help.COLUMN_TITLE,help.COLUMN_BODY, help.COLUMN_DATE};
int [] text = new int[] {R.id.item_title,R.id.item_body, R.id.item_date};
adapter = new SimpleCursorAdapter(getActivity(),R.layout.list_layout,cursor, fields, text,0);
//Calling list object instance
listView = (ListView) getView().findViewById(android.R.id.list);
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
}
This is a null pointer. Am i passing the data wrong
Cursor cursor = control.listNotes();
Your listNotes method should look like :
public Cursor listNotes(long userId) {
Cursor c = getActivity().getContentResolver().query(yourTodoTableURI, new String[]{help.COLUMN_TITLE,help.COLUMN_BODY, help.COLUMN_DATE}, help.COLUMN_USER_ID + " = ?", new String[]{String.valueOf(userId)} , null);
return c;
}
try this:
public List<String> getAllTask(int userID){
String query = "Select * from tablename where ID =" + userID;
cursor = db.query(); //do your code here
int id = cursor.getInt(cursor.getColumnIndex(tablename.ID));
List<String> task = new ArrayList<String>();
String query = "Select * from tableTask where ID =" + id";
cursor = db.query(); //
if (cursor != null) {
cursor.movetofirst();
// do your code here
do{
String task = cursor.get......
task.add(task);
}while(cursor.movetonext);
}
return task;
}
I have to following code. I want this to return an array e.g. arg[] that contains at arg[0] the number of the rows of my cursor and at arg[1] String(0) of my cursor. Since one is integer and the other is string I have a problem. Any ideas how to fix this?
public String[] getSubcategoriesRow(String id){
this.openDataBase();
String[] asColumnsToReturn = new String[] {SECOND_COLUMN_ID,SECOND_COLUMN_SUBCATEGORIES,};
Cursor cursor = this.dbSqlite.query(SECOND_TABLE_NAME, asColumnsToReturn, SECOND_COLUMN_SUBCATEGORIES + "= \"" + id + "\"", null, null, null, null);
String string = cursor.getString(0);
int count = cursor.getCount();
String arg[] = new String[]{count, string};
cursor.close();
return arg;
}
The cursor and the results and correct i just need to compine them to an array in order to return that.
Either:
Use an Object[] (an object array) instead of a String[] — not recommended, or
Create a new, meaningful class to hold the data.
public Subcategory getSubcategoriesRow(String id){
this.openDataBase();
String[] asColumnsToReturn = new String[] {SECOND_COLUMN_ID,SECOND_COLUMN_SUBCATEGORIES,};
Cursor cursor = this.dbSqlite.query(SECOND_TABLE_NAME, asColumnsToReturn, SECOND_COLUMN_SUBCATEGORIES + "= \"" + id + "\"", null, null, null, null);
String string = cursor.getString(0);
int count = cursor.getCount();
Subcategory toReturn = new Subcategory(count, string);
cursor.close();
return toReturn;
}
I'm trying to delete all contacts from a defined group but I don't know how to do a join from the contact table and group table (if it's possible).
ContentResolver cr = getContentResolver();
String where = ContactsContract.Groups.TITLE + " =='LolGroup'";
Cursor cursor = cr.query(
ContactsContract.Contacts.CONTENT_URI, null, where, null, null);
while (cursor.moveToNext()) {
String lookupKey = cursor.getString(
cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(
ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
cr.delete(uri, null, null);
}
Of course it gives me an error because there is no "title" in the contacts group, but if I do a join with the ID I should get what i want.
Any idea how to do that join?
It looks strange because ContactsContract.Contacts does not have ContactsContract.Groups.TITLE column. So I think that you can get group id with the group title you want and then search contacts with the group id. The idea might go like following:
public String getGroupIdByTitle(String groupTitle){
try {
cursor = mContentResolver.query(
ContactsContract.Groups.CONTENT_URI,
new String[] {Groups._ID},
Groups.TITLE + "=?",
new String[]{groupTitle},
null);
while (cursor.moveToNext()){
return cursor.getString(cursor.getColumnIndex(0);
}
} finally {
if (cursor!=null) cursor.close();
}
return "";
}
public String getGroupIdOfContact(String lookupKey) {
String where = String.format("%s=? AND %s=?", Data.LOOKUP_KEY, Data.MIMETYPE);
String[] whereArgs = {lookupKey, GroupMembership.CONTENT_ITEM_TYPE};
String groupRowId = "";
Cursor cursor = mContentResolver.query(
Data.CONTENT_URI,
new String[]{GroupMembership.GROUP_ROW_ID},
where, whereArgs, null);
try {
if (cursor.moveToNext()) {
return cursor.getString(cursor.getColumnIndex(GroupMembership.GROUP_ROW_ID));
}
} finally {
if (cursor!=null) cursor.close();
}
return "";
}
public void deleteContactByGroupTitle(String groupTitle) {
String targetGroupId = getGroupIdByTitle(groupTitle);
Cursor cursor = null;
try {
cursor = mContentResolver.query(Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()){
String lookupKey = cursor.getString(cursor.getColumnIndex(Contacts.LOOKUP_KEY));
String groupId = getGroupIdOfContact(lookupKey);
if (targetGroupId.equals(groupId)){
//TODO. delete this contact
}
}
} finally {
if (cursor!=null) cursor.close();
}
}
The above code has not tested but I think that basic idea would be same.