1

Table structure

[Table(TableConst.TABLE_BRANDING)]
public class BrandingInfoModel
{
    public int id { get; set; }
    public string title { get; set; }
    public string primary_color { get; set; }
    public string secondary_color { get; set; }
    public string tertiary_color { get; set; }
}

Method to get resource from database

    public string GetColorResourceFromDatabase(string key)
    {
        try
        {

            string value = mSqlConnection.Query<string>("SELECT " + key + " FROM data").ToString();
            return value;

        }
        catch (Exception e)
        {
            string error = e.Message;
            return null;
        }

    }

I have written the method that returns the value from the local database based on SELECT query. But it returns the null.

6
  • Share your data table's structure Commented Jul 18, 2017 at 14:00
  • if KEY is the PK of the item you want to select, then your SQL syntax is incorrect. Commented Jul 18, 2017 at 14:00
  • The generic parameter to the Query method specifies the type of object to create for each row (you are telling it to use string). It can be one of your table classes, or any other class whose public properties match the column returned by the query. You cannot force a column value into a string object because it has no public properties (at least not using the Query method). Also note that the query you're running has the potential to return multiple rows. I'm not sure why you're attempting to call ToString on a list of rows. Commented Jul 18, 2017 at 14:05
  • @deckertron_9000 any other way to get desire output ? Commented Jul 18, 2017 at 14:06
  • As @SamvelPetrosov suggested, it would be very helpful if you shared your table structure and also more of what you are trying to achieve. Commented Jul 18, 2017 at 14:10

3 Answers 3

4

I have tried this way

  //Get resource from Brading table
    public BrandingInfoModel GetResourceFromDatabase(string key)
    {
        try
        {
            //string value = (from i in mSqlConnection.Table<BrandingInfoModel>() select i.menu_text_color).ToString();

            var queryResult = mSqlConnection.Query<BrandingInfoModel>("SELECT " + key + " FROM " + TableConst.TABLE_BRANDING).FirstOrDefault();
            return queryResult;

        }
        catch (Exception e)
        {
            string error = e.Message;
            return null;
        }

    }

It returns the desired output.

Sign up to request clarification or add additional context in comments.

Comments

0

Instead of using SQL query to do the select. Please consider to use Linq query to achieve the same result.

For example if you want to select out a row and get the primary_color with id:

var rowData = mSqlConnection.Table<BrandingInfoModel>()
                            .FirstOrDefault(i => i.id == 123);
if (rowData != null)
{
    return rowData.primary_color;
}

Comments

0

My solution is just to create a support class PrimitiveTypeItem and query it. This will not be limited to strings, but any primitive type you want.

public class PrimitiveTypeItem<T>
{
    public T Value { get; set; }
}

var resultString = mSqlConnection.FindWithQuery<PrimitiveTypeItem<string>>("SELECT title AS VALUE FROM BrandingInfoModel LIMIT 1").Value;

// another example 
var resultLong = mSqlConnection.FindWithQuery<PrimitiveTypeItem<long>>("SELECT SUM(REALWONPRIZE) AS VALUE FROM SOMETHING").Value;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.