Home > Mobile >  Xamarin sqlite cannot use struct as table data type
Xamarin sqlite cannot use struct as table data type

Time:10-27

Using sqlite-net-pcl 1.8.116 If I define a struct and make a table in the database from that, I cannot read back values from it without getting a System.ArgumentException: 'method arguments are incompatible'. I can make the table and add rows. Only the readback fails.

If I make my struct into a class, it works without issues. I have looked and looked to see if structs are simply not supported, but I have not seen anything indicating that, or even others having the same issue.

I create and use the database like this, which fails

var db = new SQLiteAsyncConnection(path);
db.CreateTableAsync<Flight>().Wait();
var affected = await db.InsertAsync(new Flight());
var get = await db.Table<Flight>().ToListAsync(); //exception

I can do the same, with the same exception, with a sqlQuery like

var flights = await db.QueryAsync<Flight>("select * from Flight where FlightID==?",id);

A trimmed down structure for this post is

public struct Flight
{
  [PrimaryKey, AutoIncrement]
  public int Id { get; set; }
    
  public long Start { get; set; }
  public long End { get; set; }
  public string Description { get; set; }    
}
     

If I change the struct to a class, there are no issues. Are structs somehow just not supported like this?

CodePudding user response:

Yes,it is the case as you said when we use nuget sqlite-net-pcl 1.8.116. But you can try to use version 1.7.335 of nuget sqlite-net-pcl. When I created a table and insterted some items using version 1.7.335, there was no such error.

And I posted a new issue about this problem, you can follow it up here: https://github.com/praeclarum/sqlite-net/issues/1075.

Thanks for your feedback for xamarin.

Best Regards!

  • Related