[C#] Using SQLike and basic concepts under the framework of .Net Framework

2023, Week 32, Article 2. Give yourself a goal, and then insist that there will always be a receipt, if you don’t believe me, try it!
Under the .NET Framework framework of C#, there are many lightweight database options, such as: SQLike is one of them, let's learn about the simple use of SQLike.

1. Lightweight database

insert image description here

1.1. Basic concepts

A lightweight database refers to a database system that has small storage requirements, low resource consumption, and is easy to deploy and use.
They usually focus on providing basic data storage and query functions, suitable for small applications, embedded systems, or projects with temporary storage needs.

1.2、SQLite

SQLite is a self-contained, serverless, zero-configuration, transactional relational database engine.
It is characterized by file-level storage and can directly use a single file as a database.
SQLite is used in a wide variety of platforms and programming languages, including mobile application development, desktop application development, and embedded systems.

1.2、Berkeley DB

Berkeley DB is a database engine for embedded systems.
It provides fast and reliable key-value pair storage and query functions, and supports ACID transaction operations.
Berkeley DB is very flexible and can be used for many purposes such as persistent storage, caching, log files, and more.

1.3、Level DB

LevelDB is a key-value storage engine developed by Google.
It is based on the log-structured merge (Log-Structured Merge, LSM) tree data structure, with fast write and query performance.
LevelDB is open source and supports bindings for several programming languages.

1.4、Redis

Redis is an open source memory data structure storage system that can be used for multiple purposes such as database, cache and message middleware.
It supports a variety of data structures (such as strings, hash tables, lists, sets, ordered sets, etc.), with high performance, high concurrency and scalability.

1.5、H2 Database

H2 is an embedded relational database engine written in Java.
It supports SQL database standard and JDBC API, and provides rich functions, including memory database, persistent database, cluster database, etc.

These lightweight databases have their own characteristics, and you can choose a suitable database according to the specific needs of the project.
They are generally easy to deploy, configure, and use, and are suitable for small applications or environments with resource constraints.
Note that while these databases can handle smaller-scale data storage needs, larger-scale and high-concurrency scenarios may require the use of more powerful database systems.
Therefore, when selecting a database, it needs to be evaluated based on factors such as the size of the project, performance requirements, and scalability.

insert image description here

2. Advantages of SQLike

SQLike is a lightweight embedded SQL database engine with the following features:

2.1, easy to use

SQLike's syntax is similar to standard SQL syntax, making it easy to learn and use. It provides common SQL operations such as SELECT, INSERT, UPDATE, and DELETE, as well as multi-table joins, aggregate functions, and more.

2.2. Compact and lightweight

The core code of SQLike is relatively small and only depends on a small number of external libraries. This makes it low consumption of system resources and suitable for use in resource-constrained environments, such as embedded devices or mobile applications.

2.3, embedded support

SQLike can be embedded directly into applications as an embedded database engine. In this way, it can be distributed with the application, without the need for a separate database server, and without the configuration and maintenance of a separate database management system (DBMS).

2.4. Platform independence

SQLike is written in C language and can run on multiple operating systems and platforms, such as Windows, Linux, macOS, etc. This makes it have better cross-platform compatibility.

3. Disadvantages of SQLike

3.1. Limited functions

Since the main goal of SQLike is to provide a simple SQL database engine, it may be limited in some advanced features and extensibility. For example, its ability to support some complex queries and optimization techniques may be relatively weak.

3.2, limited performance

Since SQLike's design focuses on lightweight and simplicity, it may not perform as well as some database engines that are specifically designed for high performance. For scenarios such as high concurrency, large data volume, or complex queries, the performance of SQLike may be weak.

3.3. Lack of advanced features

Compared with some mature database systems, SQLike may lack advanced features, such as transaction processing, stored procedures, triggers, etc. These features are required in some scenarios, but may not be available in SQLike.

Overall, SQLike is a convenient, easy-to-use and deploy lightweight embedded database engine for simple data storage and query needs. However, in complex, high-performance, and highly scalable database scenarios, other more powerful database solutions may need to be considered.

Four, SQLike operation

3.1. Create database file

By default, you can use database to name the database name, of course, you can also name it according to your business situation.
The default database file is created in the same directory where the programmer runs, and the path can be specified for output

string connectionString = "Data Source=database.db;Version=3;";
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
    
    
    connection.Open();
    // 判断数据库文件
    string databasePath = "database.db";

    if (!File.Exists(databasePath))
    {
    
    
        SQLiteConnection.CreateFile(databasePath);
    }
}

