0

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

我的意思是,我有一个包含以下字段的注册数据库:username, password, first_name, last_name,email字段。

使用正确的用户名和密码登录用户后,我希望将THAT User's First_Name发送Textview到 Next Activity... 我该怎么做?在过去的两天里,我尝试了但失败了,请帮助我...提前谢谢...

4

5 回答 5

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");

于 2017-08-31T12:33:42.723 回答
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 作为名称 Suggest 告诉您发送 Integer.class 一样的列类型

于 2019-01-08T21:20:04.057 回答
0

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

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

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

于 2017-01-16T10:51:10.123 回答
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("Select First_Name from Note where name ='ali' limit 1");

于 2017-06-28T18:18:04.750 回答
0

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

String firstName = Select.from(User.class).where("EMAIL = "+ user.getEmail()).first().getFirstName();
于 2018-08-30T10:56:56.420 回答