SQLite Attributes and what they do

If like myself, you started using SQLite and just looked up each attribute for the models you're using when and as you needed them, then fine. However you might not realise that the others are there and how they can help you.

Here's a quick summary of the types of attributes available for use (for those of you that don't like reading and just want to know quickly):

  • PrimaryKey This property is the primary key of the table. Only single-column primary keys are supported.

  • AutoIncrement This property is automatically generated by the database upon insert.

  • Indexed This property should have an index created for it.

  • MaxLength If this property is a String then MaxLength is used to specify the varchar max size. The default max length is 140.

  • Ignore This property will not be in the table.

To apply an attribute, simply apply them as below, changing PrimaryKey, AutoIncrement for your chosen attributes.

[PrimaryKey, AutoIncrement]
public Int32 ID { get; set; }

So we've found that there are 5 main attributes to work with and how to use them. So what can they be used for?

PrimaryKey

The Primary key is most commonly used alongside AutoIncrement for obvious reasons, this allows you to have a row inserted into your database with an incrementing integer value to which you can reference from another table with the use of a foreign key. Note that the use of PrimaryKey infers that it should also be Indexed, though you do not need to specify this as an index is automatically created for a primary key.

AutoIncrement

Does what it says on the tin, the propertytype should be an integer and should also be marked with the PrimaryKey attribute.

Indexed

Allows your application to quickly lookup a value within the Indexed column.

MaxLength

Limits the available character length for the column.

Ignore

Useful if you've got a large model or information you don't want to store on the device for security purposes or simply a data field that is too complex to fit into a single column.

 

Bonus attibute: Table

There is another attribute called Table which you can apply to the entire model (class level) that you can use to define table names:

[Table("Members")]

 

Enjoyed this post, found it useful or have something to add?

Leave a comment below. Also check out this other post about setting up tables in SQLite and making use of the primary key.


By Luke Alderton at 3 Aug 2016, 06:37 AM

Tags: SQLite,Xamarin

Comments

Kaiyue
Nice post! Could you please tell me where can I find documentation about this? And can I make use of other attributes like ColumnAttribute or else?
08/02/2017
Kaiyue
Nice post! Could you please tell me where can I find documentation about this? And can I make use of other attributes like ColumnAttribute or else?
08/02/2017
Post a comment
Sit tight...
We're adding your comment.
Thank you.
Your comment has been added.
There's been a problem.
Please try again later.
Share with
Tags
Latest Comments
By dipbleds on Raspberry Pi - Bluetooth using Bluecove on Raspbian
12 Dec 2017, 21:29 PM
By Ich on Replacing the Xamarin Header/Navigation bar with a custom view/template
26 Nov 2017, 17:11 PM
By Ravi Motha on My experience at Umbraco UK Festival 2017
12 Dec 2017, 08:39 AM
By Borges on How to add a Xamarin Forms Loading Screen/Overlay
7 Nov 2017, 19:11 PM
By Rutul Mehta on Auto/Custom height on Xamarin Forms WebView for Android and iOS
4 Nov 2017, 03:15 AM
By Pablo on Xamarin MasterDetailPage has large margin/padding at top
26 Oct 2017, 15:31 PM
By faiza on Using MaryTTS or OpenMary in Java
22 Oct 2017, 12:54 PM
By Nietoperz on Using MaryTTS or OpenMary in Java
29 Sep 2017, 14:34 PM
Categories
App Development
Event
Game Development
Mapping
Modelling
Programming
Review
Robotics
Tutorial
Web Development