简体   繁体   English

使用C#中的JSON(或其他易于阅读的文本格式)中的数据填充现有SQLite数据库

[英]Fill existing SQLite database with data from JSON (or other easily-readable text format) in C#

My database is divided into two parts: language-independent part and localizable part (containing Unicode text strings). 我的数据库分为两个部分:与语言无关的部分和可本地化的部分(包含Unicode文本字符串)。 For our translation staff it's much more easier to work not with some DB viewing tool but with some text format like JSON (via some tool of course). 对于我们的翻译人员而言,使用某些数据库查看工具而不是某些文本格式(如JSON)(当然通过某种工具)要容易得多。 So I'm looking for a best way to load JSON data into my SQLite database. 因此,我正在寻找一种将JSON数据加载到SQLite数据库中的最佳方法。 For now I use the foolowing approach (assume that I already have empty SQLite database): 现在,我使用下面的方法(假设我已经有空的SQLite数据库):

  1. I deserealise JSON data into C# classes (via Newtonsoft.JSON.dll). 我将JSON数据分解为C#类(通过Newtonsoft.JSON.dll)。
  2. For each of them I manually create INSERT command with paremeters (via System.Data.SQLite.dll) and then fill that parameters with class fields' values 对于它们中的每一个,我都使用参数表(通过System.Data.SQLite.dll) 手动创建INSERT命令,然后用类字段的值填充该参数
  3. I execute that command. 我执行该命令。

Is that correct (easiest, reliable) aproach to fill SQLite database with JSON data? 用JSON数据填充SQLite数据库是否正确(最简单,可靠)? Maybe I've missed some useful API? 也许我错过了一些有用的API?

Any other easily-readable text format (with Unicode support!) which is better to work with in term of C# and SQLite is welcomed. 欢迎使用任何其他易于阅读的文本格式(具有Unicode支持!),就C#和SQLite而言,它更适合使用。

Try Sqlite.NET, a basic Sqlite client and ORM: https://github.com/praeclarum/sqlite-net 尝试Sqlite.NET,基本的Sqlite客户端和ORM: https//github.com/praeclarum/sqlite-net

From their wiki: 从他们的Wiki:

Once you have defined your entity, you can automatically generate tables in your database by calling CreateTable: 定义实体后,您可以通过调用CreateTable在数据库中自动生成表:

 var db = new SQLiteConnection("foofoo"); db.CreateTable<Stock>(); db.CreateTable<Valuation>(); 

You can insert rows in the database using Insert. 您可以使用插入在数据库中插入行。 If the table contains an auto-incremented primary key, then the value for that key will be available to you after the insert: 如果表包含一个自动递增的主键,则在插入后,该键的值将对您可用:

 public static void AddStock(SQLiteConnection db, string symbol) { var s = db.Insert(new Stock() { Symbol = symbol }); Console.WriteLine("{0} == {1}", s.Symbol, s.Id); } 

Similar methods exist for Update and Delete. 存在类似的更新和删除方法。

You would get something like this: 您将获得如下内容:

AddStock(string jsonString){
    var stock = JsonConvert.DeserializeObject<Stock>(jsonString);
    db.Insert(stock);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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