Development issue/problem:
I started using the Xamarin plugin for Visual Studio to create an Android application.
I have a local SQL database and I want to call it to view the data. I don’t know how to do it. Is it possible?
How can I solve this problem?
Solution 1:
After what I thought was a trivial thing, I was proven wrong when I tried to set up a quick test project. This post contains a comprehensive guide to setting up a database for pp on Xamarin, which will be useful for future Xamarin users.
At a glance:
- Add Sqlite.cs to your project.
- Add the database file as an asset.
- Install the database file on the build under the name AndroidAsset.
- Manually copy the database file of your apk to another folder.
- Open the database connector with Sqlite.SqliteConnection.
- Working with the database with Sqlite.
Configuring the local database for the Xamarin Android project
1. Add Sqlite.cs to your project.
First, go to this repository and download Sqlite.cs; this provides a Sqlite API that you can use to run queries on your database. Insert the file as a source file in the project.
2. Add DB as active.
Next, retrieve your strip and copy it to the Assets folder of your Android project, then import it into your project so that it appears under the Assets folder of your solution:
In this example, I am using the sample database Chinook_Sqlite.sqlite, renamed to db.sqlite from this website.
3. Install the DB on the assembly as an AndroidAsset.
Right-click on the database file and specify that it should create an AndroidAsset action. This will ensure that it is included in the KPA’s inventory.
4. Copy the database manually from your APK.
Since the database is included as an asset (packaged in the APK), you need to extract it.
You can do it with the following code:
string dbName = db.sqlite;string dbPath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.ToString (), dbName);// Check if your database is already unpacked.if (!File.Exists(dbPath)){using (BinaryReader br = new BinaryReader(Android.App.Application.Context.Assets.Open(dbName)))).{using (BinaryWriter bw = new BinaryWriter(new FileStream(dbPath, FileMode).Create)))){byte[] buffer = new byte [2048];int len = 0;while ((len = br.Read(buffer, 0, buffer.Length)) > 0){bw.Write (buffer, 0, len);}}}} }}
The database is extracted from the APK as a binary file and placed in the system path of the remote storage. Actually the DB can go anywhere, I just decided to post it here.
I also read that Android has a database folder that stores databases directly; I couldn’t get it to work, so I just ran it with the existing database.
5. DB socket open.
Now open a connection to the database using the Sqlite.SqliteConnection class:
using (var conn = new SQLite.SQLiteConnection(dbPath))
{
// Do something here….
}
6. Work on the DB.
Finally, since Sqlite.net is an ORM, you can work with the database using your own data types:
public class Album [
[PrimaryKey, AutoIncrement]
public in AlbumId [get; set; }
public channel Title { get; set; }
public in ArtistId { get; set; }
}
// Other code…
with (var conn = new SQLite.SQLiteConnection(dbPath))
{
var cmd = new SQLite.SQLiteCommand (conn);
cmd.CommandText = choose * from Album;
var r = cmd.ExecuteQuery () ;
Console.write(r);
}
Summary
And here’s how to add an existing Sqlite database to your Xamarin for Android solution! For more information, see the examples in the Sqlite.net library, the unit tests, and the examples in the Xamarin documentation.
Solution 2:
This is the one I use, and it works.
- Install the Sqlite plugin
- create an interface to access services from different platforms
- Create a template for the table
- Implement the interface you created earlier on the entire
platform you want to use. - Use the plugin to create, retrieve, paste, etc. on your desktop
For more information you can visit the following site
Good luck!
Related Tags:
xamarin forms sqlite existing database, xamarin forms cloud database, xamarin sqlite multiple tables, xamarin connect to remote database, sqlite-net-pcl, crud operation in xamarin forms using sqlite, xamarin host web service, xamarin android sqlite tutorial, xamarin web, xamarin forms web api, xamarin form todo, todo app in xamarin, simple login page in xamarin forms, how to view sqlite database in xamarin forms, getpeopleasync, xamarin cross platform sqlite example, sqlite interface xamarin, xamarin android crud sqlite, create form for sqlite, sqlite-net-pcl xamarin example, c# database singleton, how to connect database in xamarin android, sqlite mvvm xamarin forms, xamarin android sqlite insert, sqlite-net-pcl delete, sqlite-net-pcl serialized, sqlite create table c#, local database in xamarin forms, how to connect sql server database in xamarin forms, xamarin sqlite, xamarin database connection, xamarin android local database, xamarin forms database tutorial