• 【SQLite】简单的基本使用步骤


    SQLite介绍
    SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine.
    SQLite是一个开源、免费的小型RDBMS(关系型数据库),能独立运行、无服务器、零配置、支持事物,用C实现,内存占用较小,支持绝大数的SQL92标准。
    SQLite数据库官方主页:http://www.sqlite.org/index.html
    第一步:dll的引用
    C#下SQLite操作驱动dll下载:System.Data.SQLite,引用到项目,这个可以去官网下载也可以在vs的NuGet查找引用。
    在需要使用的类添加System.Data.SQLite;
    以上就是使用准备,非常简单。
    第二步:使用sqlite
    首先,创建一个空的数据库,代码
    //数据库连接
    SQLiteConnection m_dbConnection;
    //创建一个空的数据库
    void createNewDatabase()
    {
    SQLiteConnection.CreateFile("MyDatabase.sqlite");
    }
    空的数据库已经创建完成,这个时候我们需要创捷链接字符。
    //创建一个连接到指定数据库
    void connectToDatabase()
    {
    m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
    m_dbConnection.Open();
    }
    数据库已经链接成功,下面我们给它创建一个表,并且同时创建两个字段。
    //在指定数据库中创建一个table
    void createTable()
    {
    string sql = "create table highscores (name varchar(20), score int)";
    SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
    command.ExecuteNonQuery();
    }
    做到这里前提已经准备好了,你现在就已经拥有了一个sqlite数据库,并且数据库有一个叫做highscores 的表。下面我们往表里添加数据:
    //插入一些数据
    void fillTable()
    {
    string sql = "insert into highscores (name, score) values ('Me', 3000)";
    SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
    command.ExecuteNonQuery();

    sql = "insert into highscores (name, score) values ('Myself', 6000)";
    command = new SQLiteCommand(sql, m_dbConnection);
    command.ExecuteNonQuery();

    sql = "insert into highscores (name, score) values ('And I', 9001)";
    command = new SQLiteCommand(sql, m_dbConnection);
    command.ExecuteNonQuery();
    }
    数据也插入好了,我们怎么知道到底有没有成功。那好关键的地方来了,我们进行读取:
    //使用sql查询语句,并显示结果
    void printHighscores()
    {
    string sql = "select * from highscores order by score desc";
    SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
    SQLiteDataReader reader = command.ExecuteReader();
    while (reader.Read())
    MessageBox.Show("Name: " + reader["name"] + " Score: " + reader["score"]);

    }
    程序运行结果:

    没有错messagebox弹出了我们插入的结果,通过结果我们可以知道我们的sqlite创建成功了。
    此外还有一种方法也可以查看是否创建成功,就是进入debug文件找到一个与创建的数据库名字一样后缀为.sqlite的文件。

    作者:YanBigFeg —— 颜秉锋

    出处:http://www.cnblogs.com/yanbigfeg

    本文版权归作者和博客园共有,欢迎转载,转载请标明出处。如果您觉得本篇博文对您有所收获,觉得小弟还算用心,请点击右下角的 [推荐],谢谢!

  • 相关阅读:
    AJAX实例演示加载xml
    入门AJAX总结
    My97 DatePicker一款好用的日历控件
    SQL Server 安装错误 错误代码:0x800F0906的解决方案
    JS实现动态添加和删除DIV
    .Net之路(九).ashx来实现ajax用户名的验证
    善良公社项目总结之如何从前台向后台传输数据
    MySQL数据分析(16)— 数据操作之增删改查
    字典-Python基础前传(9)
    MYSQL之事务
  • 原文地址:https://www.cnblogs.com/yanbigfeg/p/9046543.html
Copyright © 2020-2023  润新知