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.

5
  • 1
    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. Commented Jan 20, 2017 at 11:12
  • 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. Commented Jan 20, 2017 at 11:14
  • Also make your ID nullable; public int? ID { get; set; } Commented Jan 20, 2017 at 11:17
  • 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. Commented Jan 21, 2017 at 9:57
  • Probably your table has different column name, not "id" but smth else? If this is the case - you can specify Attribute [Column("<column_name>")] Commented Oct 26, 2019 at 11:19

6 Answers 6

6

Please try using this, this worked for me

using SQLite.Net.Attributes;

 [PrimaryKey, AutoIncrement] public int? Id { get; set; } 
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

Comments

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

2 Comments

Could you explain how to express that in terms of Xamarin?
@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.
0

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.

Comments

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);

1 Comment

You know this is not an answer, please don't post comments as answers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.