Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 75885

Storing data with Sqlite.net Extensions

$
0
0

Hi there, i'm currently making an app that is using Sqlite to store an object that has a list of object within it. I've been having trouble trying to insert and retrieve the object for the past week and was wondering if I could get some help with this. Any helps would be greatly appreciated! Thanks!

This is the database for the object to be store in with a OneToMany relationship:

[Table("Patient")]
    public class PatientDB
    {
        [PrimaryKey]
        public string ID { get; set;}
        public string Name { get; set; }
        [OneToMany(CascadeOperations = CascadeOperation.All)]
        public List<Vitals> PatientVitals { get; set;}
    }
    [Table("PatientVitals")]
    public class VitalsDB
    {
        [PrimaryKey]
        public string PatientVitalID { get; set; }
        public string Weight { get; set;}
        [ForeignKey(typeof(PatientItem))]
        public string ID { get; set; }
    }

I have a separate object that was passed to me from a REST service in the form of json which I have successfully deserialized into the object below:
pat = new Patient
        {
            ID = "123",
            Name = "Doe",
        List<Vitals> vitals = new List<Vitals>
            {
                new Vitals
                {
                    PatientVitalID = "1",
                    Weight = "150",
                    ID = "123"
                }
                new Vitals
                {
                    PatientVitalID = "2",
                    Weight = "160",
                    ID = "123"
                }
            }
        };

Below is how I insert the patient into the database:

`conn = DependencyService.Get().GetConnection();
conn.CreateTable();
conn.CreateTable ();

var patientToInsert = new PatientDB ();
patientToInsert.ID = pat.ID;
patientToInsert.Name = pat.Name;
conn.Insert(patientToInsert);
var vitalsToInsert = new List ();
foreach (var vital in pat.Vitals)
{
var vitalToInsert = new VitalsDB ();
vitalToInsert.PatientVitalID = vital.PatientVitalsID;
vitalToInsert.Weight = vital.Weight;
vitalsToInsert.Add(vitalToInsert);
}
conn.InsertAll (vitalsToInsert);
foreach (var vital in vitalsToInsert)
{
vital.SmartCardID = patientToInsert.SmartCardID;
conn.Update (vital);
}`

Here how I retrieve the patient

public List<PatientDB> retrieveDBPatient(string ID)
{
    return conn.Query<PatientDB> ("SELECT * FROM [Patient] WHERE [ID]= '" + ID + "'");
}

Finally, the error message
SQLite.Net.SQLiteException: Constraint
at SQLite.Net.PreparedSqlLiteInsertCommand.ExecuteNonQuery (System.Object[] source) [0x0016b] in <filename unknown>:0
at SQLite.Net.SQLiteConnection.Insert (System.Object obj, System.String extra, System.Type objType) [0x000bc] in <filename unknown>:0

Viewing all articles
Browse latest Browse all 75885

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>