运算符 '!=' 不能应用于类型为 'Task' 和 'int' 的操作数 [英] Operator '!=' cannot be applied to operands of type 'Task' and 'int'

查看:24
本文介绍了运算符 '!=' 不能应用于类型为 'Task' 和 'int' 的操作数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我开始使用 Xamarin 创建 Android 应用程序.我尝试使用 SQLite 创建一个小型本地数据库.我使用了来自 Xamarin 文档网站的以下教程.

Recently I started creating Android applications with Xamarin. I try to create a small local database with SQLite. I used the following tutorial from the Xamarin documentation website.

不幸的是我收到一个错误:

Unfortunately I get an error:

错误 CS0019:运算符 '!=' 不能应用于类型为 'Task' 和 'int' (CS0019) 的操作数

Error CS0019: Operator '!=' cannot be applied to operands of type 'Task' and 'int' (CS0019)

我的代码如下:

private string createDatabase(string path)
    {
        try
        {
            var connection = new SQLiteAsyncConnection(path);{
                connection.CreateTableAsync<Person>();
                return "Database created";
            }
        }
        catch (SQLiteException ex)
        {
            return ex.Message;
        }
    }

    private string insertUpdateData(Person data, string path)
    {
        try
        {
            var db = new SQLiteAsyncConnection(path);
            if ( db.InsertAsync(data) != 0)
                db.UpdateAsync(data);
            return "Single data file inserted or updated";
        }
        catch (SQLiteException ex)
        {
            return ex.Message;
        }
    }

    private int findNumberRecords(string path)
    {
        try
        {
            var db = new SQLiteConnection(path);
            // this counts all records in the database, it can be slow depending on the size of the database
            var count = db.ExecuteScalar<int>("SELECT Count(*) FROM Person");

            // for a non-parameterless query
            // var count = db.ExecuteScalar<int>("SELECT Count(*) FROM Person WHERE FirstName="Amy");

            return count;
        }
        catch (SQLiteException ex)
        {
            return -1;
        }
    }

Person 类如下:

And the Person class is the following:

using System;
using SQLite;

namespace HelloWorld {
public class Person
{
    [PrimaryKey, AutoIncrement]
    public int ID { get; set; }

    public string FullName { get; set; }

    public string CarLicense { get; set; }

    public override string ToString()
    {
        return string.Format("[Person: ID={0}, FullName={1}, CarLicense={2}]", ID, FullName, CarLicense);
    }
}
}

有人能帮我解决这个错误吗?

Can anybody help me with this error?

推荐答案

你可以这样做:

if ( (await db.InsertAsync(data)) != 0)

另请参阅 此答案 了解为什么 await 通常优于使用 的原因.结果.

Also see this answer for reasons why await is often preferred over using .Result.

这篇关于运算符 '!=' 不能应用于类型为 'Task' 和 'int' 的操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