insert image description here

3.2, create a table

Here you can add a logical judgment, if the table does not exist, create the table, otherwise do not operate

// 判断表
bool isTable = true;
string tableName = "MyTableName";
string query = $"SELECT name FROM sqlite_master WHERE type='table' AND name='{
      
      tableName}'";
using (SQLiteCommand command = new SQLiteCommand(query, connection))
{
    
    
    var result = command.ExecuteScalar();
    if (result == null)
    {
    
    
        isTable = false;
    }
}
// 创建表
if (!isTable)
{
    
    
    string createTableQuery = $"CREATE TABLE {
      
      tableName} (Id INTEGER PRIMARY KEY, countValue int,name Text)";
    using (SQLiteCommand command = new SQLiteCommand(createTableQuery, connection))
    {
    
    
        command.ExecuteNonQuery();
    }
}

3.3. Add table records

根据上一步判断,都会确保存在表
// 并添加一条记录
if (!isTable)
{
    
    
    string insertDataQuery = $"INSERT INTO {
      
      tableName} (countValue,name) VALUES (99,'张三')";
    using (SQLiteCommand command = new SQLiteCommand(insertDataQuery, connection))
    {
    
    
        command.ExecuteNonQuery();
    }
}

3.4. Query table records

// 查询表
string tableName = "MyTableName";
string queryText = $"SELECT * FROM {
      
      tableName}";
using (SQLiteCommand command = new SQLiteCommand(queryText, connection))
{
    
    
    using (SQLiteDataReader reader = command.ExecuteReader())
    {
    
    
        while (reader.Read())
        {
    
    
            // 获取每行的数据
            int id = reader.GetInt32(0);

            int CountValue = reader.GetInt32(1);
            string name = reader.GetString(2);
        }
    }
}

3.5. Update table records

When adding or updating table records, it can also be operated through parameterization

string databasePath = "database.db";
string connectionString = $"Data Source={
      
      databasePath};Version=3;";

using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
    
    
    connection.Open();

    // 创建 UPDATE 语句
    string updateQuery = "UPDATE MyTableName SET CountValue = @CountValue WHERE Id = @Id";

    using (SQLiteCommand command = new SQLiteCommand(updateQuery, connection))
    {
    
    
        // 设置 UPDATE 语句中的参数
        command.Parameters.AddWithValue("@CountValue", CountValue);
        command.Parameters.AddWithValue("@Id", IdValue);

        // 执行 SQL 语句
        int rowsAffected = command.ExecuteNonQuery();

        if (rowsAffected > 0)
        {
    
    
            // 更新成功
        }
        else
        {
    
    
            // 更新失败            
        }
    }

    connection.Close();
}

5. Visual management tools

SQLike does not have an official visualization tool because it is a lightweight embedded SQL database engine.
SQLike is more suitable for use as an embedded database engine directly embedded in C# applications, rather than running as a stand-alone database server.

However, you can manage and operate SQLike databases with the help of other third-party visualization tools.
These tools can provide visual interfaces and functions similar to those used in other database systems, such as query editors, data views, table views, chart displays, etc.

Here are some common SQL database management tools that can be used with SQLike

5.1、DBeaver

DBeaver is a free and open source general-purpose SQL database management tool that supports multiple database engines. You can connect and manage the SQLike database through DBeaver, and use the visual interface it provides for data manipulation and query.

5.2、HeidiSQL

HeidiSQL is a visualization tool designed for database engines such as MySQL, MariaDB, SQL Server and PostgreSQL. Although its main positioning is relational database, you can also try to use HeidiSQL to connect and manage SQLike database.

5.3, Navicat (recommended)

Navicat is a commercial database management tool that supports multiple database engines, including MySQL, SQL Server, SQLite, and PostgreSQL, etc. You can use the interface and functions provided by Navicat to manage SQLike database.

These tools provide a visual, user-friendly interface that can help you manage databases, execute queries, and manipulate data more easily.
You can choose the tool that suits you and use it by connecting to the SQLike database.
Note that these tools are generally designed for relational databases and may not provide SQLike-specific features or optimizations.
Therefore, there may be some functional limitations or compatibility issues when using these tools.

Guess you like

Origin blog.csdn.net/lmy_520/article/details/132097947