SQLite笔记

一、SQLite下载:

http://www.sqlite.org/download.html (或在NuGet下载安装)

二、SQLite操作:

  1、添加引用System.Data.SQLite,如安装目录在E:\Program Files\System.Data.SQLite\2010\bin,则找到System.Data.SQLite.dll引用到当前项目中;

using System.Data.SQLite;

  2、进行简单增删改查操作,语法跟sql server相差不大

复制代码
 public class UseSQLIte
    {
        SQLiteConnection m_dbConnection;
        public UseSQLIte()
        {
            createNewDatabase();
            connectToDatabase();
            createTable();
            fillTable();
            ShowInfo();
        }

        //创建一个空的数据库
        void createNewDatabase()
        {
            SQLiteConnection.CreateFile("SqliteDemo");
        }

        //建立连接
        bool connectToDatabase()
        {
            try
            {
                m_dbConnection = new SQLiteConnection("Data Source=SqliteDemo;Version=3;");
                m_dbConnection.Open();
                return true;
            }
            catch
            {
                return false;
            }
        }

        //创建表 
        void createTable()
        {
            string sql = "create table OnePiece(name VARCHAR(20), Reward BIGINT)";
            SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();
        }

        //插入数据
        void fillTable()
        {
            string sql = "insert into OnePiece (name, Reward) values ('路飞', 5000000000)";
            SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();

            sql = "insert into OnePiece (name, Reward) values ('索隆', 3000000000)";
            command = new SQLiteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();

            sql = "insert into OnePiece (name, Reward) values ('山治', 2000000000)";
            command = new SQLiteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();

            sql = "insert into OnePiece (name, Reward) values ('乔巴', 100)";
            command = new SQLiteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();
        }

        //查询语句,并显示结果
        void ShowInfo()
        {
            string sql = "select * from OnePiece order by Reward desc";
            SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
            using (SQLiteDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                    Console.WriteLine("姓名: " + reader["name"] + "\t赏金: " + reader["Reward"]);
            }
            Console.ReadLine();
        }

        bool check(string tableName)
        {
            string sql = "select count(*) from sqlite_master where type='table' and name ='" + tableName + "'";
            SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
            int i = Convert.ToInt32(command.ExecuteScalar());
            return i > 0;
        }
    }
复制代码

  3、效果显示:

 三、资源收录

Sqlite全面学习(一、二、三)

 

posted @   山治先生  阅读(384)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示