2

我正在使用 sqlite v3,我有一个表,其中包含一些数据类型为“位”的列。我在此表中插入了一些数据,位列中的值为“true”,但问题是当我使用 C# 代码检索数据时,它总是返回值“false”。

相同的查询和相同的数据库适用于我的 android 应用程序。但是,当我从 C# 或 Windows 检索这些数据时,它会返回异常结果。

Sqlite 管理员在查询窗格中显示正确的结果.....我正在使用 chrome 浏览器....我的 C# 代码是

SQLiteConnection sqlite_connection =null;

        if(isTrans)
            sqlite_connection = sqliteTrans.Connection;
        else
            sqlite_connection = this.ConnectionInstance;

        try
        {  
            SQLiteCommand sqlite_command = new SQLiteCommand("SELECT AdditionalServices, VitalObservation from tblReadonlyPatientData where SubscriptionID = 1306", sqlite_connection);
            SQLiteDataReader reader = sqlite_command.ExecuteReader();
            StringBuilder queryToExecute = new StringBuilder();

            while (reader.Read())
            {
                queryToExecute.Append(reader.GetString(0));
            }

            // always call Close when done reading. 
            reader.Close();
            reader = null;
            sqlite_command = null

        }
            catch (Exception e)
            {
                clsException.ExceptionInstance.HandleException(e);
                throw;
            }
            finally
            {
                if (!isTrans && sqlite_connection != null)
                {
                    if (sqlite_connection.State == System.Data.ConnectionState.Open)
                        sqlite_connection.Close();                    
                }
            }     

这些列是位数据类型和“真”值,但返回假。

4

2 回答 2

4

我已更新 sql lite 查询并将位列转换为 nvarchar,因此它以“true”或“false”格式返回字符串

SQLiteCommand sqlite_command = new SQLiteCommand("SELECT Cast(AdditionalServices as nvarchar(10)) as AdditionalServices, Cast(VitalObservation as nvarchar(10)) as VitalObservation from tblReadonlyPatientData where SubscriptionID = 1306", sqlite_connection);
于 2012-05-30T09:30:49.267 回答
1

对于 sqlite,字符串 'true' 的计算结果为 False,因为 sqlite 将字符串转换为返回 0 的 Numeric 类型,该类型直接为 False:

sqlite> select cast('true' as numeric);
0

取自http://www.sqlite.org/lang_expr.html

要将 SQL 表达式的结果转换为布尔值,SQLite 首先以与 CAST 表达式相同的方式将结果转换为 NUMERIC 值。NULL 或零值(整数值 0 或实值 0.0)被认为是错误的。所有其他值都被认为是真的。

因此,我建议您将真/假列更改为数字类型,并将 1 存储为真,将 0 存储为假。

于 2012-04-25T10:02:26.030 回答