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();
}
}