2015-11-04 5 views
8

J'ai ID de Row puis je vais mettre à jour d'autres valeurs.Comment faire pour mettre à jour la ligne dans sqlite.net pcl dans Windows 10 C#?

Je ne sais pas comment mettre à jour mes valeurs! Mon tableau:

class MyTable 
{ 
    [PrimaryKey, AutoIncrement] 
    public int Id { get; set; } 
    public string Date { get; set; } 
    public string Volumes { get; set; } 
    public string Price { get; set; } 
} 

autres informations:

string path; 
    SQLite.Net.SQLiteConnection conn; 

    public updatepage() 
    { 
     this.InitializeComponent(); 
     path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "ccdb.sqlite"); 

     conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path); 

     conn.CreateTable<MyTable>(); 
    } 
+1

S'il vous plaît référence cet article http://blog.tpcware.com/2014/05/universal-app-with-sqlite-part-2/ bien qu'il dise à l'application universelle, mais je pense que cela fonctionne aussi avec l'application Windows 10. – Janak

Répondre

7

J'ai récemment commencé à travailler avec des applications UWP, et aussi est tombé sur ce problème. Alors, comment mettre à jour une ligne en utilisant SQLite dans UWP vous demandez? Ici, allez-y!

using (var dbConn = new SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), App.DB_PATH)) 
    { 
     var existingUser = dbConn.Query<User>("select * from User where Id = ?", user.Id).FirstOrDefault(); 
     if (existingUser != null) 
     { 
      existingUser.Name = user.Name; 
      existingUser.Email = user.Email; 
      existingUser.Username = user.Username; 
      existingUser.Surname = user.Surname; 
      existingUser.EmployeeNumber = user.EmployeeNumber; 
      existingUser.Password = user.Password; 
      dbConn.RunInTransaction(() => 
      { 
       dbConn.Update(existingUser); 
      }); 
     } 
    } 

App.DB_PATH est identique à votre variable 'path'. Je me rends compte qu'il y a beaucoup de différents domaines d'intérêt dans cette réponse, donc si vous avez d'autres questions, n'hésitez pas à demander.

2

Pour mettre à jour uniquement un ensemble spécifique de valeurs d'une ligne, puis une exécution des requêtes SQL serait plus simple:

conn.Execute("UPDATE MyTable SET Price = ? Where Id = ?", 1000000, 2); 

Cela suppose l'entrée que vous passez à l'instruction execute a été nettoyé.

+0

alors immédiatement j'ai besoin de mettre à jour à l'objet viewmodel? de la meilleure façon? –