c# - 以编程方式创建 SQLite 表

标签 c# sqlite xamarin.forms

我正在创建 Xamarin Forms 应用程序。该应用程序包含一个创建新列表的按钮。列表的名称和日期保存在一个表中,但列表的内容必须存储在另一个表中,该表需要使用查询 创建。我已经能够使用以下方法成功地将记录插入到包含列表名称和日期的表中:

[Table("ToDoLists")]
public class ShoppingList : INotifyPropertyChanged
{
    private int _id;

    [PrimaryKey, AutoIncrement]
    public int Id
    {
        get
        {
            return _id;
        }

        set
        {
            _id = value;
            OnPropertyChanged(nameof(Id));
        }
    }

    private string _name;

    [NotNull]
    public string Name
    {
        get
        {
            return _name;
        }

        set
        {
            _name = value;
            OnPropertyChanged(nameof(Name));
        }
    }

    private string _date;

    [NotNull]
    public string Date
    {
        get
        {
            return _date;
        }

        set
        {
            _date = value;
            OnPropertyChanged(nameof(Date));
        }
    }

    private string _description;

    [NotNull]
    public string Description
    {
        get
        {
            return _description;
        }

        set
        {
            _description = value;
            OnPropertyChanged(nameof(Description));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

我试过以下方法:

  • [Table(tableName)] 语句中将列表名称作为表名称传递,但 IDE 告诉我只允许使用常量字段,这意味着我不能动态指定表名
  • 我曾尝试阅读有关 SQLiteCommand 的内容,但我给出的示例使用了 SQLite PCL 中不可用的函数(除非我错误地安装了 SQLite PCL)。

我如何在 SQLite PCL 中使用查询 创建一个表?有可能吗?有什么建议吗?

最佳答案

看看SQLite-net :

SQLite-net is an open source, minimal library to allow .NET and Mono applications to store data in SQLite 3 databases. It was first designed to work with Xamarin.iOS, but has since grown up to work on all the platforms (Xamarin.*, .NET, UWP, Azure, etc.).

SQLite-net was designed as a quick and convenient database layer. Its design follows from these goals:

  • Very easy to integrate with existing projects and runs on all the .NET platforms.
  • Thin wrapper over SQLite that is fast and efficient. (This library should not be the performance bottleneck of your queries.)
  • Very simple methods for executing CRUD operations and queries safely (using parameters) and for retrieving the results of those query in a strongly typed fashion.
  • Works with your data model without forcing you to change your classes. (Contains a small reflection-driven ORM layer.)

首先,为您的数据库创建一个SQLiteAsyncConnection:

private SQLiteAsyncConnection database; 
database = new SQLiteAsyncConnection("YourDatabasePath");

然后您可以使用方法 CreateTableAsync() 来创建您的表:

await database.CreateTableAsync<ShoppingList>();

要向表中添加数据,您可以这样做:

public async Task SaveShoppingObjects(List<ShoppingObjects> shoppingsObjects)
{
    await database.RunInTransactionAsync(tran =>
    {
        foreach (ShoppingObject s in shoppingObjects)
        {
            tran.InsertOrReplace(SqliteEntityFactory.Create(s));
        }
    });
}

SqliteEntityFactory.Create 是一种帮助您创建表元素的方法。它可能看起来像这样:

public static ShoppingList Create(ShoppingObject s)
{
    ShoppingList slist = new ShoppingList();
    if (s == null)
    {
        return slist;
    }
    slist.Id = s.Id;
    slist.Name = s.Name;
    // etc...

    return slist;
}

如果我对你的问题的理解正确,那应该可以解决问题!

关于c# - 以编程方式创建 SQLite 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42153054/

相关文章:

c# - 无法在 C# 中将 GetValueOrDefault() 用于字典

c# - 连接状态未关闭

android - 获取 Blob 图像并将该图像转换为位图图像

python - 使用python3中的For循环在sqlite3中创建3列x 10行表

ios - 使用 TabbedPage 时更改所选 NavigationPage 上的 "Active"图标

xamarin - DatePicker 未显示泰国文化的正确年份(2021 年应为 2564)

c# - 从非默认命名空间读取 WMI 类

c# - wpf customcontrol,如何在itemscontrol DataTemplate中绑定(bind)按钮点击

c# - 从 C# 应用程序调用 C++ DLL

Python Sqlite 转义字符串选择列