![]() ![]() – Restrict the length of a text property, when a database insert is attempted.– Marks the class as being able to be stored in an underlying SQLite table with the name specified.Sets the underlying database column's name. – This attribute will cause an integer property’s value to be auto-increment for each new object inserted into the database.Composite primary keys are not supported. – This attribute can be applied to an integer property to force it to be the underlying table’s primary key.SQLite AttributesĬommon attributes that you can apply to your classes to control how they are stored in the underlying database include: Similarly the attribute is optional, and if absent a column will be added to the table with the same name as the property in the class. The actual table name is important if you write SQL queries directly against the database rather than use the ORM data access methods. Using the attribute without specifying a table name parameter will cause the underlying database table to have the same name as the class (in this case, “Stock”). only insert the data if it doesn't already existĬonsole.WriteLine (s.Id + " " + s.Symbol) Ĭonsole.WriteLine ("Creating database, if it doesn't already exist") Įnvironment.GetFolderPath (), Note that the SQLite database table is defined by adding attributes to a class (the Stock class) rather than a CREATE TABLE command. This requires that you have added SQLite to your project, as highlighted here. You’ll need to include these namespaces: using SQLite // from the github SQLite.cs class Inserting some data by creating objects and then saving them.The following code sample shows an entire database interaction using the SQLite.NET library to encapsulate the underlying database access. The code illustrates how to perform simple SQLite.NET operations and shows the results in as text in the application’s main window. The DataAccess_Basic sample code for this document looks like this when running on iOS. Retrieve Data - To retrieve an object (or a list of objects) use the following syntax: var stock = db.Get(5) // primary key id of 5 Save Data - Once you have created a SQLiteConnection object, database commands are executed by calling its methods, such as CreateTable and Insert like this: db.CreateTable () ĭb.Insert (newStock) // after creating the newStock object The dbPath variable should be determined according the rules discussed earlier in this document. ![]() You do not need to check if the file already exists – it will automatically be created if required, otherwise the existing database file will be opened. Once you have the SQLite.NET library available, follow these three steps to use it to access a database:Īdd a using statement - Add the following statement to the C# files where data access is required: using SQLite Ĭreate a Blank Database - A database reference can be created by passing the file path the SQLiteConnection class constructor. SQLite.NET is a third-party library that's supported from the praeclarum/sqlite-net repo. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |