简体   繁体   中英

Inserting/Retrieve data into Sqlite with Sqlite.net Extensions

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<ISQLite>().GetConnection();
conn.CreateTable<PatientDB>();
conn.CreateTable<VitalsDB> ();


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

Can you try this?

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

var patientToInsert = new PatientDB();
patientToInsert.ID = pat.ID;
patientToInsert.Name = pat.Name;

patiendToInsert.PatientVitals = new List();
foreach (var vital in pat.Vitals)
{
    var vitalToInsert = new VitalsDB();
    vitalToInsert.PatientVitalID = vital.PatientVitalsID;
    vitalToInsert.Weight = vital.Weight;
    patiendToInsert.PatientVitals.Add(vitalToInsert);
}
conn.Insert(patientToInsert);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM