How to operate SQLite databases using Python and VC6.0
During this time, due to work needs, I simply learned how to operate the SQLite database. In order to easily write the collected data into the SQLite database, I used Python. However, since C is needed to implement the Data Processing Algorithm in the later stage, VC6.0 must also be used to operate SQLite data. In order to summarize the study during this period, and to use relevant knowledge in the future, we can directly refer to the accumulated results. We hereby record these work in this blog. Of course, because it involves data confidentiality issues and algorithms should not be made public, Here we only introduce the Python and VC6.0 operating code for SQLite.
The first step is to install SQLite. The database I selected here is Navicat for SQLite. The interface is simple and easy to operate, and the maximum data volume can be 2 TB. In addition, to import the static Link Library to the next VC6.0, you need to have the following files:
(1) sqlite3.dll and sqlite3.def
(2) sqlite3.lib and sqlite3.h
Here, it is generated by sqlite3.def. On the DOS command line interface, the directory is transferred to the SQLite location through the cd and drive letter switch commands, and the command: LIB/DEF: sqlite3.def is run. The following common libraries will generate output:
C: \ sqlite> lib/DEF: sqlite3.def
Microsoft (R) Library Manager Version 10.00.30319.01
...... (For details, see the SQLite authoritative guide (version 2 ))
Sqlite3.h can be downloaded from the Internet directly. If necessary, contact me. In this way, the preparations for the first step are completed.
The second step is to install Python and VC6.0, so I will not be embarrassed. Here I use Python 2.7. The data file is data.txt.
1. Navicat for SQLite operation interface
Navicat for SQLite is a fast, reliable and affordable database management tool designed to simplify database management and reduce system management costs. It is quite easy for new users of database servers to learn. It has an excellent graphic user interface (GUI) that allows users to easily create, organize, access, and share information in a secure and simple way.
When we get the data. db file, we can use SQL statements to query the database by establishing a connection. You need to import four columns of data. (Sorry, I cannot upload the full data view ):
2. Python operations on the SQLite Database
Next, open the Python 2.7 IDLE (Python 2.7 GUI) and write the Python code for operating the SQLite database, as shown in:
File → New Window: Create a New. py File and write the following code:
# Import Python SQLite database module
Import sqlite3
# Create/open a database
Cx = sqlite3.connect ("E:/data. db ")
# Querying a database with a cursor
Cu = cx. cursor ()
# Table Creation
Cu.exe cute ("create table me (id integer primary key, ivalues1 string, ivalues2 string, ivalues3 string, ivalues4 string )")
TxtRead_0 = open ("E:/0.txt", 'R ');
File_0 = txtRead_0.readlines ();
Icount = 1
For line in file_0:
Ivalues1, ivalues2, ivalues3, ivalues4 = line. split ('\ t ')
Cx.exe cute ("insert into me values ('% d',' % s')" % (icount, ivalues1, ivalues2, ivalues3, ivalues4 ))
Cx. commit ()
Icount + = 1
TxtRead_0.close
This completes writing data in data.txt to the database file data. db. You can use Navicat for SQLite to perform query and other operations, or export the groups you are interested in.
3. Operations of VC6.0 on SQLite Database
Compared with Python for SQLite operations, using VC6.0 to operate databases is a little more troublesome, but considering the portability of code in the future, even if it is troublesome, it is worthwhile.
Open the Project Settings interface through Project → Settings on the VC6.0 interface, as shown in:
Add the previously obtained sqlite3.lib in Object/library modules: to build our own program. Copy sqlite3.lib and sqlite3.dll to the current project directory. Then, follow the new project we are most familiar with and add the previously downloaded sqlite3.h to the project as the Win32 console program. Then we write the code as follows:
# Include <stdio. h>
# Include "sqlite3.h"
Int main (void)
{
Sqlite3 * db = NULL;
Char * MyErrMsg = 0;
Int icount;
// Open the specified database file. If it does not exist, a database file with the same name will be created.
Icount = sqlite3_open ("data. db", & db );
If (icount)
{
Fprintf (stderr, "Cann't open database: % s \ n", sqlite3_errmsg (db ));
Sqlite3_close (db );
}
Else
Printf ("Open data. db successfulliy! \ N ");
Sqlite3_close (db );
Return 0;
}
Here I will just briefly introduce the simple operations on SQLite using VC6.0 and Python. As for insert, update, delete, and other operations, and programming based on your own application scenarios, I will not go into details here. If you are interested, contact me. Thank you!