2016-03-16 10 views
7

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?

risposta

7

Si può fare in questo modo:

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

vedere anche this answer per ragioni await è spesso preferito rispetto all'uso di .Result.

2

Questo sta dicendo che è necessario confrontare il risultato, invece del compito. Si prega di aggiornare questa linea:

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

a

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

grazie, questo è stato l'errore. E devo anche aspettare, giusto? –

+0

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. –

+1

Questo è molto pericoloso e potrebbe causare blocchi morti. Usa 'attendi'! –

1

È necessario aggiungere .result su db.InsertAsync (dati)

4

Invece di usare Result, che può causare morte-serrature, è dovrebbe attendere il risultato:

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

Ciò richiede che il metodo sia anch'esso asincrono, quindi il segno e dovrebbe leggere:

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

@L'utente senza numero - Questo dovrebbe essere contrassegnato come la migliore risposta per il prossimo sviluppatore con la stessa domanda. –

+0

Lascia che OP scelga la risposta migliore @ Mr.B –

2

È possibile utilizzare asincrona, attendere se si vuole che innsertUpdateData sarà asincrona.

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