SQLite UWP - retreive一行数据返回null

时间:2018-01-07 20:15:23

标签: sqlite uwp uwp-xaml

我试图通过SQLite中的UWP数据库来处理数据,但只有一条特定的行。现在,它适用于多行,但仍不适用于一行...这是我使用的代码;

public static Participant GetParticipant(int klasse_id, int start_number)
        {
            Participant res = new Participant();
            try
            {
                string sSQL = @"SELECT * FROM participants WHERE class_id = " + klasse_id + " AND start_number = " + start_number + " LIMIT 1;";
                ISQLiteStatement dbState = dbConnection.Prepare(sSQL);
                // Get the records
                if (SQLiteResult.DONE == dbState.Step())
                {
                    Debug.WriteLine(dbState["pilot_firstName"].ToString());
                    // Set the new value
                    res = new Participant()
                    {
                        pilot_firstName = dbState["pilot_firstName"].ToString(),
                        pilot_lastName = dbState["pilot_lastName"].ToString(),
                        pilot_club = dbState["pilot_club"].ToString(),
                        pilot_license = dbState["pilot_license"].ToString(),
                        pilot_tel = dbState["pilot_tel"].ToString(),
                        navigator_firstName = dbState["navigator_firstName"].ToString(),
                        navigator_lastName = dbState["navigator_lastName"].ToString(),
                        navigator_club = dbState["navigator_club"].ToString(),
                        navigator_license = dbState["navigator_license"].ToString(),
                        navigator_tel = dbState["navigator_tel"].ToString(),
                        is_stopped = Int32.Parse(dbState["is_stopped"].ToString()),
                        class_id = Int32.Parse(dbState["class_id"].ToString()),
                        start_id = Int32.Parse(dbState["start_id"].ToString()),
                        star_nmbr = Int32.Parse(dbState["start_number"].ToString())
                    };
                }
                return res;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                throw ex;
            }
        }

我每次都得到Object reference not set to an instance of an object. ...但是当我执行我的SQL查询时,我得到了有效的数据;

Query result

Participant我的班级如下:

public class Participant
    {
        public int id { get; set; }
        public string pilot_firstName { get; set; }
        public string pilot_lastName { get; set; }
        public string pilot_club { get; set; }
        public string pilot_license { get; set; }
        public string pilot_tel { get; set; }
        public string navigator_firstName { get; set; }
        public string navigator_lastName { get; set; }
        public string navigator_club { get; set; }
        public string navigator_license { get; set; }
        public string navigator_tel { get; set; }
        public int is_stopped { get; set; }
        public int class_id { get; set; }
        public int start_id { get; set; }
        public int star_nmbr { get; set; }
    }

有人看到这里有什么问题吗?

1 个答案:

答案 0 :(得分:2)

试试这个:

public static Participant GetParticipant(int klasse_id, int start_number)
    {
        try
        {
            string sSQL = @"SELECT * FROM participants WHERE class_id = " + klasse_id + " AND start_number = " + start_number + " LIMIT 1;";
            ISQLiteStatement dbState = dbConnection.Prepare(sSQL);
            // Get the records
            while (SQLiteResult.ROW == dbState.Step())
            {
                Debug.WriteLine(dbState["pilot_firstName"].ToString());
                // Set the new value
                var res = new Participant()
                {
                    pilot_firstName = dbState["pilot_firstName"].ToString(),
                    pilot_lastName = dbState["pilot_lastName"].ToString(),
                    pilot_club = dbState["pilot_club"].ToString(),
                    pilot_license = dbState["pilot_license"].ToString(),
                    pilot_tel = dbState["pilot_tel"].ToString(),
                    navigator_firstName = dbState["navigator_firstName"].ToString(),
                    navigator_lastName = dbState["navigator_lastName"].ToString(),
                    navigator_club = dbState["navigator_club"].ToString(),
                    navigator_license = dbState["navigator_license"].ToString(),
                    navigator_tel = dbState["navigator_tel"].ToString(),
                    is_stopped = Int32.Parse(dbState["is_stopped"].ToString()),
                    class_id = Int32.Parse(dbState["class_id"].ToString()),
                    start_id = Int32.Parse(dbState["start_id"].ToString()),
                    star_nmbr = Int32.Parse(dbState["start_number"].ToString())
                };
        return res;
            }
            return null; // In case if there is no result
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex);
            throw ex;
        }
    }
相关问题