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?
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With