{"id":18,"date":"2025-04-05T07:30:05","date_gmt":"2025-04-05T07:30:05","guid":{"rendered":"https:\/\/blogs.giamkichsan.com\/?p=18"},"modified":"2025-04-13T09:48:46","modified_gmt":"2025-04-13T09:48:46","slug":"gioi-thieu-sqlite","status":"publish","type":"post","link":"https:\/\/blogs.giamkichsan.com\/index.php\/2025\/04\/05\/gioi-thieu-sqlite\/","title":{"rendered":"Gi\u1edbi thi\u1ec7u SQLite"},"content":{"rendered":"\n<p><code>SQLite<\/code>&nbsp;l\u00e0 m\u1ed9t h\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u quan h\u1ec7, kh\u00e1c v\u1edbi c\u00e1c h\u1ec7 qu\u1ea3n tr\u1ecb CSDL kh\u00e1c n\u00f3 kh\u00f4ng c\u1ea7n&nbsp;<code>client\u2013server database engine<\/code>&nbsp;(m\u1ed9t d\u1ecbch v\u1ee5, \u1ee9ng d\u1ee5ng c\u00e0i \u0111\u1eb7t \u0111\u1ea7y \u0111\u1ee7 th\u00f4ng qua n\u00f3 k\u1ebft n\u1ed1i, t\u01b0\u01a1ng t\u00e1c v\u1edbi c\u00e1c CSDL), n\u00f3 \u0111\u01b0\u1ee3c nh\u00fang v\u00e0o r\u1ea5t nhi\u1ec1u ch\u01b0\u01a1ng tr\u00ecnh kh\u00e1c nhau, t\u1eeb destop, mobile \u0111\u1ebfn website. H\u1ea7u h\u1ebft ng\u00f4n ng\u1eef l\u1eadp tr\u00ecnh c\u00f3 c\u00e1c th\u01b0 vi\u1ec7n h\u1ed7 tr\u1ee3 k\u1ebft n\u1ed1i, truy v\u1ea5n \u0111\u1ebfn SQLite nh\u01b0:&nbsp;<code>PHP<\/code>&nbsp;<code>C<\/code>,&nbsp;<code>Java<\/code>,&nbsp;<code>C#<\/code>,&nbsp;<code>JavaScript<\/code>&#8230; N\u00f3 \u0111\u01b0\u1ee3c nh\u00fang v\u00e0o c\u00e1c tr\u00ecnh duy\u1ec7t, v\u00e0o c\u00e1c h\u1ec7 th\u1ed1ng nh\u00fang, h\u1ec7 \u0111i\u1ec1u h\u00e0nh nh\u01b0&nbsp;<code>Android<\/code>,&nbsp;<code>IOS<\/code>&nbsp;&#8230; \u0111\u1ec1u \u0111\u00e3 nh\u00fang m\u1eb7c \u0111\u1ecbnh.<\/p>\n\n\n\n<p>V\u00ec&nbsp;<code>SQLite<\/code>&nbsp;kh\u00f4ng c\u1ea7n&nbsp;<code>client\u2013server database engine<\/code>&nbsp;n\u00ean c\u00f3 th\u1ec3 hi\u1ec3u file CSDL \u0111\u00e3 t\u00edch h\u1ee3p m\u1ecdi th\u1ee9, s\u1eed d\u1ee5ng ngay kh\u00f4ng c\u1ea7n ph\u1ea3i c\u00e0i \u0111\u1eb7t th\u00eam th\u1ee9 g\u00ec.<\/p>\n\n\n\n<p><code>SQLite<\/code>&nbsp;t\u01b0\u01a1ng th\u00edch v\u1edbi chu\u1ea9n&nbsp;<code>SQL<\/code><\/p>\n\n\n\n<p>Vi\u1ec7c k\u1ebft n\u1ed1i v\u00e0 thi c\u00e1c l\u1ec7nh SQL v\u1edbi SQLite th\u1eadt s\u1ef1 r\u1ea5t \u0111\u01a1n gi\u1ea3n, v\u00ed d\u1ee5:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">SQLite v\u1edbi PHP<\/h3>\n\n\n\n<p>V\u00ed d\u1ee5 b\u1ea1n c\u1ea7n thi h\u00e0nh m\u1ed9t l\u1ec7nh&nbsp;<code>SQL<\/code>&nbsp;tr\u00ean CSDL SQLite v\u1edbi file c\u00f3 t\u00ean&nbsp;<code>db.sqlite<\/code>. Gi\u1ea3 s\u1eed c\u00e2u l\u1ec7nh \u0111\u00f3 l\u00e0:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>SELECT<\/strong> * <strong>FROM<\/strong> Danhmuc<\/pre>\n\n\n\n<p><a href=\"https:\/\/xuanthulab.net\/chay-sql-online-cong-cu-hoc-cau-lenh-sql.html?sql=vidu01\" target=\"_blank\" rel=\"noreferrer noopener\">Ch\u1ea1y th\u1eed<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">SQLite v\u1edbi .NET<\/h3>\n\n\n\n<p>\u0110\u1ed1i v\u1edbi .NET th\u01b0 vi\u00ean truy c\u1eadp SQLite \u0111\u01b0\u1ee3c cung c\u1ea5p t\u1ea1i&nbsp;<a href=\"http:\/\/sqlite.org\/download.html\" target=\"_blank\" rel=\"noreferrer noopener\">http:\/\/sqlite.org\/download.html<\/a>, v\u00e0o \u0111\u00f3 t\u00ecm \u0111\u00fang phi\u00ean b\u1ea3n binary cho .NET c\u1ea7n d\u00f9ng, t\u1ea3i v\u1ec1 v\u00e0 c\u00e0i \u0111\u1eb7t. Sau \u0111\u00f3 th\u00eam&nbsp;<strong>System.Data.SQLite<\/strong>&nbsp;\u1edf th\u01b0 m\u1ee5c c\u00e0i \u0111\u1eb7t&nbsp;<strong>C:\\Program Files\\SQLite.NET\\bin<\/strong>&nbsp;v\u00e0o d\u1ef1 \u00e1n c\u1ee7a b\u1ea1n \u0111\u1ec3 s\u1eed d\u1ee5ng.<\/p>\n\n\n\n<p>C\u00e1ch th\u1ee9 2 c\u00e0i&nbsp;<strong>System.Data.SQLite<\/strong>&nbsp;v\u00e0o d\u1ef1 \u00e1n b\u1eb1ng&nbsp;<strong>Package Manager Console<\/strong>&nbsp;trong&nbsp;<code>Visual Studio<\/code><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>M\u1edf c\u1eeda s\u1ed5 l\u1ec7nh&nbsp;<strong>Package Manager Console<\/strong>, r\u1ed3i g\u00f5 l\u1ec7nh sau \u0111\u1ec3 c\u00e0i:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>PM> <\/strong>Install-Package Microsoft.EntityFrameworkCore.Sqlite<br>Install-Package Microsoft.EntityFrameworkCore.Tools<br>Install-Package Microsoft.EntityFrameworkCore.Design<br><br>Add-Migration InitialCreate<br>Update-Database<\/pre>\n\n\n\n<p>N\u1ebfu c\u00e0i \u0111\u1eb7t th\u00e0nh c\u00f4ng, khai b\u00e1o s\u1eed d\u1ee5ng th\u01b0 vi\u1ec7n trong file code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">using System.Data.SQLite;\n<\/pre>\n\n\n\n<p>D\u01b0\u1edbi \u0111\u00e2y l\u00e0 \u0111o\u1ea1n m\u00e3, k\u1ebft n\u1ed1i v\u00e0 truy v\u1ea5n \u0111\u1ebfn SQLite b\u1eb1ng C#<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using Microsoft.EntityFrameworkCore;\nusing System;\nusing System.Collections.Generic;\n\npublic class BloggingContext : DbContext\n{\n    public DbSet&lt;Blog&gt; Blogs { get; set; }\n    public DbSet&lt;Post&gt; Posts { get; set; }\n\n    public string DbPath { get; }\n\n    public BloggingContext()\n    {\n        var folder = Environment.SpecialFolder.LocalApplicationData;\n        var path = Environment.GetFolderPath(folder);\n        DbPath = System.IO.Path.Join(path, \"blogging.db\");\n    }\n\n    \/\/ The following configures EF to create a Sqlite database file in the\n    \/\/ special \"local\" folder for your platform.\n    protected override void OnConfiguring(DbContextOptionsBuilder options)\n        =&gt; options.UseSqlite($\"Data Source={DbPath}\");\n}\n\npublic class Blog\n{\n    public int BlogId { get; set; }\n    public string Url { get; set; }\n\n    public List&lt;Post&gt; Posts { get; } = new();\n}\n\npublic class Post\n{\n    public int PostId { get; set; }\n    public string Title { get; set; }\n    public string Content { get; set; }\n\n    public int BlogId { get; set; }\n    public Blog Blog { get; set; }\n}\n\n\n\nusing System;\nusing System.Linq;\nusing Microsoft.EntityFrameworkCore;\n\nusing var db = new BloggingContext();\n\n\/\/ Note: This sample requires the database to be created before running.\nConsole.WriteLine($\"Database path: {db.DbPath}.\");\n\n\/\/ Create\nConsole.WriteLine(\"Inserting a new blog\");\ndb.Add(new Blog { Url = \"http:\/\/blogs.msdn.com\/adonet\" });\nawait db.SaveChangesAsync();\n\n\/\/ Read\nConsole.WriteLine(\"Querying for a blog\");\nvar blog = await db.Blogs\n    .OrderBy(b =&gt; b.BlogId)\n    .FirstAsync();\n\n\/\/ Update\nConsole.WriteLine(\"Updating the blog and adding a post\");\nblog.Url = \"https:\/\/devblogs.microsoft.com\/dotnet\";\nblog.Posts.Add(\n    new Post { Title = \"Hello World\", Content = \"I wrote an app using EF Core!\" });\nawait db.SaveChangesAsync();\n\n\/\/ Delete\nConsole.WriteLine(\"Delete the blog\");\ndb.Remove(blog);\nawait db.SaveChangesAsync();\n\nSQLiteStudio - Giao di\u1ec7n tr\u1ef1c quan l\u00e0m vi\u1ec7c v\u1edbi SQLite\n\u0110\u1ec3 th\u1ef1c h\u00e0nh, h\u1ecdc v\u1ec1 SQL n\u00f3i chung b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng CSDL m\u1eabu b\u1eb1ng SQLite \u0111\u1ec3 th\u1ef1c h\u00e0nh, v\u1edbi m\u1ee5c \u0111\u00edch t\u01b0\u01a1ng t\u00e1c v\u1edbi CSDL m\u00e0 ch\u01b0a c\u1ea7n \u00e1p d\u1ee5ng cho lo\u1ea1i ng\u00f4n ng\u1eef l\u1eadp tr\u00ecnh n\u00e0o. B\u1ea1n c\u00f3 th\u1ec3 c\u00e0i \u0111\u1eb7t c\u00f4ng c\u1ee5 SQLiteStudio, b\u1ea1n c\u00f3 th\u1ec3 t\u1ea3i v\u1ec1 cho macOS, Windows hay Linux.\n\nSau \u0111\u00f3 t\u1ea3i lu\u00f4n d\u1eef li\u1ec7u m\u1eabu t\u00f4i chu\u1ea9n b\u1ecb s\u1eb5n, l\u01b0u file d\u1eef li\u1ec7u m\u1eabu t\u00ean db.sqlite CSDL SQLite m\u1eabu v\u00e0o folder n\u00e0o b\u1ea1n th\u00edch.\nCh\u1ea1y file: SQLiteStudio, giao di\u1ec7n ch\u01b0\u01a1ng tr\u00ecnh c\u00f3 d\u1ea1ng\n\nSQLite Studio\nGiao di\u1ec7n SQLiteStudio\n&lt;img src=\"https:\/\/raw.githubusercontent.com\/xuanthulabnet\/sql-learning\/master\/docs\/sql-001.png\" alt=\"SQLite Studio\"&gt;\nB\u1ea1n b\u1eaft \u0111\u1ea7u c\u00f3 th\u1ec3 t\u1ea1o m\u1edbi CSDL SQLite, thi h\u00e0nh c\u00e1c l\u1ec7nh SQL tr\u00ean giao di\u1ec7n n\u00e0y, \u1edf \u0111\u00e2y b\u1ea1n \u0111\u00e3 c\u00f3 file d\u1eef li\u1ec7u m\u1eabu db.sqlite n\u00ean ch\u1ec9 vi\u1ec7c th\u00eam n\u00f3 v\u00e0o v\u00e0 th\u1ef1c h\u00e0nh\n\nNh\u1ea5n CTRL-0 th\u00eam file db.sqlite m\u1eabu v\u1eeba t\u1ea3i v\u1ec1 \u0111\u1ec3 l\u00e0m vi\u1ec7c\nNh\u1ea5n ALT+E m\u1edf c\u1eeda s\u1ed5 so\u1ea1n th\u1ea3o l\u1ec7nh SQL v\u00ed d\u1ee5 nh\u1eadp l\u1ec7nh select * from Danhmuc\nNh\u1ea5n F9 \u0111\u1ec3 ch\u1ea1y l\u1ec7nh SQL nh\u1eadp v\u00e0o, k\u1ebft qu\u1ea3 tr\u1ea3 v\u1ec1 \u0111\u01b0\u1ee3c th\u00f4ng b\u00e1o hi\u1ec7n th\u1ecb tr\u1ef1c quan trong c\u1eeda s\u1ed5 ch\u01b0\u01a1ng tr\u00ecnh.\n\u0110\u00e2y l\u00e0 c\u00e1ch m\u00e0 c\u00e1c b\u00e0i h\u01b0\u1edbng d\u1eabn v\u1ec1 SQL g\u1ee3i \u00fd d\u00f9ng \u0111\u1ec3 th\u1ef1c h\u00e0nh tr\u00ean website n\u00e0y. Ngo\u00e0i ra b\u1ea1n c\u00f3 th\u1ec3 d\u00f9ng th\u1eb3ng c\u00e1c ch\u01b0\u01a1ng tr\u00ecnh kh\u00e1c nh\u01b0: MySQL console\n\nB\u1ea1n c\u00f3 th\u1ec3 c\u00e0i \u0111\u1eb7t XAMPP ho\u1eb7c wampserver \u0111\u1ec3 c\u00f3 lu\u00f4n c\u01a1 s\u1edf d\u1eef li\u1ec7u MariaDB, MySQL v\u00e0 c\u00f4ng c\u1ee5 PHPAdmin \u0111\u1ec3 t\u01b0\u01a1ng t\u00e1c tr\u1ef1c quan v\u1edbi CSD<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>SQLite&nbsp;l\u00e0 m\u1ed9t h\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u quan h\u1ec7, kh\u00e1c v\u1edbi c\u00e1c h\u1ec7 qu\u1ea3n tr\u1ecb CSDL kh\u00e1c n\u00f3 kh\u00f4ng c\u1ea7n&nbsp;client\u2013server database engine&nbsp;(m\u1ed9t d\u1ecbch <a class=\"mh-excerpt-more\" href=\"https:\/\/blogs.giamkichsan.com\/index.php\/2025\/04\/05\/gioi-thieu-sqlite\/\" title=\"Gi\u1edbi thi\u1ec7u SQLite\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-18","post","type-post","status-publish","format-standard","hentry","category-sqlite"],"_links":{"self":[{"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/comments?post=18"}],"version-history":[{"count":5,"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/posts\/18\/revisions"}],"predecessor-version":[{"id":32,"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/posts\/18\/revisions\/32"}],"wp:attachment":[{"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/media?parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/categories?post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/tags?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}