0

I have created a Sugar ORM database successfully in my app, I can update, delete and also get all data from a row, but I want a single column data matched with another data...

I mean, I have a registration database with fields: username, password, first_name, last_name, email fields.

After login a user with right username and password, I want THAT User's First_Name in a Textview sent to the Next Activity... How can I do this? Over last two days I have tried but failed, please help me... Thanks in advance...

Jon Goodwin
  • 9,053
  • 5
  • 35
  • 54

5 Answers5

1
   public static List<String> getResultWithRawQuery(String rawQuery, Context mContext) {
        List<String> stringList = new ArrayList<>();
        if (mContext != null) {
            long startTime = System.currentTimeMillis();
            SugarDb sugarDb = new SugarDb(mContext);
            SQLiteDatabase database = sugarDb.getDB();

            try {
                Cursor cursor = database.rawQuery(rawQuery, null);
                try {
                    if (cursor.moveToFirst()) {
                        do {
                            stringList.add(cursor.getString(0));
                        } while (cursor.moveToNext());
                    }
                    Timber.d(cursor.getString(0), "hi");
                } finally {
                    try {
                        cursor.close();
                    } catch (Exception ignore) {
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            long endTime = System.currentTimeMillis();
            long totalTime = endTime - startTime;
            System.out.println("total time query" + totalTime);
        }
        return stringList;
    }

Another example that returns a List of values in the column. Use as such: String rawQuery = ("SELECT feed_key FROM team_feed_key WHERE team_id = " + mTeam_id + " ORDER BY feed_key DESC");

HannahCarney
  • 3,441
  • 2
  • 26
  • 32
0

Did you try to run a raw query like this?

List<Note> notes = Note.findWithQuery(Note.class, "Select * from Note where name = ?", "satya");

from: http://satyan.github.io/sugar/query.html

mcatta
  • 481
  • 5
  • 12
  • yes, but just this query execute correctly... this query returns all the data from database, every row data and also every column data...... But I want just want field data... can you help? – Shovon Mohonto Jan 16 '17 at 16:10
  • I think this feature is not ready yet, i have found this issue https://github.com/satyan/sugar/issues/270 – mcatta Jan 16 '17 at 21:55
  • I would suggest writing your own method if you chose to use SugarDB - judging from the amount of open issues the code is completely unmaintained. I would say no more work will be done on this ORM and it's better to go with another option. – HannahCarney Aug 31 '17 at 12:34
0

you can add function to SugarRecord.java forever

public static String Scaler(String Query) {
    String Result = "";

    SugarDb db = getSugarContext().getSugarDb();
    SQLiteDatabase sqLiteDatabase = db.getDB();

    SQLiteStatement sqLiteStatament = sqLiteDatabase
            .compileStatement(Query);

    try {
        Result = sqLiteStatament.simpleQueryForString();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        sqLiteStatament.close();
    }

    return Result;
}

or

 public static String Scaler(String Query) {
    String Result = "";
    SQLiteDatabase sqLiteDatabase =              SugarContext.getSugarContext().getSugarDb().getDB();

    SQLiteStatement sqLiteStatament = sqLiteDatabase
            .compileStatement(Query);

    try {
        Result = sqLiteStatament.simpleQueryForString();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        sqLiteStatament.close();
    }
    return Result;
}

Scaler("Select First_Name from Note where name ='ali' limit 1");

Hossein Hajizadeh
  • 1,357
  • 19
  • 10
0

I had the same problem. I hope this helps someone:

String firstName = Select.from(User.class).where("EMAIL = "+ user.getEmail()).first().getFirstName();
Kaleab O.
  • 11
  • 3
0

Hi this must work you can not edit the libraries but you can extend them so check this out:

public class DBUtils extends SugarRecord {
    public static <T> List<Object> findByColumn(Context context, String tableName,T ColumnObjectType, String columnName) {
        Cursor cursor = new SugarDb(context).getDB().query(tableName, new String[]{columnName}, null, null,
                null, null, null, null);
        List<Object> objects = new ArrayList<>();
        while (cursor.moveToNext()){
        if (ColumnObjectType.equals(long.class) || ColumnObjectType.equals(Long.class)) {
            objects.add(cursor.getLong(0));
        }else if(ColumnObjectType.equals(float.class) || ColumnObjectType.equals(Float.class)){
            objects.add(cursor.getFloat(0));
        }else if(ColumnObjectType.equals(double.class) || ColumnObjectType.equals(Double.class)){
            objects.add(cursor.getDouble(0));
        }else if(ColumnObjectType.equals(int.class) || ColumnObjectType.equals(Integer.class)){
            objects.add(cursor.getInt(0));
        }else if(ColumnObjectType.equals(short.class) || ColumnObjectType.equals(Short.class)){
            objects.add(cursor.getShort(0));
        }else if(ColumnObjectType.equals(String.class)){
            objects.add(cursor.getString(0));
        }else{
            Log.e("SteveMoretz","Implement other types yourself if you needed!");
        }
        }
        if (objects.isEmpty()) return null;
        return objects;
    }
}

The usage is simple use DBUtils.findByColumn(...); Any where you like and from now on you can use only this class instead of SugarRecord and add your own other functions as well.

hint: ColumnObjectType as the name Suggest tells the type of column like you send Integer.class

Steve Moretz
  • 2,758
  • 1
  • 17
  • 31