äžè¬ã«ããã®èšäºã¯SQL Liteã®ã€ã³ã¹ããŒã«ããã³æäœã®æ瀺ã«ããé¡äŒŒããŠããããšãå€æããŸããã
èšäºã¯èª¬æããŸã
sutdiaã®æ¡åŒµæ©èœãã€ã³ã¹ããŒã«ããŸãã
ãããžã§ã¯ããžã®SQLiteã®è¿œå
sqlite-netãããã€ããŒã®è¿œå
linqãããã€ããŒïŒLinqToSqlãEFã®ã¹ã¿ã€ã«ïŒãä»ããŠããŒã¿ããŒã¹ãæäœãã
SQLã¯ãšãªïŒADO.NETã®ã¹ã¿ã€ã«ïŒãä»ããŠããŒã¿ããŒã¹ãæäœãã
ãã¢ãããžã§ã¯ãã§ã¯ãèšäºã§æ€èšãããæžç±ã®ãªã¹ããæäœããéåžžã«ç°¡åãªäŸãèšäºã«æ·»ä»ãããŠããŸãã
Tim Heuerããã°ã«ã¯ãSQLiteãµããŒãã®è¿œå æ¹æ³ã«é¢ããçŽ æŽãããèšäºããããŸãã ããã§ãSQLiteãè¿œå ããæé ãæ€èšããŠãã ããã
ã¹ã¿ãžãªã®æºåã æ¡åŒµæ©èœãã€ã³ã¹ããŒã«ããŸãã
SQLiteãæå¹ã«ããæãç°¡åãªæ¹æ³ã¯ãã«ã¹ã¿ã æ¡åŒµæ©èœãã€ã³ã¹ããŒã«ããããšã§ãã ãããè¡ãã«ã¯ã[ããŒã«]ã¡ãã¥ãŒã®[æ¡åŒµæ©èœãšæŽæ°ããã°ã©ã ]ãéããŸãã
æ¡åŒµæ©èœãã€ã³ã¹ããŒã«ããåŸãã¹ã¿ãžãªãåèµ·åããå¿ èŠããããŸããããã§ããããžã§ã¯ãã®SQLiteã©ã€ãã©ãªãžã®ãªã³ã¯ããããžã§ã¯ãã«è¿œå ã§ããããã«ãªããŸãã
ãããžã§ã¯ããžã®SQLiteã®è¿œå
æåã«ãSQLiteã©ã€ãã©ãªãžã®ãªã³ã¯ãè¿œå ããå¿ èŠããããŸãïŒãããžã§ã¯ããå³ã¯ãªãã¯-åç §ãè¿œå ïŒïŒ
Microsoft Visual C ++ Runtime Packageãªã³ã¯ããã§ãã¯ããŠãã ããã ãã®ã³ã³ããŒãã³ãã¯ãSQLiteã©ã€ãã©ãªã«ãã£ãŠäœ¿çšãããŸãã è¿œå ããªãå Žåããããžã§ã¯ãã¯éå§ãããŠåäœããŸããããªã³ã¯ãæäŸããªãå Žåããããžã§ã¯ãã¯Windows App Certification Toolkitã«åæ Œããªããããè¿œå ããå¿ èŠããããŸãã
ããã©ã«ãã§ã¯ããããžã§ã¯ãã¯ã³ã³ãã€ã«ãããŸããã
ãããžã§ã¯ããã³ã³ãã€ã«ããã«ã¯ãConfiguration ManagerïŒãã«ãã¡ãã¥ãŒã®æåŸã®é ç®ïŒã§x86ãx64ãŸãã¯Armãã©ãããã©ãŒã ãéžæããå¿ èŠããããŸã
æ®å¿µãªãããã¢ããªã±ãŒã·ã§ã³ãã¬ã€ã¢ãŠããããšãã¯ããã©ãããã©ãŒã ããšã«ãããžã§ã¯ããåå¥ã«ã³ã³ãã€ã«ããå¿ èŠããããŸãããçŽç²ãªCïŒã«SQL LiteããŒãã衚瀺ããããŸã§ãAny CPUãªãã·ã§ã³ã§ã³ã³ãã€ã«ããããšã¯ã§ããŸããã
ãã®å Žåããããžã§ã¯ãããããã£ã§ããã©ãããã©ãŒã ããã³ãã©ãããã©ãŒã ã¿ãŒã²ãããªãã·ã§ã³ã®ä»»æã®CPUãè¿ãããšãã§ããŸãã
Linqãããã€ããŒãè¿œå ãã
.NETãããžã§ã¯ãã«Tim Heuer sqlite-netã©ãããŒãè¿œå ããŠãLinqToSqlãããã€ããŒãšåãæ¹æ³ã§SQLiteãæäœããããšã«ãããSQLiteã®æäœãå€§å¹ ã«ç°¡çŽ åã§ããŸãã ãã®ã©ãããŒã¯ãSQLã§ã¯ãªãlinqã奜ããŠãŒã¶ãŒã«åœ¹ç«ã¡ãWindows Phoneããã®ã¢ããªã±ãŒã·ã§ã³ã®ç§»æ€ãç°¡çŽ åããŸãã
ããã±ãŒãžãããŒãžã£ãŒã«ç²ŸéããŠããå Žåã¯ã次ã®ã³ãã³ãã§ã©ãããŒãã€ã³ã¹ããŒã«ã§ããŸãã
Install-Package sqlite-net
ããžã¥ã¢ã«ããã±ãŒãžãããŒãžã£ãŒã§ã®äœæ¥ãåžæããå Žåãããã±ãŒãžã¯ãNuGetããã±ãŒãžã®ç®¡çã¡ãã¥ãŒïŒãããžã§ã¯ãã®ã³ã³ããã¹ãã¡ãã¥ãŒïŒãããžã§ã¯ããå³ã¯ãªãã¯ïŒã§äœ¿çšå¯èœïŒãŸãã¯ã¹ã¿ãžãªã¡ãã¥ãŒã®[ããŒã«]-> [ã©ã€ãã©ãªããã±ãŒãžãããŒãžã£ãŒ]-> [ãœãªã¥ãŒã·ã§ã³ã®NuGetããã±ãŒãžã®ç®¡ç]ããã€ã³ã¹ããŒã«ã§ããŸã...
ããªã«åã®åé¡ã解決ããŸãã ïŒãã·ã¢èªã®ãŠãŒã¶ãŒåïŒ
ãŠãŒã¶ãŒåã¯ã¢ããªã±ãŒã·ã§ã³ã®éé¢ãããã¹ãã¬ãŒãžã®ãã¹ã®äžéšã§ãããå°ããªãã°ã®ãããsqlite-netã¯ããªã«ãã¹ã䜿çšã§ããŸããã ããªã«æåã§ååãæå®ãããŠãããŠãŒã¶ãŒã«å¯ŸããŠãã¢ããªã±ãŒã·ã§ã³ãæ©èœããªãå ŽåããããŸãã
Sqlite-netãã€ã³ã¹ããŒã«ãããšã2ã€ã®ãã¡ã€ã«ããããžã§ã¯ãã«è¿œå ãããŸã
SQLite.cs
SQLiteAsync.cs
ãšã©ãŒãä¿®æ£ããã«ã¯ããããªãã¯SQLiteConnectionã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ãŒå ã®sqlite.netãã¡ã€ã«ïŒstring databasePathãbool storeDateTimeAsTicks = falseïŒã§ãè¡ãå€æŽããŸã
var r = SQLite3.Open(DatabasePath, out handle);
ã«
var r = SQLite3.Open16(DatabasePath, out handle);
ããã§ãããŒã¿ããŒã¹ãæäœããæºåããã¹ãŠæŽããŸããã
次ã®ã³ãŒãã䜿çšããŠãããŒã¿ããŒã¹ã«æ¥ç¶ã§ããŸãã
using (var db = new SQLiteConnection(dbPath)) { //db code }
ããã§ãdbPathã¯ãã¡ã€ã«ã·ã¹ãã å ã®ããŒã¿ããŒã¹ãžã®ãã«ãã¹ã§ãã
dbPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "databasename.db");
sqlite-netã䜿çšãããšãLinqãããã€ããŒã䜿çšããŠãADO.NETã®ã¹ã¿ã€ã«ã§äœæ¥ã§ããŸãã äž¡æ¹ã®ç¹ãããå°ãæ€èšããŠãã ããã
sqlite-netãããã€ããŒã®è¿œå
ããšãã°ãããŒã¿ããŒã¹ã«è£œåã®ãªã¹ããä¿åããå¿ èŠããããŸãã ãŸãã察å¿ããã¢ãã«ãæºåããå¿ èŠããããŸãã
ããšãã°ãèªåã€ã³ã¯ãªã¡ã³ããš250æåã®ããã¹ããã£ãŒã«ããæã€äž»ããŒã瀺ãåçŽãªè£œåã¢ãã«ãäœæããŸãã
public class Product { [PrimaryKey, AutoIncrement] public int ProductId { get; set; } [MaxLength(250)] public string Name { get; set; } }
ããŒã¿ããŒã¹ãæäœããããã®å¥åã®DataLayerã¯ã©ã¹ããããšããŸãã ã¯ã©ã¹ã³ã³ã¹ãã©ã¯ã¿ãŒã§ãããŒã¿ããŒã¹æ¥ç¶æååãäœæããŸãã
private readonly string dbPath; public DataLayer() { dbPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "products.db"); }
ãã®ã¡ãœããã®å®è¡æã«ããŒã¿ããŒã¹ãšProductããŒãã«ãäœæããã¯ã©ã¹ã«Initã¡ãœãããè¿œå ããŸãã
public void Init() { using (var db = new SQLiteConnection(dbPath)) { db.CreateTable<Product>(); } }
ãã®ã¡ãœãããå®è¡ããããšãproducts.dbããŒã¿ããŒã¹ãããŒã«ã«ãã©ã«ããŒã«äœæãããŸãã ããã¯ãä»ã®ãã©ãããã©ãŒã ã§äœ¿çšã§ããéåžžã®sqliteããŒã¿ããŒã¹ã«ãªããŸãã ä»»æã®ãŠãŒãã£ãªãã£ã䜿çšããŠããŒã¿ããŒã¹ã衚瀺ããããšãã§ããŸãã ããšãã°ãç¡æã®SQLite Expert Personalã䜿çšããŠãã®ããŒã¿ããŒã¹ãéããšãè¡šã®å³ãèŠãããšãã§ããŸãã
å žåçãªã·ããªãªã§ã®äœæ¥ã®äŸãããã€ãèããŠã¿ãŸãããã ããšãã°ãæ°ãã補åãè¿œå ããããã®ã³ãŒãïŒ
public Product AddProduct(string name) { var product = new Product() {Name = name}; using (var db = new SQLiteConnection(dbPath)) { db.Insert(product); } return product; }
ãã®ã¡ãœãããå®è¡ããããšãProductIdããããã£ã«ãã£ãŠããŒã¿ããŒã¹ããèå¥åãProductãªããžã§ã¯ãã«èªåçã«å²ãåœãŠãããŸãã
次ã®ã³ãŒãã»ã¯ã·ã§ã³ã¯ãéžæããèå¥åã§è£œåãåãåããŸãã
public Product GetProductById(int productId) { using (var db = new SQLiteConnection(dbPath)) { return db.Table<Product>().FirstOrDefault(i => i.ProductId == productId); } }
çŽæ¥SQLã¯ãšãªã䜿çšããŠã¬ã³ãŒããååŸããããšãã§ããŸãã
public Product GetProductById(int productId) { using (var db = new SQLiteConnection(dbPath)) { return db.Query<Product>("select * from product where ProductId=?", productId).FirstOrDefault(); } }
ãããã£ãŠãåãæ¹æ³ã§ãã¹ãŠã®ã¬ã³ãŒããååŸã§ããŸãã
public List<Product> GetAllProducts() { using (var db = new SQLiteConnection(dbPath)) { return db.Table<Product>().ToList(); } }
ã©ãããŒã®ãã1ã€ã®åªããæ©èœã¯ãèå¥åã«ãã£ãŠè£œåãåé€ããæ©èœã§ãïŒLinqToSQLã«ã¯ãããŸããïŒã
public void DeleteProduct(int id) { using (var db = new SQLiteConnection(dbPath)) { db.Delete<Product>(id); } }
æžç±ã®ãªã¹ããæäœããéåžžã«ç°¡åãªäŸã瀺ãå®å šãªäŸã¯ã ããããããŠã³ããŒãã§ããŸã ã
SQLã¯ãšãªïŒADO.NETã®ã¹ã¿ã€ã«ïŒãä»ããŠããŒã¿ããŒã¹ãæäœãã
Linqãããã€ããŒã®å©äŸ¿æ§ã«ãããããããå Žåã«ãã£ãŠã¯ãããšãã°æ¢åã®ã³ãŒãã移æ€ãããšããSQLã¯ãšãªã䜿çšããŠããŒã¿ããŒã¹ãçŽæ¥æäœããæ¹ã䟿å©ãªå ŽåããããŸãã sqlite-netã䜿çšãããšãããŒã¿ããŒã¹ãå®å šã«æäœããŠãããŒã¿ãè¿ããªãåçŽãªã¯ãšãªããŸãã¯è€éãªããŒã¿åãè¿ãããŒã¿ãå®è¡ã§ããŸãã
次ã®äŸã§ã¯ãåçŽãªã¯ãšãªããã©ã¡ãŒã¿ã䜿çšããã¯ãšãªãããã³åçŽãªããŒã¿åãšããŒãã«ãè¿ãã¯ãšãªãå®è¡ããæ¹æ³ãèŠãŠãããŸãã
äŸãšããŠã6.3ã§æ€èšãããã®ãšåæ§ã®ãã®ãæ€èšããŸãã ã³ã³ã¹ãã©ã¯ã¿ãŒã«æ¥ç¶æååã䜿çšããŠãããŒã¿ãæäœããããã®DataLayerã¯ã©ã¹ïŒ
private readonly string dbPath; public DataLayer() { dbPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "products.db"); }
åæ§ã®ããŒãã«äœææ¹æ³ãæ€èšããŠãã ããã
public void Init() { using (var db = new SQLiteConnection(dbPath)) { db.CreateCommand( @"CREATE TABLE ""Product""( ""ProductId"" integer primary key autoincrement not null , ""Name"" varchar(250) );" ).ExecuteNonQuery(); } }
å Žåã«ãã£ãŠã¯ãå ¥ååŒæ°ä»ãã®èŠæ±ãå¿ èŠã§ãã ãã®å Žåãåãã©ã¡ãŒã¿ãŒã®sqlã¯ãšãªã§ãïŒãèšå·ã䜿çšããŠãç¡å¶éã®æ°ã®ãã©ã¡ãŒã¿ãŒãè¿œå ã§ããŸãã ããšãã°ã補åãååã§è¿œå ããæ¹æ³ã®å®è£ ãèããŠã¿ãŸãããã
public void AddProduct(string name) { using(var db=new SQLiteConnection(dbPath)) { db.CreateCommand("INSERT INTO Product (Name) VALUES (?)", name).ExecuteNonQuery(); } }
å Žåã«ãã£ãŠã¯ãã¯ãšãªã®çµæãšããŠåçŽãªããŒã¿åãè¿ãå¿ èŠããããŸãã ãããã£ãŠãããŒã¿ããŒã¹å ã®è¡æ°ãè¿ãã¯ãšãªã¯æ¬¡ã®ããã«ãªããŸãã
public int GetCount() { using (var db = new SQLiteConnection(dbPath)) { return db.CreateCommand("SELECT COUNT(ProductId) FROM Product").ExecuteScalar<int>(); } }
ããŒãã«ãè¿ãå¿ èŠãããå Žåãã¬ã³ãŒããååŸããããã®é©åãªã¢ãã«ãå¿ èŠã§ãã ãã®è¡šã§ã¯ãã¢ãã«ã¯æ¬¡ã®ããã«ãªããŸãã
public class Product { public int ProductId { get; set; } public string Name { get; set; } }
ããã§ãSQLãªã¯ãšã¹ããä»ããŠããŒã¿ããŒã¹ãããã¹ãŠã®ã¬ã³ãŒããååŸã§ããŸãã
public List<Product> GetAllProducts() { using(var db=new SQLiteConnection(dbPath)) { return db.CreateCommand("select * from product").ExecuteQuery<Product>(); } }
ãŸãšã
ã芧ã®ãšãããSQL Liteã䜿çšããããšã¯ãSQL CEãæäœããããšã»ã©é£ãããããŸããã ãããã£ãŠãã¢ããªã±ãŒã·ã§ã³ã®ãã¹ãŠã®ããŒãžã§ã³ã§åäžã®ã¯ãã¹ãã©ãããã©ãŒã ããŒã¿ããŒã¹ã䜿çšã§ããŸãã 11æã®èšäºã§ã¯ããã®ã©ã€ãã©ãªã䜿çšããŠã¯ãã¹ãã©ãããã©ãŒã ã¢ããªã±ãŒã·ã§ã³ãäœæããæ¹æ³ã«ã€ããŠèª¬æããŸãã
PSããªã«æåã§åé¡ãç 究ããåé¡ã®å©ããã¢ããã€ã¹ã解決çã«ã€ããŠVoldemarRingerãšStasusã«æè¬ããŸãã