如何从Android糖orm数据库中检索单个列数据

时间:2017-01-16 10:34:10

标签: android sugarorm android-orm

我已经在我的应用中成功创建了一个Sugar ORM数据库,我可以更新,删除并从一行获取所有数据,但我希望单个列数据与另一个数据匹配...

我的意思是,我的注册数据库包含以下字段:usernamepasswordfirst_namelast_nameemail字段。

使用正确的用户名和密码登录用户后,我希望Textview中的 THAT用户的First_Name 发送到下一个Activity ... 我怎样才能做到这一点?过去两天我试过但失败了,请帮帮我... 提前谢谢......

5 个答案:

答案 0 :(得分: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;
    }

另一个返回列中值列表的示例。使用原样: String rawQuery = ("SELECT feed_key FROM team_feed_key WHERE team_id = " + mTeam_id + " ORDER BY feed_key DESC");

答案 1 :(得分:0)

您是否尝试过这样的原始查询?

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

来自:http://satyan.github.io/sugar/query.html

答案 2 :(得分:0)

你可以永久地向SugarRecord.java添加功能

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;
}

 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(“从Note中选择First_Name,其中name ='ali'limit 1”);

答案 3 :(得分:0)

我有同样的问题。 我希望这可以帮助某人:

String firstName = Select.from(User.class).where("EMAIL = "+ user.getEmail()).first().getFirstName();

答案 4 :(得分:0)

您好,这必须可行,您不能编辑库,但可以扩展它们,所以请查看以下内容:

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;
    }
}

用法很简单,使用DBUtils.findByColumn(...); 从现在开始,任何您喜欢的地方都只能使用此类而不是SugarRecord,还可以添加自己的其他功能。

提示: ColumnObjectType作为名称,就像您发送Integer.class

一样,告诉您列的类型