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的优点是便捷,安装也简单,性能也可以。

主要参考:

  1. https://blog.csdn.net/kasama1953/article/details/52655497
  2. https://www.cnblogs.com/leemano/p/6578050.html

在c#中使用sqlite3的更多相关文章

  1. python django中使用sqlite3数据库 存储二进制数据ByteArray

    在python中使用sqlite3数据库存储二进制流数据ByteArray,在django使用sqlite3数据库时,有时候也要注意最好使用二进制流ByteArray插入字符串. 使用ByteArra ...

  2. MFC中使用sqlite3操作数据库

    需要用到的文件有sqlite3.h .sqlite3.dll.sqlite3.lib.网上很多人分享下载地址这里不再赘述. 将这三个文件拷贝到自己新建MFC项目目录下,在解决方案窗口下 添加现有项,选 ...

  3. node-webkit中使用sqlite3(MAC平台)

    前言 最近使用node-webkit开发一款博客发布软件,来替换难用的Windows Live Writer(主要是对Markdown标签的支持很差劲).为了解决博文信息临时保存的问题,想到了使用sq ...

  4. 在MFC中支持sqlite3

    在vc环境下支持sqlite3的方法有很多,sqlite官网也有推荐sqlitewrappers列表,我选用的是CppSqlite 建立MFC工程的步骤我就不赘述了,以下操作均假设你已经创建好了一个M ...

  5. Linux系统中关于Sqlite3中文乱码问题及解决办法

    新做的一个项目在本地(Win8)测试时没有问题,但传到服务器(Linux)时从Sqlite3数据库查询到的数据中文却是乱码(数据库中是正常的) 将php文件.html文件都设置成统一的utf8还是一样 ...

  6. centos7 python3.5中引入sqlite3

    在centos系统中创建Django app,报错如下: django.core.exceptions.ImproperlyConfigured: Error loading either pysql ...

  7. node-webkit中使用sqlite3

    sqlite3的官方文档提到:nodejs和node-webkit的ABI不同,所以默认的安装方式: npm install sqlite3 安装的sqlite3是无法使用的,需要重新编译. 编译方法 ...

  8. [置顶] Android中使用sqlite3操作SQLite

    SQLite库包含一个名字叫做sqlite3的命令行,它可以让用户手工输入并执行面向SQLite数据库的SQL命令.本文档提供一个样使用sqlite3的简要说明. 一.创建数据库:  1.将sqlit ...

  9. python2中在sqlite3中插入中文

    # -*- coding: utf-8 -*- import sqlite3 conn = sqlite3.connect('SWC_Perf_Info.db') cur = conn.cursor( ...

随机推荐

  1. [欣赏代码片段] (JavaScript) Responsive jQuery

    jQuery(document).ready(function($) { /* getting viewport width*/ var responsive_viewport = $(window) ...

  2. NArrange 配置与使用

    1. 在VS中设置一下就可以用了 2.It is also recommended to setup a restore command using the same parameters, repl ...

  3. javafx 继承Application打开

    前段时间需要用到javafx的Application来写一些图形界面之类的东西,但是run了之后eclipese不会去运行它,很纳闷,然后看了一下run as发现是没有main入口 其实加上一个mai ...

  4. javascript高级程序设计读书笔记----严格模式

    ECMAScript5最早引入“严格模式". 使用 "use strict"开启严格模式 function test(){ "use strict"; ...

  5. Linux系统root密码修改

    重启系统. 进入系统引导界面: 按下e键: 选择第二项,内核启动参数设置,按下e键: 在结尾处,输入数字 1或者 英文 " single",再回车: 按下b键启动,此时以单用户模式 ...

  6. [LeetCode 题解]: Merge k Sorted Lists

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题 ...

  7. 基于flask的轻量级webapi开发入门-从搭建到部署

    基于flask的轻量级webapi开发入门-从搭建到部署 注:本文的代码开发工作均是在python3.7环境下完成的. 关键词:python flask tornado webapi 在python虚 ...

  8. 小程序:如何让scroll-view包含内容完整滚动

    1.关于scroll-view scroll-view是小程序用来控制可滚动视图区域的组件. 通过设置scroll-x ="true" 或 scroll-y="true& ...

  9. jQuery find()方法 eq()方法

    find() 使用: 返回 <ul> 后代中所有的 <span> 元素: $(document).ready(function(){$("ul").find ...

  10. SFML从入门到放弃(1) 窗口和交互

    SFML从入门到放弃(1) 窗口和交互 创建一个新窗口: sf::RenderWindow window(sf::VideoMode(,),"new window"); 但是光创建 ...