c# - 使用 C# 将数据插入 SQLite 数据库

标签 c# android database sqlite xamarin

我正在使用 C# 中的 XamarinVisual Studio 中创建一个 SQLite 数据库。

我应该注意,这仅适用于 android

我正在努力做到这一点,以便能够将数据插入 SQLite 数据库,但我不确定如何操作。

我一直在关注this但我仍然不确定。

这是我尝试创建的方法。

    /// <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.
        }
    }

对于上下文,这里是整个类。

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>();
    }        
}
}

提前谢谢你。

最佳答案

要将对象插入到 sqlite 数据库中,您可以使用类似的东西:

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


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

并检索对象(例如):

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

SQLite 库根据您的类定义创建其表,因此您可以将类的属性视为表中的列。

关于c# - 使用 C# 将数据插入 SQLite 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29575156/

相关文章:

c# - 连接到 .NET 中嵌入了 C# 的 Firebird 3 时出现问题

java - 将动态 NdefRecords 发送到构造函数

android - 从 Activity 通知中检索 FCM 数据负载

android - Jersey/Rest 默认字符编码

c# - nameof() 是否总是等于 typeof().Name?

c# - 将 JSON 字符串转换为 C# 字符串数组

c# - 使用 JQuery 构建 Windows 软件

mysql - 删除所有没有表的模式

mysql - 从表中获取所有记录以及单个 MySQL 查询语句中的选定记录

database - 在数据库中搜索字符串 'somewhere'