3

I try to use the SQLite Library for WP8.

I defined a table:

class ShoppingItem { [SQLite.PrimaryKey] public int Id {get; set;} public string Name {get; set;} public string Shop {get; set;} public bool isActive {get; set;} } 

According to http://www.sqlite.org/autoinc.html a value is assigned to Id automatically by sqlite, if no value for Id has been given. Right?

So I try to insert new entries by

using (var db = new SQLiteConnection(m_DatabasePath)) { db.Insert(new ShoppingItem() { Name = anItem, Shop = aShop, isActive = aIsActive, }); } 

When I insert a first item, it gets the ID 0. Well, why not. When I insert a second item, i get a SQLiteException with the awesome message "Constraint". So how can I insert a new entry without giving an id?

btw.: As an alternative solution, I tried to add a null value to id, but this resulted in a compilation error. :(

... db.Insert(new ShoppingItem() { Id = null, // <- does not compile Name = anItem, ... 
2
  • Is your ID an AUTOINCREMENT in your table in your database? Commented Mar 18, 2013 at 10:02
  • No, but I do not think this is necessary, according to sqlite.org/autoinc.html Commented Mar 18, 2013 at 10:05

1 Answer 1

4

Your ShoppingItem does not know that the field is autoincrementing, so it assigns a default value of zero.

Use:

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

2 Comments

yeah, it works, thank you. But I don't understand why I have to add the AutoIncrement flag, as the AutoIncrement should be done automatically by SQLite?! Or is my problem a matter of the SQLite lib for C#?
The problem is that C# automatically assigned a value.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.