Implement C ++ operations for SQLite databases step by step

Source: Internet
Author: User
Tags sql error

SQLiteIs a lightweight database that compliesACIDAssociationRelational Database Service (RDS) is already used in many embedded products. It occupies very low resources and may only require several hundredKThe memory is enough. It supportsWindows/Linux/UnixAnd so on.In the operating system, the same code can be compiled across platforms. This database and Microsoft AccessVery much, they are all small database management systems, and the biggest difference isSqliteNoOfficeIs a true cross-platform database..

Due to work needs, these days for sqliteStudy the database and useC ++A simple operation is performed on it. I encountered many problems and found a lot of information to solve them. To sum up the problem, no matter whether the problem is improved, write out the problem and solution and hope it will be useful to you.

1: Download and compile the source code

My version isSqlite-amalgamation-3070701.zip, Which contains the main source code.Sqlite-dll-win32-x86-3070701.zipThis isWindowsCompiledDLLFile andDefFile, which contains two files,Sqlite3.defAndSqlite3.dll.

It is very easy to compile the source code.C ++Empty Project, putSqlite-amalgamation-3070701.zipCopy the decompressed file and compile and link it.

My goal isSqliteA database is used as a part of its own project and embedded. This should be exploitedSqlite3.dllFile. However, the source file only hasSqlite3.defAndSqlite3.dllNoSqlite3.libFile, how to use it?

LIBFile andDLLThe file is actually similar, but the time used is different.LIBFile Usage in the compilation phaseDLLThe file is used in the running stage. AccordingDefFile can generate the correspondingLIBFile. Command Line generationLIBFile.

FindVSInstallation path of, my isD: \ Program Files \. Use the command line to enter the following path.

D: \ Program Files \ Microsoft Visual Studio 9.0 \ VC \ bin> lib/def: sqlite3.def/machine: ix86

Problem 1: mspdb80.dllCannot be found

The reason is that there is no"Msobj80.dll, mspdb80.dll, mspdbcore.dll, and mspdbsrv.exe.

Solution:Common7 \ IDE \Copy the four filesVC \ Bin \.

Corresponding generationLibNote thatCopy sqlite3.def to the preceding path and generateSqlite3.libFile, which is required for compilation in the program. The result is as follows:

Microsoft (R) Library Manager Version 9.00.21022.08

Copyright (C) Microsoft Corporation. All rights reserved.

Creating Database...Sqlite3.libAnd objectSqlite3.exp

2: InC ++Operation in progressSQLiteDatabase

Problem 2: sqlite3.dll not found, So this application failed to start

I putSqlite3.dll,Sqlite3.h,Sqlite3.libCopy to the same folder. This problem is known during compilation.

Originally required for Program ExecutionDLLSearch by current working path and system directoryDLLFile. MyDLLThe file is not in the same path as the executable file. Understand thisSqlite3.dllCopyDebugFolder.

3: Demo code

The main source code is from the internet. I modified the source code.

  1. # Include <iostream>
  2. Using NamespaceStd;
  3. # Include "./sqlite/sqlite3.h"
  4. # Pragma comment (lib, "./sqlite/sqlite3.lib ")
  5. Static Int_ Callback_exec (Void* Notused,IntArgc,Char** Argv,Char** AszColName)
  6. {
  7. IntI;
  8. For(I = 0; I <argc; I ++)
  9. {
  10. Printf ("% S = % s \ n", AszColName [I], argv [I] = 0?"NUL": Argv [I]);
  11. }
  12. Return0;
  13. }
  14. IntMain (IntArgc,Char* Argv [])
  15. {
  16. Const Char* File ="Test. db";
  17. Const Char* SSQL ="Select * from stu ;";
  18. Char* PErrMsg = 0;
  19. IntRet = 0;
  20. Sqlite3 * db = 0;
  21. Ret = sqlite3_open ("./Test. db", & Db );
  22. If(Ret! = SQLITE_ OK)
  23. {
  24. Fprintf (stderr,"Cocould not open database: % s", Sqlite3_errmsg (db ));
  25. Exit (1 );
  26. }
  27. Printf ("Successfully connected to database \ n");
  28. Sqlite3_exec (db, sSQL, _ callback_exec, 0, & pErrMsg );
  29. If(Ret! = SQLITE_ OK)
  30. {
  31. Fprintf (stderr,"SQL error: % s \ n", PErrMsg );
  32. Sqlite3_free (pErrMsg );
  33. }
  34. Sqlite3_close (db );
  35. Db = 0;
  36. Return0;
  37. }

4:SQLiteNotes

I operate on the command lineSQLiteA database cannot be generated until it enters the database, and the Table Generation Command is not successful.

Then I finally understood,SQLiteThe command line format is:Sqlite.exe dbfile, Followed by the database name. If it does not exist, a database name is automatically generated after the operation.

SQLiteOfSQLAll commands are;"Is the Terminator, And the execution fails. It was not added.";"Statement end symbol.

Finally, a simple SQLite program is completed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.