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.


Published at

Tags: SQLite,Xamarin

Luke Alderton

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
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 Alexander Gontar on Xamarin Forms - Working with threads
13 Mar 2020, 07:10 AM
By Arthur Richardson on Disabling XAML Hot Reload
12 Mar 2020, 17:52 PM
By George on Xamarin Forms - Working with threads
10 Mar 2020, 05:04 AM
By Darío on Xamarin forms - Stop/Cancel back button event
1 Mar 2020, 03:00 AM
By Benjamin Roberts on Simple fix for 'Cannot bind source type Umbraco.Web.Models.RenderModel to model type xx'
17 Feb 2020, 19:45 PM
By Chris Boot on Xamarin Forms multiple instances of same app open
26 Jan 2020, 17:10 PM
By Hannah on Xamarin forms - Stop/Cancel back button event
14 Jan 2020, 02:28 AM
By SK on Xamarin forms - Stop/Cancel back button event
14 Dec 2019, 02:06 AM
Categories
App Development
Event
Game Development
Mapping
Modelling
Programming
Review
Robotics
Tutorial
Web Development