【问题标题】:SQLite is not storing GUID values properly on MonoDroid and MonoTouchSQLite 没有在 MonoDroid 和 MonoTouch 上正确存储 GUID 值
【发布时间】:2013-04-14 22:28:45
【问题描述】:

我正在使用 MonoDroid 将数据层从我的 Window Mobile 应用程序移植到 Android 和 iOS。 我的数据层在 SQL SERVER、SQL CE 和 Windows SQLite 上运行并经过很好的测试。

我在我的 Android 解决方案中为我的项目创建了类库并导入了我的所有代码。 我创建了一个新的 DbEngine 实现来调整 Mono 上 SQLite 的查询。

当我通过诸如“插入 [user] 值('xx'、'hello'、null)'之类的文本插入表格时,它工作正常,我可以看到受影响的行 > 0。 但是,当我尝试使用命令和参数插入时,GUID 列中的值不正确,请查看屏幕截图。

// Creating the parameter
public override DbParameter CreateParameter(string name, object value)
{
    DbParameter result;
    if (value is byte[])
    {
        var length = (value as Array).Length;
        result = new SqliteParameter(name, DbType.Binary, length) { Value = value};
    }
    else if (value is string && ((string)value).Length > 4000)
    {
        var length = ((string)value).Length;
        result = new SqliteParameter(name, DbType.String, length) { Value = value };
    }
    else if (value is Guid)
    {
        result = new SqliteParameter(name, DbType.Guid) { Value = value }; // up to here, I can see that the GUID has the right value and the SQLiteParameter is created properly. 
    }
    else
    {
        result = new SqliteParameter(name, value);
    }
    return result;
}

这是作为参数传递时插入行的方式

【问题讨论】:

    标签: c# sqlite ado.net xamarin.ios xamarin.android


    【解决方案1】:

    经过长时间的斗争,Mono.Data.SQLite 似乎不支持GUID 参数。 我需要解决的问题是在我的代码中创建参数时检查它是否为GUID,然后创建string 类型的参数,如下所示:

    result = new SqliteParameter(name, value.ToString());
    

    这很好用,可以正确存储值,也可以用于查询数据库。 这非常令人沮丧,尤其是当 Xamarin 团队告诉我这是一个 Unicode 问题时 :( 希望它可以帮助别人。

    【讨论】:

      猜你喜欢
      • 2013-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-13
      • 2021-11-11
      • 1970-01-01
      • 1970-01-01
      • 2016-03-23
      相关资源
      最近更新 更多