运算符“!=”不能应用于 'Task' 和 'int' 类型的操作数
Operator '!=' cannot be applied to operands of type 'Task' and 'int'
最近我开始使用 Xamarin 创建 Android 应用程序。我尝试使用 SQLite 创建一个小型本地数据库。我使用了 Xamarin documentation website.
中的以下教程
不幸的是我得到一个错误:
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;
}
}
人 class 如下:
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);
}
}
}
谁能帮我解决这个错误?
这告诉你你需要比较结果,而不是任务。请更新此行:
if ( db.InsertAsync(data) != 0)
至
if ( db.InsertAsync(data).Result != 0)
您需要在 db.InsertAsync(data)
上添加 .result
不要使用可能导致死锁的Result
,您应该等待结果:
if (await db.InsertAsync(data) != 0)
{
await db.UpdateAsync(data);
}
这要求您的方法也是异步的,因此签名应为:
private async Task<string> insertUpdateData(...)
您可以使用 async,await 如果您希望 innsertUpdateData 是异步的。
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;
}
}
你可以这样做:
if ( (await db.InsertAsync(data)) != 0)
另请参阅 this answer,了解为什么 await
通常优于使用 .Result
的原因。
最近我开始使用 Xamarin 创建 Android 应用程序。我尝试使用 SQLite 创建一个小型本地数据库。我使用了 Xamarin documentation website.
中的以下教程不幸的是我得到一个错误:
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;
}
}
人 class 如下:
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);
}
}
}
谁能帮我解决这个错误?
这告诉你你需要比较结果,而不是任务。请更新此行:
if ( db.InsertAsync(data) != 0)
至
if ( db.InsertAsync(data).Result != 0)
您需要在 db.InsertAsync(data)
上添加 .result不要使用可能导致死锁的Result
,您应该等待结果:
if (await db.InsertAsync(data) != 0)
{
await db.UpdateAsync(data);
}
这要求您的方法也是异步的,因此签名应为:
private async Task<string> insertUpdateData(...)
您可以使用 async,await 如果您希望 innsertUpdateData 是异步的。
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;
}
}
你可以这样做:
if ( (await db.InsertAsync(data)) != 0)
另请参阅 this answer,了解为什么 await
通常优于使用 .Result
的原因。