SqLite本地数据库常规用法

打印 上一主题 下一主题

主题 523|帖子 523|积分 1569

SqLite数据库介绍

SqLite是一个进程内的库,实现了自给自足的、无服务器的、零配置的、事务性的 SQL 数据库引擎。它是一个零配置的数据库,这意味着与其他数据库一样,您不需要在系统中配置。
就像其他数据库,SqLite 引擎不是一个独立的进程,可以按应用程序需求进行静态或动态连接。SqLite 直接访问其存储文件。
为什么要用SqLite


  • 不需要一个单独的服务器进程或操作的系统(无服务器的)。
  • SqLite 不需要配置,这意味着不需要安装或管理。
  • 一个完整的 SqLite 数据库是存储在一个单一的跨平台的磁盘文件。
  • SqLite 是非常小的,是轻量级的,完全配置时小于 400KiB,省略可选功能配置时小于250KiB。
  • SqLite 是自给自足的,这意味着不需要任何外部的依赖。
  • SqLite 事务是完全兼容 ACID 的,允许从多个进程或线程安全访问。
  • SqLite 支持 SQL92(SQL2)标准的大多数查询语言的功能。
  • SqLite 使用 ANSI-C 编写的,并提供了简单和易于使用的 API。
  • SqLite 可在 UNIX(Linux, Mac OS-X, Android, iOS)和 Windows(Win32, WinCE, WinRT)中运行。
创建SqLite数据库

使用命令行创建数据库
  1. cd. >test3.db
复制代码
使用代码创建数据库
  1. //方式一
  2. System.IO.File.Create(@"c:\test.db");
  3. //方式二
  4. //需要安装Neget包。.Net FX安装 System.Data.SQLite;.Net Core安装 System.Data.SQLite.Core
  5. System.Data.SQLite.SQLiteConnection.CreateFile(@"c:\test2.db");
复制代码
使用官网工具创建数据库

下载SqLite工具包
博客下载地址
官网下载地址

创建数据库
工具包下载完成后j解压,可以看到SqLite3.exe文件,直接在命令行当前目录中以下命令创建数据库。
  1. sqlite3 "test.db" ".databases" ".quit"
复制代码

连接数据库

test.db文件创建完成以后,默认会创建一个main名称的数据库,然后就可以直接用数据库IDE打开连接数据库即可。
数据库IDE:
代码操作数据库

链接字符串


  • 基本
    Data Source=filepath;database=main;
  • 版本号
    可以通过IDE中执行sql查询版本号 select sqlite_version(*)
    Data Source=filepath;database=main;Version=3;
  • 只读链接
    Data Source=filepath;database=main;Read Only=True;
实体类操作Sqlite数据库

需要Neget安装sqlite-net-pcl包。
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using SQLite;
  5. namespace ConsoleApp1
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string path = @"F:\DownLoad\sqlite\text4.db";
  12.             NoteDatabase noteDatabase = new NoteDatabase(path);
  13.             Note note = new Note();
  14.             note.Text = "123456";
  15.             note.Date = DateTime.Now;
  16.             noteDatabase.SaveNoteAsync(note).Wait();
  17.         }
  18.     }
  19.     public class Note
  20.     {
  21.         //主键,自增
  22.         [PrimaryKey, AutoIncrement]
  23.         public int ID { get; set; }
  24.         //唯一标识,不为空
  25.         [Unique, NotNull]
  26.         public string Text { get; set; }
  27.         [MaxLength(30), NotNull]
  28.         public DateTime Date { get; set; }
  29.     }
  30.     public class NoteDatabase
  31.     {
  32.         readonly SQLiteAsyncConnection database;
  33.         readonly SQLite.SQLiteConnection database2;
  34.         public NoteDatabase(string connectionString)
  35.         {
  36.             database = new SQLiteAsyncConnection(connectionString);
  37.             database.CreateTableAsync<Note>().Wait();
  38.         }
  39.         public Task<List<Note>> GetNotesAsync()
  40.         {
  41.             //Get all notes.
  42.             return database.Table<Note>().ToListAsync();
  43.         }
  44.         public Task<Note> GetNoteAsync(int id)
  45.         {
  46.             // Get a specific note.
  47.             return database.Table<Note>()
  48.                             .Where(i => i.ID == id)
  49.                             .FirstOrDefaultAsync();
  50.         }
  51.         public Task<int> SaveNoteAsync(Note note)
  52.         {
  53.             if (note.ID != 0)
  54.             {
  55.                 // Update an existing note.
  56.                 return database.UpdateAsync(note);
  57.             }
  58.             else
  59.             {
  60.                 // Save a new note.
  61.                 return database.InsertAsync(note);
  62.             }
  63.         }
  64.         public Task<int> DeleteNoteAsync(Note note)
  65.         {
  66.             // Delete a note.
  67.             return database.DeleteAsync(note);
  68.         }
  69.     }
  70. }
复制代码
ADO.NET

需要安装Neget包。.Net FX安装 System.Data.SQLite;.Net Core安装System.Data.SQLite.Core。
  1. using System;
  2. using System.Data;
  3. using System.Data.Common;
  4. using System.Data.SQLite;
  5. namespace ConsoleApp1
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             SqlHelper helper = new SqlHelper();
  12.             string strSql = "Select * From Test";
  13.             DataTable dt = helper.GetDataTable(strSql);
  14.             string updateSql = "update Test set id=4 where id=3";
  15.             int count = helper.ExecuteNonQuery(updateSql);
  16.             Console.WriteLine(count);
  17.         }
  18.     }
  19.     public class SqlHelper
  20.     {
  21.         //创建数据库连接字符串
  22.         private static string ConnString = @"Data Source=F:\DownLoad\sqlite\test.db;database=main;";
  23.         //创建数据库连接对象
  24.         private static SQLiteConnection Conn = null;
  25.         //增删改
  26.         public int ExecuteNonQuery(string sqlStr)
  27.         {
  28.             Conn = new SQLiteConnection(ConnString);
  29.             Conn.Open();
  30.             IDbCommand cmd = new SQLiteCommand(sqlStr, Conn);
  31.             int result = cmd.ExecuteNonQuery();
  32.             Conn.Close();
  33.             return result;
  34.         }
  35.         /// <summary>
  36.         /// 获得datatable
  37.         /// </summary>
  38.         /// <param name="sql"></param>
  39.         /// <returns></returns>
  40.         public DataTable GetDataTable(string sqlStr)
  41.         {
  42.             Conn = new SQLiteConnection(ConnString);
  43.             DataTable dt = new DataTable();
  44.             DbDataAdapter dap = new SQLiteDataAdapter(sqlStr, Conn);
  45.             dap.Fill(dt);
  46.             Conn.Close();
  47.             return dt;
  48.         }
  49.     }
  50. }
复制代码
Dapper

需要Neget安装System.Data.SQLite和Dapper包。
  1. using Dapper;
  2. using System.Data.SQLite;
  3. namespace ConsoleApp1
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             using (var conn = new SQLiteConnection(@"Data Source=F:\DownLoad\sqlite\text4.db;database=main;"))
  10.             {
  11.                 var data = conn.Query<object>("Select * From Test");
  12.                 int a = conn.Execute("update Test set id=4 where id=3");
  13.             }
  14.         }
  15.     }
  16. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

九天猎人

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表