Get Generated Primary Key Sqlite Android
- Get Generated Primary Key Sqlite Android 2017
- Sqlite Change Primary Key
- Sqlite Primary Key Unique
- Sqlite Name Primary Key
- Get Generated Primary Key Sqlite Android Download
- Sqlite String As Primary Key
If you don’t have any requirement like this, you should not use the AUTOINCREMENT attribute in the primary key. In this tutorial, you have learned how SQLite AUTOINCREMENT attribute works and how it influences the way SQLite assigns values to the primary key column. See the separate documentation for details on the capabilities and limitations of generated columns. The PRIMARY KEY. Each table in SQLite may have at most one PRIMARY KEY. If the keywords PRIMARY KEY are added to a column definition, then the primary key for the table consists of that single column. SQLite autoincrement FAQ: How do I get the autoincrement value from my last SQLite INSERT command? You can get the integer value of the primary key field from the last insert into an autoincrement field using a SQLite function named lastinsertrowid, as shown in the example below.
Xamarin 'in önerdiği SQLite.NET kitaplığı, bir Android cihazında yerel SQLite veritabanındaki nesneleri kolayca depolamanıza ve almanızı sağlayan çok temel bir ORM 'dir.The SQLite.NET library that Xamarin recommends is a very basic ORM that lets you easily store and retrieve objects in the local SQLite database on an Android device.ORM, SQL deyimleri yazmadan bir veritabanından 'Objects' i kaydetmenizi ve almanızı sağlayan bir API – nesne Ilişkisel eşlemesini temsil eder.ORM stands for Object Relational Mapping – an API that lets you save and retrieve 'objects' from a database without writing SQL statements.
SQLite.NET kitaplığını bir Xamarin uygulamasına eklemek için aşağıdaki NuGet paketini projenize ekleyin:To include the SQLite.NET library in a Xamarin app, add the following NuGet package to your project:
- Paket adı: SQLite-net-PCLPackage Name: sqlite-net-pcl
- Yazar: Filiz A. KruegerAuthor: Frank A. Krueger
- Kimlik: sqlite-net-pclId: sqlite-net-pcl
- URL:NuGet.org/packages/SQLite-net-PCLUrl:nuget.org/packages/sqlite-net-pcl
İpucu
Kullanılabilir sayıda farklı SQLite paketi vardır: doğru olanı seçtiğinizden emin olun (aramada en üstteki sonuç olmayabilir).There are a number of different SQLite packages available – be sure to choose the correct one (it might not be the top result in search).
SQLite.NET kitaplığı kullanılabilir olduğunda, bir veritabanına erişmek için bu üç adımı izleyin:Once you have the SQLite.NET library available, follow these three steps to use it to access a database:
Using Ifadesini ekleyin – veri erişiminin gerekli olduğu C# dosyalara aşağıdaki ifadeyi ekleyin:Add a using statement – Add the following statement to the C# files where data access is required:
Boş bir veritabanı oluşturun – SQLiteConnection sınıf oluşturucusunun dosya yolu geçirerek veritabanı başvurusu oluşturulabilir.Create a Blank Database – A database reference can be created by passing the file path the SQLiteConnection class constructor.Dosyanın zaten mevcut olup olmadığını kontrol etmeniz gerekmez – gerektiğinde otomatik olarak oluşturulur, aksi takdirde var olan veritabanı dosyası açılır.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.
dbPath
değişkeni, bu belgede daha önce açıklanan kurallara göre belirtilmelidir:ThedbPath
variable should be determined according the rules discussed earlier in this document:Verileri kaydetme – bir SQLiteConnection nesnesi oluşturduktan sonra veritabanı komutları, CreateTable gibi yöntemleri çağırarak yürütülür ve şunun gibi INSERT:Save Data – Once you have created a SQLiteConnection object, database commands are executed by calling its methods, such as CreateTable and Insert like this:
Veri alma – bir nesneyi (veya bir nesne listesini) almak için aşağıdaki sözdizimini kullanın:Retrieve Data – To retrieve an object (or a list of objects) use the following syntax:
Temel veri erişimi örneğiBasic Data Access Sample
Bu belgenin DataAccess_Basic örnek kodu, Android üzerinde çalışırken bu şekilde görünür.The DataAccess_Basic sample code for this document looks like this when running on Android.Kod, basit SQLite.NET işlemlerinin nasıl gerçekleştirileceğini gösterir ve sonuçları uygulamanın ana penceresinde metin olarak gösterir.The code illustrates how to perform simple SQLite.NET operations and shows the results in as text in the application's main window.
Outlook Web Access (OWA)Android
Aşağıdaki kod örneği, temel alınan veritabanı erişimini kapsüllemek için SQLite.NET kitaplığını kullanarak tüm veritabanı etkileşimini gösterir.The following code sample shows an entire database interaction using the SQLite.NET library to encapsulate the underlying database access.Şunu gösterir:It shows:
Veritabanı dosyası oluşturuluyorCreating the database file
Nesneler oluşturarak ve sonra kaydederek bazı verileri eklemeInserting some data by creating objects and then saving them
Veriyi sorgulamaQuerying the data
Bu ad alanlarını eklemeniz gerekir:You'll need to include these namespaces:
Son bir, projenize SQLite ' i eklemiş olmanızı gerektirir.The last one requires that you have added SQLite to your project.SQLite veritabanı tablosunun, bir CREATE TABLE komutu yerine bir sınıfa (Stock
sınıfı) öznitelikler eklenerek tanımlandığını unutmayın.Note that the SQLite database table is defined by adding attributes to a class (the Stock
class) rather than a CREATE TABLE command.
Tablo adı parametresi belirtmeden [Table]
özniteliği kullanılması, temel alınan veritabanı tablosunun sınıfla aynı ada sahip olmasına neden olur (Bu durumda, 'stok').Using the [Table]
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').Asıl tablo adı, ORM veri erişim yöntemlerini kullanmak yerine doğrudan veritabanına karşı SQL sorguları yazarsanız önemlidir.The actual table name is important if you write SQL queries directly against the database rather than use the ORM data access methods.Benzer şekilde, [Column('_id')]
özniteliği isteğe bağlıdır ve bir sütunun yok olması, sınıftaki özelliği ile aynı ada sahip tabloya eklenmez.Similarly the [Column('_id')]
attribute is optional, and if absent a column will be added to the table with the same name as the property in the class.
SQLite öznitelikleriSQLite Attributes
Temel alınan veritabanında nasıl depolandığını denetlemek için sınıflarınıza uygulayabileceğiniz ortak öznitelikler şunlardır:Common attributes that you can apply to your classes to control how they are stored in the underlying database include:
[PrimaryKey] – bu öznitelik, temel alınan tablonun birincil anahtarı olmasını zorlamak için bir tamsayı özelliğine uygulanabilir.[PrimaryKey] – This attribute can be applied to an integer property to force it to be the underlying table's primary key.Bileşik birincil anahtarlar desteklenmiyor.Composite primary keys are not supported.
[AutoIncrement] – bu öznitelik, bir tamsayı özelliğin değerinin veritabanına yerleştirilen her yeni nesne için otomatik olarak artışa neden olacak[AutoIncrement] – This attribute will cause an integer property's value to be auto-increment for each new object inserted into the database
[Sütun (ad)] –
name
parametresi, temel alınan veritabanı sütununun adını ayarlar.[Column(name)] – Thename
parameter sets the underlying database column's name.[Table (Name)] –, sınıfı, belirtilen ada sahip bir temel bir SQLite tablosunda depolanabilecek şekilde işaretler.[Table(name)] – Marks the class as being able to be stored in an underlying SQLite table with the name specified.
[MaxLength (değer)] – bir veritabanı eklemesi denendiğinde metin özelliğinin uzunluğunu kısıtlayın.[MaxLength(value)] – Restrict the length of a text property, when a database insert is attempted.Bir veritabanı ekleme veya güncelleştirme işlemi denendiğinde, bu öznitelik yalnızca ' Checked ' olduğundan, nesne eklemeden önce bu özniteliğin kullanılması gerekir.Consuming code should validate this prior to inserting the object as this attribute is only 'checked' when a database insert or update operation is attempted.
[IGNORE] – SQLite.net 'in bu özelliği yoksaymasına neden olur.[Ignore] – Causes SQLite.NET to ignore this property.Bu, özellikle veritabanında depolanabilecek bir tür veya SQLite tarafından otomatik olarak çözümlenemeyen koleksiyonları modelleyerek özellikler için yararlıdır.This is particularly useful for properties that have a type that cannot be stored in the database, or properties that model collections that cannot be resolved automatically by SQLite.
[Unique] –, temel alınan veritabanı sütunundaki değerlerin benzersiz olmasını sağlar.[Unique] – Ensures that the values in the underlying database column are unique.
Bu özniteliklerin çoğu isteğe bağlıdır.Most of these attributes are optional.Seçim ve silme sorgularının verilerinize etkin bir şekilde gerçekleştirilebilmesi için her zaman bir tamsayı birincil anahtar belirtmelisiniz.You should always specify an integer primary key so that selection and deletion queries can be performed efficiently on your data.
Daha karmaşık sorgularMore Complex Queries
SQLiteConnection
üzerindeki aşağıdaki yöntemler diğer veri işlemlerini gerçekleştirmek için kullanılabilir:The following methods on SQLiteConnection
can be used to perform other data operations:
Ekle – veritabanına yeni bir nesne ekler.Insert – Adds a new object to the database.
Birincil anahtarı kullanarak bir nesneyi alma girişimlerini – <t>alın .Get<T> – Attempts to retrieve an object using the primary key.
Tablo<t> – tablodaki tüm nesneleri döndürür.Table<T> – Returns all the objects in the table.
Silme –, birincil anahtarını kullanarak bir nesneyi siler.Delete – Deletes an object using its primary key.
Sorgu<t> – bir dizi satırı döndüren bir SQL sorgusu gerçekleştirir (nesneler olarak).Query<T> – Perform an SQL query that returns a number of rows (as objects).
Yürütün – SQL 'den geri DÖNMEZSENIZ (INSERT, Update ve DELETE yönergeleri gibi) bu yöntemi kullanın (
Query
değil).Execute – Use this method (and notQuery
) when you don't expect rows back from the SQL (such as INSERT, UPDATE and DELETE instructions).
Birincil anahtar tarafından nesne almaGetting an object by the primary key
SQLite.Net, birincil anahtarını temel alan tek bir nesneyi almak için get yöntemini sağlar.SQLite.Net provides the Get method to retrieve a single object based on its primary key.
LINQ kullanarak nesne seçmeSelecting an object using Linq
Koleksiyonu döndüren yöntemler IEnumerable<T>
destekler, böylece LINQ kullanarak bir tablonun içeriğini sorgulayabilir veya sıralayabilirsiniz.Methods that return collections support IEnumerable<T>
so you can use Linq to query or sort the contents of a table.Aşağıdaki kod, 'A' harfiyle başlayan tüm girişleri filtrelemek için LINQ kullanarak bir örnek gösterir:The following code shows an example using Linq to filter out all entries that begin with the letter 'A':
SQL kullanarak nesne seçmeSelecting an object using SQL
SQLite.Net, verilerinize nesne tabanlı erişim sağlayabilse de bazen LINQ 'ın izin verdiğinden daha karmaşık bir sorgu yapmanız gerekebilir (veya daha hızlı performans gerekebilir).Even though SQLite.Net can provide object-based access to your data, sometimes you might need to do a more complex query than Linq allows (or you may need faster performance).SQL komutlarını sorgu yöntemiyle birlikte aşağıda gösterildiği gibi kullanabilirsiniz:You can use SQL commands with the Query method, as shown here:
Get Generated Primary Key Sqlite Android 2017
Not
Doğrudan SQL deyimlerini yazarken, veritabanınızdaki tablo ve sütun adlarında, sınıflarınızdaki ve onların özniteliklerinin oluşturulduğu bir bağımlılık oluşturursunuz.When writing SQL statements directly you create a dependency on the names of tables and columns in your database, which have been generated from your classes and their attributes.Kodunuzda bu adları değiştirirseniz, el ile yazılmış herhangi bir SQL deyimini güncelleştirmeyi unutmayın.If you change those names in your code you must remember to update any manually written SQL statements.
Sqlite Change Primary Key
Nesne silmeDeleting an object
Birincil anahtar, burada gösterildiği gibi satırı silmek için kullanılır:The primary key is used to delete the row, as shown here:
Kaç satır etkilendiğini (Bu durumda silindi) onaylamak için rowcount
kontrol edebilirsiniz.You can check the rowcount
to confirm how many rows were affected (deleted in this case).
Birden çok Iş parçacığı ile SQLite.NET kullanmaUsing SQLite.NET with Multiple Threads
Sqlite Primary Key Unique
SQLite üç farklı iş parçacığı modunu destekler: tek iş parçacığı, Çoklu Iş parçacığıve serileştirilmiş.SQLite supports three different threading modes: Single-thread, Multi-thread, and Serialized.Herhangi bir kısıtlama olmadan veritabanına birden çok iş parçacığından erişmek istiyorsanız, SQLite 'u serileştirilmiş iş parçacığı modunu kullanacak şekilde yapılandırabilirsiniz.If you want to access the database from multiple threads without any restrictions, you can configure SQLite to use the Serialized threading mode.Bu modu uygulamanızda erken ayarlamanız önemlidir (örneğin, OnCreate
yönteminin başlangıcında).It's important to set this mode early in your application (for example, at the beginning of the OnCreate
method).

