Sqlite: application in C program

Sqlite brief introduction:

1. It's a relational database. It's open source, not open contribution. That is to say, everyone can get the source code, but only the relevant personnel can upload the code. After downloading it to the local area, they can modify the code for their own use, but they can't upload it. There's a special person to maintain it, that's to say, the quality is guaranteed.

2. Another feature is that, unlike other relational databases, it works with applications and does not need a server running independently. It only reads and writes files on the current hard disk, which is also the reason for its rapid promotion.

3. Written in ANSI-C, there are two files. c and. h. It can be cross platform, whether windows, IOS or Andorid are supported, and it is also the component of mobile phone.

Sqlite is used in C ා:

Then we can use the following steps to simply build a C program and use Sqlite to build the database.

1. Build a windows form project with Visual Studio:

2. Use NuGet to install the ADO.NET provider of SQLite, which can be used to connect (open files, close files, etc.) SQLite database.

First, right-click the project to open Nuget Manager

If you search SQLite in search, you will list a lot of packages about SQLite, select System.Data.Sqlite package (in order to be compatible with. NET CORE, a new mircosoft.data.sqlite is developed, API is basically the same, and cross platform is implemented), and install it

3. Create a new class

4. Add new methods to the new class

        SQLiteConnection con;
        SQLiteCommand cmd;
        SQLiteDataReader dr;
        public void CreateDatabaseAndTable()
        {
            if (!File.Exists("MyDatabase.sqlite"))
            {
                SQLiteConnection.CreateFile("MyDatabase.sqlite");
 
                string sql = @"CREATE TABLE Student(
                               ID INTEGER PRIMARY KEY AUTOINCREMENT ,
                               FirstName           TEXT      NOT NULL,
                               LastName            TEXT       NOT NULL
                            );";
                con = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
                con.Open();
                cmd = new SQLiteCommand(sql, con);
                cmd.ExecuteNonQuery();
                con.Close();
 
            }
            else
            {
                con = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
            }
        }

In the CreateDatabaseAndTable() function, we first judge whether the file MyDatabase.sqlite exists. If it does not exist, we first create the file and create a new table Student. If the file already exists, we directly open the file.

4. If you want to add data or query data, please refer to if code

        public void AddData(string name,string lastname)
        {
            cmd = new SQLiteCommand();
            con.Open();
            cmd.Connection = con;
            cmd.CommandText = "insert into Student(FirstName,LastName) values ('" + name + "','" + lastname + "')";
            cmd.ExecuteNonQuery();
            con.Close();
        }
 
        public void SelectData()
        {
            int counter = 0;
            cmd = new SQLiteCommand("Select *From Student", con);
            con.Open();
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                counter++;
                Console.WriteLine(dr[0] + " : " + dr[1]+" "+dr[2]);
 
            }
            con.Close();
 
        }

5. In the Main function, you can make the following call, then the example is finished.

    class Program
    {
        static void Main(string[] args)
        {
            Sample samp = new Sample();
            samp.CreateDatabaseAndTable();
 
 
            Console.Write("Add Name      :");
            string name = Console.ReadLine();
 
            Console.Write("Add LastName  :");
            string lastname = Console.ReadLine();
 
            samp.AddData(name,lastname);
 
            Console.WriteLine("*************************");
            samp.SelectData();
 
            Console.ReadLine();
        }
    }

 

 

 

 

 

 

 

 

 

 

 

 

 

28 original articles published, 5 praised, 30000 visitors+
Private letter follow

Tags: SQLite Database Windows SQL

Posted on Tue, 17 Mar 2020 03:28:52 -0400 by renaker