0

I'm using xamarin and I'm currently using custom array adapter but my prof. told me our system should include database, so I kept searching till I found json but someone told me I can retrieve my data faster if I use SQlite. So I did.. I have already created my own pre-populated SQLite database file using SQLite Manager(firefox plugin). I've been following this link to use my own SQLite database file.. I do not know what to do next.. been searching for almost 2 hours but couldn't find any useful examples. sorry for being a noob programmer. could anyone give me useful links?

what I have so far:

public class DBOpenHelper:SQLiteOpenHelper
{
    private static string DB_PATH = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
    private static string DB_NAME = "akyatpinas.db";
    private static int VERSION = 1;
    private Context context;

    public DBOpenHelper(Context context) : base(context, DB_NAME, null, VERSION)
    {
        this.context = context;
    }

    private string GetSQLiteDBPath()
    {
        return Path.Combine(DB_PATH, DB_NAME);
    }

    public override SQLiteDatabase WritableDatabase
    {
        get { return CreateSQLiteDB(); }
    }

    private SQLiteDatabase CreateSQLiteDB()
    {
        SQLiteDatabase sqliteDB =null;
        string path = GetSQLiteDBPath();
        Stream streamSQLite = null;
        FileStream streamWriter = null;
        Boolean isSQLiteInit = false;

        try
        {
            if (File.Exists(path))
                isSQLiteInit = true;
            else
            {
                streamSQLite = context.Resources.OpenRawResource(Resource.Raw.akyatpinas);
                streamWriter = new FileStream(path,FileMode.OpenOrCreate,FileAccess.Write);
                if (streamSQLite != null && streamWriter != null)
                {
                    if (CopySQLiteDB(streamSQLite, streamWriter))
                        isSQLiteInit = true;
                }
            }
            if (isSQLiteInit)
                sqliteDB = SQLiteDatabase.OpenDatabase(path, null, DatabaseOpenFlags.OpenReadonly);
        }
        catch
        {
            // ignored
        }
        return sqliteDB;
    }

    private bool CopySQLiteDB(Stream readStream, FileStream writeStream)
    {
        bool isSuccess = false;
        int length = 256;
        Byte[] buffer = new Byte[length];
        try
        {
            // write the required bytes
            int bytesRead = readStream.Read(buffer, 0, length);
            while (bytesRead > 0)
            {
                writeStream.Write(buffer, 0, bytesRead);
                bytesRead = readStream.Read(buffer, 0, length);
            }
            isSuccess = true;
        }
        catch
        {
            //ignore 
        }
        finally
        {
            readStream.Close();
            writeStream.Close();
        }
        return isSuccess;
    }


    public override void OnCreate(SQLiteDatabase db)
    {

    }

    public override void OnUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        throw new NotImplementedException();
    }
}
chlara
  • 113
  • 2
  • 9

1 Answers1

0

So from what I've read you have created an SQLite database. Have you moved it into your app directory? That should be your first step. If one doesn't already exist, create an assets directory in your src/main and put the database there, so the full path should be src/main/assets/yourdatabase.db

The DatabaseHelper class at this link should then tell you how you're meant to communicate with it. You will want to ensure you change the DATABASE_NAME constant to the name of the file in your assets directory, so for the above it would yourdatabase.db

Community
  • 1
  • 1
Josh Laird
  • 6,974
  • 7
  • 38
  • 69
  • yes sir.. I have created it but my problem is I do not know how to manipulate it. edited my post – chlara Sep 28 '16 at 18:01