Sqlite study notes 7: Using sqlite in C/C ++ to open a database

Source: Internet
Author: User
The basic content of the database has been mentioned earlier. Next, let's take a look at how to use sqlite in the C language. One interface: sqlite3_open (constchar * filename, sqlite3 ** ppDb) Open the database. If the database does not exist, create a new database and open sqlite3_close (sqlite3 *) to close the database.

The basic content of the database has been mentioned earlier. Next, let's take a look at how to use sqlite in the C language. One interface: sqlite3_open (const char * filename, sqlite3 ** ppDb) Open the database. If the database does not exist, create a new database and open sqlite3_close (sqlite3 *) to close the database.

The basic content of the database has been mentioned earlier. Next, let's take a look at how to use sqlite in the C language.

Interface 1

sqlite3_open(const char *filename, sqlite3 **ppDb)
Open the database. If the database does not exist, create a new database and open
sqlite3_close(sqlite3*)
Shut down the database. If you close a statement that has not been executed, SQLITE_BUSY is returned.

Example 2

1. directory structure

Projects {

Main. c // file where the code is located

Sqlite {// directory of the decompressed sqlite package downloaded from the official website

Shell. c // This file is not actually used in the project. This file is used to generate the sqlite command tool. For details, refer to: sqlite study note 1.

Sqlite3.c

Sqlite3.h

Sqlite3ext. h

}

}

2 source code

// main.c#include 
 
  #include 
  
   #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 compile and run

There are two ways to run the command. Based on the preceding notes, we didn't configure the sqlite environment. We just downloaded and decompressed the file to get a folder named sqlite. Therefore, we need to use the following command:

gcc -o main main.c ./sqlite/sqlite3.c -lpthread -ldl
If sqlite is installed in the download configuration, you need to change the header file:
#include 
 
Then run the following command:
gcc -o main main.c  -lsqlite3
After the command is executed, an executable file named main is generated. Enter:
./main
You can see the result.

# If g ++ is used during compilation, the following error occurs: error: invalid conversion from 'const void * 'to 'const char *'

G ++ seems to have stricter requirements on type conversion and does not support such conversion.

If you know how to compile with g ++, please advise ......

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.