簡體   English   中英

如何從Android Sugar Orm數據庫檢索單列數據

[英]How to retrieve a single column data from a Android sugar orm database

我已經在我的應用程序中成功創建了Sugar ORM數據庫,我可以更新,刪除並從一行中獲取所有數據,但是我希望將單列數據與另一個數據進行匹配...

我的意思是,我有一個注冊數據庫,其中包含以下字段: usernamepasswordfirst_namelast_nameemail字段。

使用正確的用戶名和密碼登錄用戶后,我希望將Textview中該用戶的First_Name發送給Next Activity ...我該怎么做? 在過去的兩天里,我嘗試了但失敗了,請幫助我。

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

您是否嘗試過運行像這樣的原始查詢?

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

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

您可以將功能永久添加到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;
}

縮放器(“從注釋中選擇名字,其中名稱='ali'限制1“);

我有同樣的問題。 我希望這可以幫助別人:

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

嗨,這必須可行,您不能編輯庫,但是可以擴展它們,所以請檢查以下內容:

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一樣,建議告訴列的類型

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM