SQLite database multi-platform application and common error analysis

 

SQLite is a software library that implements a self-sufficient, serverless, zero-configuration, transactional SQL database engine. SQLite is one of the most widely deployed database engines in the world. The SQLite source code is open and there are no authorization restrictions. It is precisely because of its free, lightweight, green and other characteristics that SQLite has a wide range of applications and has applications in various software, including Adobe, Apple, Firefox, and even companies such as Google, Microsoft, and SUN. In the host computer project development. SQLite is also widely used. Today I will share with you the deployment, application and common error analysis of SQLite.

1 Database deployment

Compared with other databases, SQLite database has a big advantage in that it is free of deployment, which means it can be used without installation, but it also needs the support of database client management software. SQLite has a lot of client software, including SQLite Database Browser, SQLite Administrator, SQLite Expert Personal, SQLite Studio, etc. I prefer to use SQLite Studio. You can follow this official account for children's shoes you need and send the keyword SQLiteStudio in the background to get it. In order to introduce the SQLite data application later, we must first prepare a database and data table, which can be easily created through SQLite, and a file will be generated after the creation is completed.

As shown in the figure above, a SQLite database is created with the name SQLiteDemo, and a data table is created in the database with the name Student. There will be four fields under the Student data table, namely StudentID, StudentName, StudentGender and StudentClass, and to Two pieces of data are inserted.

2 Database application

First, create a Windows application. Simply drag and drop two controls on the form, a query button, and a display control DataGridView. All we have to do is to query data based on the SQLite database. As shown below:

As we all know, Windows development will have platform version problems. Generally, we use Any CPU. Any CPU is a 32-bit target platform by default, but sometimes we may need to use SQLite on a 64-bit platform. Today we will focus on Let's take a look at how to implement SQLite applications on various platforms.

2.1 Default Any CPU

In general, our projects use Any CPU as the target platform, and the default is 32-bit. If you want to implement SQLite database applications, you must first prepare some class libraries. Because SQLite is not a Microsoft database, there is no SQLite class library in Windows systems. SQLite class libraries will distinguish between 32-bit and 64-bit, as shown in the following figure:

  • First copy the public class library to the Debug root directory, and then manually add SQLiteHelper. SQLiteHelper is a packaged SQLite database operation class, as shown in the figure below. After adding, there will be an error message, and the System.Data.SQLite. Add references to dll to eliminate errors.

  •  

 

  • Set the connection string: The SQLite connection string points to the database file, so here you must first copy the previously created data file to the project and put it directly under the Debug path, and then set the connection character when the form is initialized. The format of the connection string is as follows:

  • Under the query button, write a simple query code, as shown below:

  • Run the program and execute the query event, the code will report an error, as shown in the following figure:

This error is often encountered in many children's shoes. The error message is very obvious, that is, the specified module is missing. The module name is SQLite.Interop.dll. If you still have an impression, this dll is in the SQLite library, 32-bit and 64-bit. Bit proprietary folder, as shown in the figure below, therefore, we only need to copy these two files under the x86 folder to the Debug directory, here must copy the x86 folder, because Any CPU is x86 by default platform.

  • After copying is complete, click query again, the results are as follows:

2.2 64-bit platform

When we use a 64-bit platform, first change the target platform to x64, as shown in the following figure:

At this time, when running the interface and clicking on the query, the following error will appear:

 

For this error, we copy the two files in the x64 folder to the Debug directory, then replace them, and then execute the query to achieve the final data reading:

When we modify the target platform in the figure below to x64 at the same time, and then regenerate, a x64 folder will be regenerated under the bin folder. At this time, all the generated content will be stored in the x64 folder, which is the same as before Debug has nothing to do.

 

 

At this time, regenerate, run the interface, click query, and the following error will appear again:

When we encounter this problem again, we should be very clear that we only need to copy these two files in the x64 folder in the class library to the Debug folder in the x64 folder in the Bin directory, and also need to copy the database Copy it in. Regenerate, click query, the results are as follows:

2.3 x86 platform

When we modify the target platform in the figure below to x86, we will regenerate it. Similarly, we will regenerate an x86 folder in the bin folder. At this time, all generated content will be stored in the x86 folder, which is the same as before. The Debug has nothing to do. The solution here is similar to the description in 2.2 above, so I will not repeat it.

3 Overall summary

Through the instructions for each platform, we found that there are only a few common errors in the use of SQLite, and the solutions are similar. We always need to follow a principle to ensure that the third-party dll we need is placed in the path generated by the project. When we don't know the path of project generation, we can clean up the solution and regenerate the solution to observe whether the modification time of the project exe is the current latest time, so as to determine whether the path is the project generation path.

 

Guess you like

Origin blog.csdn.net/xiketangAndy/article/details/107383585