Forum Xamarin Xamarin.Android

How can i seeding a local SQLite database

imanmohadesi.6653imanmohadesi.6653 USMember ✭✭
edited April 2015 in Xamarin.Android

Hi,

I programmed an app that needs a lot of data . I need to publish this with local data in db (sqlite) , how can i do that?
please help me

Answers

  • imanmohadesi.6653imanmohadesi.6653 USMember ✭✭

    No answer?

  • CheesebaronCheesebaron DKInsider, University mod

    You are being very vague in your question, so don't expect any useful answers. Be more specific in what you are trying to achieve, what you have tried, what failed etc. No one is going to serve you any complete solution on a silver platter.

  • JulienMialonJulienMialon FRUniversity ✭✭

    You can probably put your database in your app resources or assets and copy it on the device at first launch. Or if you do not need to modify the data, just read the database from your resources anytime you need.

  • imanmohadesi.6653imanmohadesi.6653 USMember ✭✭

    Hi, Julien Mialon

    How can i do that ?
    I attached my simple project.
    Can u or anyone do it ??

  • imanmohadesi.6653imanmohadesi.6653 USMember ✭✭

    How about this ??

    Using a pre-populated seed SQLite database file
    Some applications are shipped with a database already populated with data. You can easily accomplish this in your mobile application by shipping an existing SQLite database file with your app and copying it to a writable directory before accessing it. Because SQLite is a standard file format that is used on many platforms, there are a number of tools available to create an SQLite database file:

    MonoDevelop – From the Tools > Database menu you can create or open SQLite files.
    SQLite Manager Firefox Extension – Works on Mac and Windows and produces files that are compatible with iOS and Android.
    Command Line – See www.sqlite.org/sqlite.html .
    When creating a database file for distribution with your app, take care with the naming of tables and columns to ensure they match what your code expects, especially if you’re using SQLite.NET which will expect the names to match your C# classes and properties (or the associated custom attributes).

    Android
    To ensure that some code runs before anything else in your Android app, you can place it in the first Activity to load or you can create an Application subclass that is loaded before any activities. The same below shows an Application subclass that copies an existing database file data.sqlite out of the /Resources/Raw/ directory.

    [Application]
    public class YourAndroidApp : Application {
    public override void OnCreate ()
    {
    base.OnCreate ();
    var docFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
    Console.WriteLine ("Data path:" + Database.DatabaseFilePath);
    var dbFile = Path.Combine(docFolder, "data.sqlite"); // FILE NAME TO USE WHEN COPIED
    if (!System.IO.File.Exists(dbFile)) {
    var s = Resources.OpenRawResource(Resource.Raw.data); // DATA FILE RESOURCE ID
    FileStream writeStream = new FileStream(dbFile, FileMode.OpenOrCreate, FileAccess.Write);
    ReadWriteStream(s, writeStream);
    }
    }
    // readStream is the stream you need to read
    // writeStream is the stream you want to write to
    private void ReadWriteStream(Stream readStream, Stream writeStream)
    {
    int Length = 256;
    Byte[] buffer = new Byte[Length];
    int bytesRead = readStream.Read(buffer, 0, Length);
    // write the required bytes
    while (bytesRead > 0)
    {
    writeStream.Write(buffer, 0, bytesRead);
    bytesRead = readStream.Read(buffer, 0, Length);
    }
    readStream.Close();
    writeStream.Close();
    }
    }

  • imanmohadesi.6653imanmohadesi.6653 USMember ✭✭

    please help me

  • imanmohadesi.6653imanmohadesi.6653 USMember ✭✭

    please help me

  • YayinoYayino ZAMember ✭✭

    but you have the solution right there, did you try it ?

    put your sqlite db in asset

    then use
    string Path = System.IO.Path.Combine(Android.OS.Environment.ExternalStorageDirectory.ToString(), your_db_name);

    then use your steam read and write like the way you got them

  • jaydeepkarenajaydeepkarena Member ✭✭
    edited July 2018

    @imanmohadesi.6653 said:
    please help me

    Do you find any solution ? I am having the same problem.

Sign In or Register to comment.