微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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,Reward) values ('山治',2000000000)";
            command = new sqliteCommand(sql,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全面学习(一、二、三)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