Sqlite Name Primary Key
İş parçacığı modunu değiştirmek için SqliteConnection.SetConfig
çağırın.To change the threading mode, call SqliteConnection.SetConfig
.Örneğin, bu kod satırı seri hale getirilmiş mod için SQLite 'u yapılandırır:For example, this line of code configures SQLite for Serialized mode:
SQLite Android sürümünde birkaç adım daha gerektiren bir sınırlama vardır.The Android version of SQLite has a limitation that requires a few more steps.SqliteConnection.SetConfig
çağrısı library used incorrectly
gibi bir SQLite özel durum üretirse, aşağıdaki geçici çözümü kullanmanız gerekir:If the call to SqliteConnection.SetConfig
produces a SQLite exception such as library used incorrectly
, then you must use the following workaround:
Get Generated Primary Key Sqlite Android Download
sqlite3_shutdown
vesqlite3_initialize
API 'Lerinin uygulama için kullanılabilir hale getirilmeleri için yerel libsqlite.so kitaplığına bağlanın:Link to the native libsqlite.so library so that thesqlite3_shutdown
andsqlite3_initialize
APIs are made available to the app:OnCreate
yönteminin en başında, bu kodu kapalı SQLite öğesine ekleyin, seri hale getirilmiş mod için yapılandırın ve SQLite 'u yeniden başlatın:At the very beginning of theOnCreate
method, add this code to shutdown SQLite, configure it for Serialized mode, and reinitialize SQLite:
Sqlite String As Primary Key
Bu geçici çözüm Mono.Data.Sqlite
kitaplığı için de geçerlidir.This workaround also works for the Mono.Data.Sqlite
library.SQLite ve çoklu iş parçacığı hakkında daha fazla bilgi için bkz. SQLite ve birden çok Iş parçacığı.For more information about SQLite and multi-threading, see SQLite and Multiple Threads.