Recentemente ho iniziato a creare applicazioni Android con Xamarin. Provo a creare un piccolo database locale con SQLite. Ho usato il seguente tutorial dal Xamarin documentation website.Operatore '! =' Non può essere applicato agli operandi di tipo 'Task' e 'int'
Purtroppo ho un errore:
Error CS0019: Operator '!=' cannot be applied to operands of type 'Task' and 'int' (CS0019)
Il mio codice è il seguente:
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;
}
}
E la classe Persona è la seguente:
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);
}
}
}
Qualcuno può aiutarmi con questo errore?
grazie, questo è stato l'errore. E devo anche aspettare, giusto? –
No, perché l'attesa si verifica in InsertAsync, credo. Per farlo è necessario chiamarlo con il proprio metodo asincrono e attendere la risposta e quindi restituire l'int con cui confrontarsi. –
Questo è molto pericoloso e potrebbe causare blocchi morti. Usa 'attendi'! –