如何使用SQLite-net在SQLite中插入和更新

时间:2014-01-22 05:34:49

标签: sqlite windows-runtime

我有以下内容:

1)后端服务器中的SQL Server:

tblCustomer

ID   CompanyName  Group      Product group   GST
--   -----------  -------    -------------   ----
1    The One       Exp        A               4    
2    The One       Exp        A               8     
.
.
20

2)Tablet设备内的SQLite-DB。


tblCustomer

ID   CompanyName  Group      Product group   GST
--   -----------  -------    -------------   ----
1    The One       Exp        A               4    
2    The One       Exp        A               8 
.    The One
.
20  The One

3) 我使用webservice从服务器获取数据并使用下面的代码使用Sqlite-Net api将记录插入到SQLite Db中


The Problems:

1). How to I update the tblCustomer in SQLite for changes below:

Note: ID in SQL sever and Sqlite ARE not the same.


ID   CompanyName  Group      Product group   GST
--   -----------  -------    -------------   ----
1    The One       Exp        A               6   < -- before it was 4


2) Someone add data in SQL Server and the total record is 21 now.

 How to add this record in SQLite tbl customer?


foreach ( var client in Customers)
{
 InsertNewCustomer(client.Company, client.Group, client.ProductGroup, client.GST)
}


private void InsertNewCustomer(string Company,string Grp, string PGrp, int Gst)
{


1) How to create SQL Statement for this case?

 var existingCustomer = (db2.Table<Customer>().Where(c => c.No == Company)) ???

 if (existingCustomer != null)
  {

     ??-- how to handle?


     int success = db2.Update(existingCustomer);

   }
   else
   {
      int success = db2.Insert(new Customer()
      {
          Name = Company,
          Group = Grp,
          ProductGroup = PGrp,
          GST = Gst

      });
    }

}

1 个答案:

答案 0 :(得分:0)

1)UPDATE tblCustomer SET GST = 6 WHERE ID = 1;

2)你需要像这样执行SQLite语句: INSERT INTO tblCustomer(CompanyName,Group,[Product group],GST)VALUES('Newcompany','EXP','A',2);

3)与1)相同,但是 UPDATE tblCustomer SET CompanyName ='Newcompany',Group ='EXP',[Product group] ='A',GST = 6 WHERE ID = 1;

当然,你必须用你的实际数据替换'Newcompany','EXP',ID = 1 ......