The reason why SQLite inserts data slowly: SQLite uses transaction operations for each insert operation when transactions are not explicitly used, while SQLite databases exist on disks as files, it is equivalent to opening a file every time you access the file. If you perform a large amount of operations on the data, the time is spent on I/O operations, so it is very slow.
The solution is to explicitly commit in the form of transactions: Because after a transaction is started, a large number of statements for operations are stored in the memory, and all the statements are written to the database only when the transaction is committed. At this time, the database file is opened only once.
I used 100 s when I did not explicitly insert 12.226 data records in the transaction form. In the explicit transaction form, it took 100 seconds to insert 0.172 data records and 1000000 data records, it's very relevant.
Example of explicitly using transactions:
# Include <iostream> # Include <Windows. h> Using Namespace STD; # include " SQLite/sqlite3.h " Int Main () {sqlite3 *DB; Int Nresult = sqlite3_open ( " Test. DB " ,& DB ); If (Nresult! = Sqlite_ OK) {cout < " Failed to open database: " <Sqlite3_errmsg (db) < Endl; Return 0 ;} Else {Cout < " Database opened successfully " < Endl ;} Char * Errmsg; nresult = Sqlite3_exec (dB, " Create Table fuck (ID integer primary key autoincrement, name varchar (100 )) " , Null, null ,& Errmsg ); If (Nresult! = Sqlite_ OK) {sqlite3_close (db); cout < Errmsg; sqlite3_free (errmsg ); Return 0 ;} String Strsql; strsql + = " Begin; \ n " ; For ( Int I =0 ; I < 100 ; I ++ ) {Strsql + = " Insert into fuck values (null, 'heh'); \ n " ;} Strsql + = " Commit; " ; // Cout <strsql <Endl; Systemtime tm_s; getlocaltime ( &Tm_s); nresult = Sqlite3_exec (dB, strsql. c_str (), null, null ,& Errmsg); systemtime tm_e; getlocaltime ( & Tm_e ); If (Nresult! = Sqlite_ OK) {sqlite3_close (db); cout <Errmsg < Endl; sqlite3_free (errmsg ); Return 0 ;} Cout < " Start: " <Tm_s.wminute < " : " <Tm_s.wsecond < " : " <Tm_s.wmilliseconds < Endl; cout < " End: " <Tm_e.wminute < " : " <Tm_e.wsecond < " : " <Tm_e.wmilliseconds < Endl; Return 0 ;}