当前位置:网站首页>C# 使用SQLite
C# 使用SQLite
2022-07-24 21:46:00 【flysh05】
SQLite是一个进程内的轻量级嵌入式数据库,它的数据库就是一个文件,实现了自给自足、无服务器、零配置的、事务性的SQL数据库引擎。它是一个零配置的数据库,这就体现出来SQLite与其他数据库的最大的区别:SQLite不需要在系统中配置,直接可以使用。且SQLite不是一个独立的进程,可以按应用程序需求进行静态或动态连接。SQLite可直接访问其存储文件。
1. 安装:System.Data.SQLite 组件

2. 创建数据表
首先需要在数据创建一张表,才能添加数据,有数据了才能演示查询,修改和删除。
创建一张Person表。
string dbfile = @"URI=file:sql.db";
SQLiteConnection cnn = new SQLiteConnection(dbfile);
cnn.Open();
string sql = "Create table Person (Id integer primary key, Name text);";
SQLiteCommand cmd = new SQLiteCommand(sql, cnn);
cmd.ExecuteNonQuery();
cnn.Close();
3.添加行数据
string dbfile = @"URI=file:sql.db";
SQLiteConnection cnn = new SQLiteConnection(dbfile);
cnn.Open();
string sql = "insert into Person (Id , Name) values(1,'Mike');";
SQLiteCommand cmd = new SQLiteCommand(sql, cnn);
cmd.ExecuteNonQuery();
cnn.Close();
Console.WriteLine("Insert row OK");
4. 查询数据
string dbfile = @"URI=file:sql.db";
SQLiteConnection cnn = new SQLiteConnection(dbfile);
cnn.Open();
string sql = "Select * From Person";
SQLiteCommand cmd = new SQLiteCommand(sql, cnn);
SQLiteDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
Console.WriteLine($"{
reader.GetInt32(0)} {
reader.GetString(1)} ");
}
reader.Close();
cnn.Close();
5. 更新数据
string dbfile = @"URI=file:sql.db";
SQLiteConnection cnn = new SQLiteConnection(dbfile);
cnn.Open();
string sql = "update Person set Name='Jim jones' where id=1;";
SQLiteCommand cmd = new SQLiteCommand(sql, cnn);
cmd.ExecuteNonQuery();
cnn.Close();
Console.WriteLine("Update row OK");
6.删除数据
string dbfile = @"URI=file:sql.db";
SQLiteConnection cnn = new SQLiteConnection(dbfile);
cnn.Open();
string sql = "delete from Person where id=3;";
SQLiteCommand cmd = new SQLiteCommand(sql, cnn);
cmd.ExecuteNonQuery();
cnn.Close();
Console.WriteLine("Delete row OK");
实现了基本的数据操作,增删改查。在日常应用中稍加修改就即可使用。
边栏推荐
- Unfair distribution
- CAD sets hyperlinks to entities (WEB version)
- Binary search
- How to realize three schemes of finclip wechat authorized login
- Discussion on solving the application ecological problems of domestic systems based on small programs
- LED digital display driver IC and anti-interference LED digital tube display driver ic-vk1s68c ssop24 are applicable to finger clip pulse oximeter, arm electronic sphygmomanometer, thermometer, fetal
- [development tutorial 6] crazy shell arm function mobile phone - interruption experiment tutorial
- @typescript-eslint/ [email protected]
- Calling Laser Galvanometer control in the application and development tutorial of motion control card
- 2018mysql technology Q & a collection, hoping to give some help to students who like MySQL
猜你喜欢
![[Development Tutorial 4] open source Bluetooth heart rate waterproof sports Bracelet - external flash reading and writing](/img/cf/db40dcbb141c5a2e8ccaed7700fa3e.png)
[Development Tutorial 4] open source Bluetooth heart rate waterproof sports Bracelet - external flash reading and writing
![Leetcode: the shortest dice sequence impossible to get [thinking questions + grouping ideas]](/img/89/0789cd381302237a28f3f18b3bfa74.png)
Leetcode: the shortest dice sequence impossible to get [thinking questions + grouping ideas]

CAD sets hyperlinks to entities (WEB version)

Discussion on solving the application ecological problems of domestic systems based on small programs

陈春花与莫言,都有苦难言

Using skills and design scheme of redis cache (classic collection version)
![[image processing] pyefd.elliptic_ fourier_ How descriptors are used](/img/72/d2c825ddd95f541b37b98b2d7f6539.png)
[image processing] pyefd.elliptic_ fourier_ How descriptors are used

Feeding Program Source Code to ZK VMs

Everything about database, database and table is here

ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost:3306‘ (10061)
随机推荐
CAD disable a button on the toolbar (WEB version)
"Paper reproduction" bidaf code implementation process (4) model training + verification
Mysql database commands
What are the most problematic database accounts in DTS?
Image processing notes (1) image enhancement
[CCNA experiment sharing] routing between VLANs of layer 3 switches
【南瓜书ML】(task4)神经网络中的数学推导
“IRuntime”: 未声明的标识符
Everything about database, database and table is here
Makefile基础知识--扩展
Atcoder beginer contest 260 a~f problem solution
What should I pay attention to when selecting DTS database type?
Binary search
微信小程序监听实时地理位置变化事件接口申请
91. (leaflet chapter) leaflet situation plotting - offensive direction drawing
Es+redis+mysql, the high availability architecture design is awesome! (supreme Collection Edition)
Big country "grain" policy | wheat expert Liu Luxiang: China's rations are absolutely safe, and the key to increasing grain potential lies in science and technology
Make good use of these seven tips in code review, and it is easy to establish your opposition alliance
[good question with two points]
day10:声明式事务控制