I am working with Visual Studio Mac 8.3.2 and Xamarin.Forms 4.2.0 to create an app for iOS and Android. As db interface library I am using sqlite-net-pcl 1.6.292. I tried older versions, but the result is the same.
On iOS everything is working fine. The query is returning the expected results correctly. On Android I am getting the correct number of result records, but the result values are always 0 or empty.
Here some code
public class ewCatAbstract
{
[PrimaryKey]
public int id { get; set; }
public string Name { get; set; }
public string NoOfObjects { get; set; }
public string ImageName { get; set; }
public IEnumerable<ewCatAbstract> GetCategories()
{
var fieldName = Globals.dbFieldName(); // returning field name depending on currect language, e.g. nameEng
var list = Globals.dbConnection.Query<ewCatAbstract>(
"SELECT ewCat.id, ewCat." + fieldName + " AS Name, COUNT(ewObject2Category.id) AS NoOfObjects, " +
"CASE WHEN ewCat.id = 2 THEN 'a.png' WHEN ewCat.id = 3 THEN 'b.png' ELSE '?' END AS ImageName " +
"FROM ewCat, ewObject2Category " +
"GROUP BY ewCat.id, ewObject2Category.category " +
"HAVING ewObject2Category.category = ewCat.id " +
"ORDER BY ewCat.sort");
// debug code followng
Console.WriteLine("ewCatAbstract GetCategories: {0} ", list.Count);
foreach (var element in list)
{
Console.WriteLine("mybase: Type is {0}", element.GetType());
Console.WriteLine("id " + element.id);
Console.WriteLine("name " + element.Name);
}
return list;
}
}
The number of returned records is correct. When running through the loop, I see the correct class type for every returned element, but when printing the "id" and "Name" values, "id" is always 0 and the "Name" string is always an empty string.
The connection to the database can be opened. I do not get any errors here. Otherwise I would not get the correct number of results. Please note: the code is working correctly on iOS. It is failing under Android. Any ideas would be highly appreciated.
More Info: The sqlite database is a pre-populated database. I copied the database file into the 'Assets' folder and open it via the following code.
public SQLite.SQLiteConnection CreateConnection()
{
var sqliteFilename = AppResources.EwAppDatabase; // fetching file name from AppResources
string documentsDirectoryPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var path = Path.Combine(documentsDirectoryPath, sqliteFilename);
// This is where we copy in our pre-created database
if (!File.Exists(path))
{
using (var binaryReader = new BinaryReader(global::Android.App.Application.Context.Assets.Open(sqliteFilename)))
{
using (var binaryWriter = new BinaryWriter(new FileStream(path, FileMode.Create)))
{
byte[] buffer = new byte[2048];
int length = 0;
while ((length = binaryReader.Read(buffer, 0, buffer.Length)) > 0)
{
binaryWriter.Write(buffer, 0, length);
}
}
}
}
var conn = new SQLite.SQLiteConnection(path);
return conn;
}
I use the database for reading only. I am not writing to it.