I have a android app who use SQLite and sqlite.net pcl library (with xamarin and c#)
This is my table definition (just the id part)
[Table("ConsumoMensual")]
public class ConsumoMensual
{
[PrimaryKey, AutoIncrement]
public int? id {get;set;} //also try with int and int?
...
I insert data with this code, and returns 1
public void RegistroLlamada(MOD.phoneScrapView v){
ConsumoMensual c = findRegistroMes (v.FechaLlamada);
bool update = true;
if (c == null) {
update = false;//this is because always get 0
c = new ConsumoMensual ();
}
c.annio = v.FechaLlamada.Year;
c.mes = v.FechaLlamada.Month;
c.minutosMismaCompania += minutosMismaCompania (v);
c.minutosNumerosGratuitos+= ValidoGratuitos (v);
c.minutosOtraCompania+= minutosOtraCompania (v);
lock (locker) {
if (update)
database.Update (c);//crash here (ID is null)
else
database.Insert (c);//returns 1 here
}
}
but when i read it, i got zero in the id
public ConsumoMensual findRegistroMes(DateTime fecha)
{
lock (locker) {
return (from i in database.Table<ConsumoMensual> ()
where i.mes == fecha.Month && i.annio == fecha.Year
select i).FirstOrDefault ();
}
}
this is the interface code
namespace PlanControlDAL
{
public interface ISQLite
{
SQLiteConnection GetConnection();
}
}
and this is the class who implements the interface public class SQLite_Android : ISQLite { public SQLite_Android () { }
#region ISQLite implementation
public SQLite.Net.SQLiteConnection GetConnection ()
{
var sqliteFilename = "planControl.db3";
string documentsPath = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal); // Documents folder
var path = Path.Combine(documentsPath, sqliteFilename);
// This is where we copy in the prepopulated database
Console.WriteLine (path);
if (!File.Exists(path))
{
/*var s = Forms.Context.Resources.OpenRawResource(Resource.Raw.totalSQLite); // RESOURCE NAME ###*/
// create a write stream
/*using (StreamReader sr = new StreamReader (Resource.Raw.totalSQLite)) {
FileStream writeStream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
// write to the stream
ReadWriteStream (sr, writeStream);
}*/
}
var plat = new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid();
var conn = new SQLite.Net.SQLiteConnection(plat, path);
// Return the database connection
return conn;
}
#endregion
/// <summary>
/// helper method to get the database out of /raw/ and into the user filesystem
/// </summary>
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();
}
}
I'm follow the demos to the letter and i keep getting zero (or null depends if int or int?) what's doing wrong?
Ps. I use the 2.5.1 nuget version of sqlite.net pcl & 2.5.1 sqlite.net.plataform.xamarin.android
Aucun commentaire:
Enregistrer un commentaire