Operator '!=' cannot be applied to operand

2020-04-04 14:43发布

问题:

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:

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

My code is the following:

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;
        }
    }

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?

回答1:

You can do it like this:

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

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



回答2:

Instead of using Result, which may cause dead-locks, you should await the result:

if (await db.InsertAsync(data) != 0)
{
    await db.UpdateAsync(data);
}

This requires your method to be async too, so the signature should read:

private async Task<string> insertUpdateData(...)


回答3:

This is telling you that you need to compare the result, instead of the task. Please update this line:

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

to

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


回答4:

You can use async, await if you want that innsertUpdateData will be asynchronous.

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


回答5:

You need to add .result on db.InsertAsync(data)