在c#中使用sqlite3
Sqlite3是一款优秀的数据库软件,在嵌入式设备和移动端都有使用,我司现在有些项目使用的数据库是access,说实话,对这些不太感冒,我还是喜欢优雅简单的东东,于是乘着这几天休息的时间学习了下在c#中使用sqlite3。先看代码
using System;
using System.Data.SQLite;
using System.IO;
using System.Data;
using System.Linq;
namespace sqlite3
{
class Program
{
static string path = @"./test.sqlite";
static SQLiteConnection connection = null;
static void CreateDB()
{
connection = new SQLiteConnection("data source = " + path);
connection.Open();
connection.Close();
}
static void CreateTable()
{
connection.Open();
// create table
SQLiteCommand command = connection.CreateCommand();
command.CommandText = "CREATE TABLE IF NOT EXISTS t1(id varchar(4), score int)";
command.ExecuteNonQuery();
// insert into some data
Random random = new Random();
for (int i = 0; i < 100; i++)
{
command.CommandText = string.Format("insert into t1(id, score) values({0}, {1})", i+1, random.Next());
command.ExecuteNonQuery();
Console.Clear();
Console.WriteLine("wrote {0} items", i + 1);
}
connection.Close();
}
static void DeleteDB()
{
if (File.Exists(path))
{
File.Delete(path);
}
}
static void QueryDB()
{
connection.Open();
// query the rows
SQLiteCommand command = connection.CreateCommand();
command.CommandText = "select * from t1 limit 10";
SQLiteDataAdapter adapter = new SQLiteDataAdapter(command);
DataTable table = new DataTable();
adapter.Fill(table);
foreach(DataRow row in table.Rows)
{
Console.WriteLine($"id: {row["id"]} score: {row["score"]}");
}
// get the rows' count
command.CommandText = "select count(*) from t1";
Int64 cnt = (Int64)command.ExecuteScalar();
Console.WriteLine($"table t1 has {cnt} rows");
connection.Close();
}
static void Main(string[] args)
{
DeleteDB();
CreateDB();
CreateTable();
QueryDB();
Console.ReadKey();
}
}
}
实现了一些简单的操作,比较以往使用的mysql,sqlite3的优点是便捷,安装也简单,性能也可以。
主要参考:
- https://blog.csdn.net/kasama1953/article/details/52655497
- https://www.cnblogs.com/leemano/p/6578050.html
在c#中使用sqlite3的更多相关文章
- python django中使用sqlite3数据库 存储二进制数据ByteArray
在python中使用sqlite3数据库存储二进制流数据ByteArray,在django使用sqlite3数据库时,有时候也要注意最好使用二进制流ByteArray插入字符串. 使用ByteArra ...
- MFC中使用sqlite3操作数据库
需要用到的文件有sqlite3.h .sqlite3.dll.sqlite3.lib.网上很多人分享下载地址这里不再赘述. 将这三个文件拷贝到自己新建MFC项目目录下,在解决方案窗口下 添加现有项,选 ...
- node-webkit中使用sqlite3(MAC平台)
前言 最近使用node-webkit开发一款博客发布软件,来替换难用的Windows Live Writer(主要是对Markdown标签的支持很差劲).为了解决博文信息临时保存的问题,想到了使用sq ...
- 在MFC中支持sqlite3
在vc环境下支持sqlite3的方法有很多,sqlite官网也有推荐sqlitewrappers列表,我选用的是CppSqlite 建立MFC工程的步骤我就不赘述了,以下操作均假设你已经创建好了一个M ...
- Linux系统中关于Sqlite3中文乱码问题及解决办法
新做的一个项目在本地(Win8)测试时没有问题,但传到服务器(Linux)时从Sqlite3数据库查询到的数据中文却是乱码(数据库中是正常的) 将php文件.html文件都设置成统一的utf8还是一样 ...
- centos7 python3.5中引入sqlite3
在centos系统中创建Django app,报错如下: django.core.exceptions.ImproperlyConfigured: Error loading either pysql ...
- node-webkit中使用sqlite3
sqlite3的官方文档提到:nodejs和node-webkit的ABI不同,所以默认的安装方式: npm install sqlite3 安装的sqlite3是无法使用的,需要重新编译. 编译方法 ...
- [置顶] Android中使用sqlite3操作SQLite
SQLite库包含一个名字叫做sqlite3的命令行,它可以让用户手工输入并执行面向SQLite数据库的SQL命令.本文档提供一个样使用sqlite3的简要说明. 一.创建数据库: 1.将sqlit ...
- python2中在sqlite3中插入中文
# -*- coding: utf-8 -*- import sqlite3 conn = sqlite3.connect('SWC_Perf_Info.db') cur = conn.cursor( ...
随机推荐
- webstorm最新破解方法
方法来自 Rover12421 大神. 1.从官网下载WebStorm2016.1安装. 2.下载 破解补丁 并解压,记住路径 3.编辑WebStorm安装目录下 bin 文件夹中的 WebStorm ...
- 使用VM克隆CentOS后,更改网络配置
在使用VM克隆之后,遇到一件非常郁闷的事,就算我使用‘setup’命令,修改了我的网络配置,依然无法查询到我配置的网卡,也无法开启网卡. 经过百度等一系列手段,总结如下套路--PS:朋友称之为“破釜沉 ...
- SQLServer学习-- Microsoft SQL Server 2008 Management Studio Express
Microsoft SQL Server 2008 Management Studio Express is a free, integrated environment for accessing, ...
- Exception (2) Java Exception Handling
The Java programming language uses exceptions to handle errors and other exceptional events.An excep ...
- 执行“hdfs dfs -ls”时报ConnectException
原因可能是指定的端口号不对,该端口号由hdfs-site.xml中的属性"dfs.namenode.rpc-address"指定,即为NameNode的RPC服务端口号. 文件上传 ...
- Creating Custom UITableViewCells with NIB files
Maksim Pecherskiy 13 November 2012 Well this sucks. Apparently these days you can only use the Inter ...
- 【微服务架构】SpringCloud之Hystrix断路器(六)
一:什么是Hystrix 在分布式环境中,许多服务依赖项中的一些将不可避免地失败.Hystrix是一个库,通过添加延迟容差和容错逻辑来帮助您控制这些分布式服务之间的交互.Hystrix通过隔离服务之间 ...
- 《JavaScript权威指南》(第6版)翻译错误集 更新中。。。
§6.2.2 P126 原文:If o inherits x,and that property is an accessor property with a setter method. 原译:如 ...
- ASP.NET MVC 使用过滤器需要注意
想往下继续执行就return~
- C#中匿名函数、委托delegate和Action、Func、Expression、还有Lambda的关系和区别
以前一直迷迷糊糊的,现在总算搞明白. Lambda表达式 Lamda表达式基本写法是()=>{ };Lambda和方法一样都可以传入参数和拥有返回值.(int x)=>{return x; ...