1

I am creating an SQLite Database in Visual Studio with Xamarin in C#.

I should note that this is for android only.

I am trying to make it so I am able to insert data into the SQLite database but I am unsure how.

I have been following this but I'm still unsure.

Here is the method I am trying to create.

    /// <summary>
    /// Insert a single ping group into the SQLite ping database.
    /// </summary>
    /// <param name="pingGroup"></param>
    public void AddUnsynchronizedPing(PingGroup pingGroup)
    {
        // TODO: Add the passed ping group parameter into the SQLite database as new/unsynchronized.
        if (pingGroup != null)
        {
            // Add ping group to the database.
            // Add pings to the database.
            // Maybe one step, maybe done separately.
            // If done separately, must set Ping.PingGroupID to ID of original ping group.
        }
    }

For context, here is the entire class.

namespace BB.Mobile
{
/// <summary>
/// A class to provide a single interface for interacting with all SQLite data operations for stored tracking points.
/// </summary>
/// 
class DataManager
{
    private SQLiteConnection db = null;

    public DataManager()
    {
        if (this.db == null)
        {
            string dbPath = Path.Combine(
             System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),
             "bb.db3");

            db = new SQLiteConnection(dbPath);
            db.CreateTable<Ping>();
            db.CreateTable<PingGroup>();
        }
    }

    /// <summary>
    /// Will compile and return all matching unsynchronized ping data from the SQLite database.
    /// </summary>
    /// <returns></returns>
    public List<PingGroup> GetUnsynchronizedPings()
    {
        List<PingGroup> unsynchronizedPings = new List<PingGroup>();

        // TODO: Retrieve all unsynchronized pings from the SQLite database and return them to the caller.
        //var pGroup = db.Get<PingGroup>();
        //var pGroupList = db.List<PingGroup>();

        var pGroups = db.Table<PingGroup>();
        foreach (var pGroup in pGroups)
        {

        }

        return unsynchronizedPings;
    }

    /// <summary>
    /// Insert a single ping group into the SQLite ping database.
    /// </summary>
    /// <param name="pingGroup"></param>
    public void AddUnsynchronizedPing(PingGroup pingGroup)
    {
        // TODO: Add the passed ping group parameter into the SQLite database as new/unsynchronized.
        if (pingGroup != null)
        {
            // Add ping group to the database.
            // Add pings to the database.
            // Maybe one step, maybe done separately.
            // If done separately, must set Ping.PingGroupID to ID of original ping group.
        }
    }

    /// <summary>
    /// Mark all open and unsynchronized pings in the database as synchronized.
    /// </summary>
    public void SetAllPingsSynchronized()
    {
        db.DeleteAll<PingGroup>();
        db.DeleteAll<Ping>();
    }        
}
}

Thank you in advance.

1 Answer 1

1

To insert the object to sqlite database, you can just use something like:

void InsertPing(Ping p)
{
      db.Insert(p);
}


void InsertGroupOfPings(IEnumerable<Ping> pings)
{
   db.InsertAll(pings);
}

and to retrieve objects (for example):

List<Ping> GetPings()
{  
// I assume here that Ping object has property named Synchronized
    return db.Query<Ping>("select * from Ping where Synchronized = 0");
}

The SQLite library creates its tables according to your class definitions, so you can think about the properties of the class as of columns inside the table.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.