SQLite Learning Note 7:c/c++ using SQLite's Open database

Source: Internet
Author: User
Tags sqlite

The basic content of the database has been said before, and then see how to use SQLite in C language.

One interface

Sqlite3_open (const char *filename, Sqlite3 **ppdb)

Open the database, create a new database if the database does not exist, and open the


Sqlite3_close (sqlite3*)

Close the database, and if there are statements that are not finished before closing, it will returnSqlite_busy


Two examples

1 directory structure

projects{

main.c//code is located in the file

sqlite{//Download down the SQLite compressed package files directory after decompression

shell.c//This file is actually not used in the project, this file is used to generate the SQLite command tool, the specific reference: SQLite Learning Note 1

Sqlite3.c

Sqlite3.h

Sqlite3ext.h

}

}


2 Source code

Main.c#include <stdio.h> #include <stdlib.h> #include "sqlite/sqlite3.h" #define DB_NAME "hanfeng.db" int Main () {    sqlite3* db = NULL;    char* msg = NULL;    int ret = 0;        ret = Sqlite3_open (db_name, &db);    if (ret) {        fprintf (stderr, "Error open datebase:%s\n.", db_name);        Exit (0);    }    else{        fprintf (stderr, "successfully open datebase.\n");    Sqlite3_close (db);    return 0;}

3 Compiling and running

Run there are two ways, based on the previous notes, we did not configure the SQLite environment, just download the extract to get the column a folder SQLite, so you need to use the following command:

Gcc-o main main.c./SQLITE/SQLITE3.C-LPTHREAD-LDL

If the download configuration has SQLite installed, you will need to change the header file above to include:

#include <sqlite3.h>

Then execute the command:

Gcc-o main MAIN.C  -lsqlite3


When the command executes, it generates an executable file called Main, entering:

./main

You can see both results.


#在编译时使用g + + will error: error:invalid conversion from ' const void* ' to ' const char* '

G++ seems to be more demanding on type conversions and does not support such conversions.

Passing the warrior, know how to compile with g++, please advise ...

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.