SQLite-net-pcl - Always returning ID as 0 (Xamarin)
Asked Answered
E

6

7

I recently moved across from SQLite.NET to SQLite-net-pcl due to the Android 7.0 SQlite issue.

In the process I have updated all my libraries and all is working well with regards to insert/drop etc.

The only issue I have is that every time I retrieve an item from the DB it always has an ID of 0. This is causing problems with updating items. Has anyone had this issue before?

 [SQLite.PrimaryKey, SQLite.AutoIncrement]
    public int ID { get; set; }
    public string objectId { get; set; }
    public DateTime createdAt { get; set; }
    public DateTime updatedAt { get; set; }
    public string Title { get; set; }

Thanks in advance.

Englishman answered 20/1, 2017 at 11:2 Comment(5)
Make sure that you're not using InsertOrReplace with an AutoIncrement primary key. It will always override item with id 0 and never insert a new one.Excalibur
In addition, I think that SQLite-Net namespace is SQLite.Net, in your sample you're using SQLite, maybe your primary key attribute is not being recognized.Excalibur
Also make your ID nullable; public int? ID { get; set; }Heterocercal
Thanks for the answers guys but none of these seem to be working. I seem to have a very strange issue. Definitely Xamarin related. I cant build and run the app in debug and release. All works perfectly. When I package my app I have either 1 of 3 scenarios. 1. App crashes on launch, 2. App works but doesn't pull all data, next load crashes, wipe the data for that app and works perfect. 3. Doesn't install.Englishman
Probably your table has different column name, not "id" but smth else? If this is the case - you can specify Attribute [Column("<column_name>")]Apologete
E
6

Please try using this, this worked for me

using SQLite.Net.Attributes;

  [PrimaryKey, AutoIncrement]
    public int? Id { get; set; }
Ecliptic answered 28/9, 2017 at 16:49 Comment(0)
S
2

Had almost the same situation. with all rows returning id of 0

class ToDo : Java.Lang.Object
{
    [PrimaryKey, AutoIncrement]
    public int ID { get; }

except I simply had forgotten to type set; in the property.

one thing that might help someone with this sorta problem is using the GetMapping() method to get some info on the mapping used when CreateTable() is used to create the table. as example:

        Toast.MakeText(this, db.GetMapping<ToDo>().TableName.ToString(), ToastLength.Long).Show();
        Toast.MakeText(this, db.GetMapping<ToDo>().HasAutoIncPK.ToString(), ToastLength.Long).Show();

using this I found out that AutoIncrement (HasAutoIncPK = false) wasn't being set on my table.

Sager answered 9/8, 2017 at 9:53 Comment(0)
K
0

See if you created the table with the methods CreateComand(query) and ExecuteNonQuery(). If this is the case, create your table with the CreateTable<Type>() method. The primary key and autoincrement attributes are initialized at the time of creating the table through said method

Kingsbury answered 18/7, 2017 at 4:33 Comment(0)
I
0

I have been struggling with the same issue here.

I was manually creating the tables to guarantee a smoother update process moving forwards rather than using the CreateTable methods available.

The fix that I eventually stumbled upon was that I was using the wrong data type for my PRIMARY KEY column.

Wrong definition

[RecordIndexId] int PRIMARY KEY NOT NULL

Correct definition

[RecordIndexId] integer PRIMARY KEY NOT NULL

To add a little context there is a big different between the int and integer data types. Explained in this SO answer

Insouciance answered 7/2, 2018 at 13:19 Comment(2)
Could you explain how to express that in terms of Xamarin?Giess
@AlexanderGorg I am not sure I understand what you are looking for. The issue I mentioned relates to the database table that you create in SQLite.Insouciance
L
0

Make sure that you're not using InsertOrReplace with an AutoIncrement primary key. It will always override item with id 0 and never insert a new one. -redent84

All database entries need to be added using await _database.InsertAsync(myClass);

Loosejointed answered 4/1 at 2:43 Comment(1)
You know this is not an answer, please don't post comments as answers.Oquendo
Z
-1

My problem was that I had an internal set for my ID property setter. This had to be public to work correctly.

I'd add this as a comment but alas... I don't have enough rep.

Zodiac answered 18/11, 2019 at 21:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.