Introduction to SQLite and C/C ++

Source: Internet
Author: User


: Http://www.sqlite.org/download.html
On this page
Download sqlite-amalgamation-3071300.zip from source code. the package contains two header files and two implementation files.



Download sqlite-dll-win32-x86-3071300.zip from precompiled binaries for windows. the package contains a Def file and a DLL file.






Because the content downloaded on the official website does not have Lib, You need to compile it by yourself. Let's talk about the compilation process here.
In vs2010, create a project, Win32 console, select a blank DLL project, and decompress all downloaded files.
In this project, select static library. Lib as the configuration type in project --- properties --- general, and click Generate.



The followingProgramAn example I wrote has basic functions, but the method may not be very good.





#include "sqlite3.h"
#include <stdio.h>
#include <stdlib.h>
#pragma comment (lib, "Sqllib.lib")


int select_callback (void * data, int col_count, char ** col_values, char ** col_Name);
void DoSelect (int id);
sqlite3 * db = NULL;
char * zErrMsg = NULL;

int select_callback (void * data, int col_count, char ** col_values, char ** col_Name)
{
for (int i = 0; i <col_count; i ++)
printf ("% s =% s;", col_Name [i], col_values [i] == 0? "NULL": col_values [i]);
printf ("\ n");
return 0;
}

int main ()
{
int rc;
int select;
rc = sqlite3_open ("Test.s3db", & db);
if (rc)
{
fprintf (stderr, "The database failed to open, please check% s \ n", sqlite3_errmsg (db));
} else
{
do
{
printf ("******** Database is connected ******** \ n");
printf ("Please select the corresponding operation: \ n");
printf ("1, increase data \ n");
printf ("2. Delete a single piece of data \ n");
printf ("3. Modify data \ n");
printf ("4. Delete all data \ n");
printf ("5. View all data \ n");
printf ("6. Exit the program \ n");
scanf ("% d", & select);
DoSelect (select);
} while (select! = 6);
}
return 0;
}

void DoSelect (int id)
{
char strSQL [100];
char name [50], address [100];
int i;
switch (id)
{
case 1:
printf ("Please enter name address \ n");
scanf ("% s% s", name, address);
sqlite3_exec (db, "begin;", 0,0, & zErrMsg);
sprintf (strSQL, "insert into test (name, address) values ('% s', '% s');", name, address);
sqlite3_exec (db, strSQL, NULL, NULL, & zErrMsg);
sqlite3_exec (db, "commit;", 0,0, & zErrMsg);
break;
case 2:
sqlite3_exec (db, "select * from test;", select_callback, 0, & zErrMsg);
printf ("Please enter the id \ n" to be deleted);
scanf ("% d", & i);
sprintf (strSQL, "delete from test where id =% d;", i);
sqlite3_exec (db, strSQL, NULL, NULL, & zErrMsg);
break;
case 3:
sqlite3_exec (db, "select * from test;", select_callback, 0, & zErrMsg);
printf ("Please enter the id \ n" to be modified);
scanf ("% d", & i);
printf ("Please enter a new name address \ n");
scanf ("% s% s", name, address);
sprintf (strSQL, "update test set name = '% s', address = '% s' where id =% d;", name, address, i);
sqlite3_exec (db, strSQL, NULL, NULL, & zErrMsg);
sqlite3_exec (db, "select * from test;", select_callback, 0, & zErrMsg);
break;
case 4:
sqlite3_exec (db, "delete from test;", select_callback, 0, & zErrMsg);
break;
case 5:
sqlite3_exec (db, "select * from test;", select_callback, 0, & zErrMsg);
break;
case 6:
sqlite3_close (db);
break;
}
} 



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.