gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:45:27 30 4
gpt4 key购买 nike

我正在创建 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com