SQLite Learning Note 7:c using SQLite's Open database in the language

Source: Internet
Author: User
Tags sqlite



The basic content of the database is almost the same as before. Take a look at how to use SQLite in the C language.



One interface





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

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










Sqlite3_close (sqlite3*)

Close the database. Assuming that there are no statements running out before closing, you will returnSqlite_busy








Two examples



1 folder structure



projects{



main.c//code is located in the file



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



shell.c//This document is not actually available in the project. This file is used to generate the SQLite command tool, detailed can be taken: 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(stdout, "successfully open datebase.\n") ;
    }
    sqlite3_close(db) ;
    return 0;
}



To facilitate future expansion, code changes such as the following are now available:


#include <stdio.h>
#include <stdlib.h>
#include "sqlite/sqlite3.h"

#define DB_NANE "sqlite/test.db"

sqlite3 *db = NULL;
char* sql = NULL;
char *zErrMsg = NULL;
int ret = 0;

typedef enum{
    false,
    true
} bool;

static int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
    int i = 0;
    for(i=0; i < argc; i++){
        printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
    }
    printf("\n");
    
    return 0;
}

bool connectDB()
{
    ret = sqlite3_open(DB_NANE, &db);
    
    if( ret != SQLITE_OK){
        fprintf(stderr, "Error open database: %s\n", sqlite3_errmsg(db));
        sqlite3_free(zErrMsg);
        
        return false;
    }
    
    fprintf(stdout, "Successfully opened database\n");
    return true;
}

bool closeDB()
{
    int ret = 0;
    ret = sqlite3_close(db);
    if ( ret == SQLITE_BUSY ){
        return false;
    }
    
    return true;
}

int main(int argc, char* argv[])
{
    connectDB();
    closeDB();
    
    return 0;
}



3 Compile execution





There are two ways to do this, based on the previous notes. We do not configure the environment for SQLite. But download unzip get column A directory SQLite, so. Need to use such as the following command:





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

Assuming that the download configuration has SQLite installed, you will need to include the above header file instead:







#include <sqlite3.h>

Then run the command:







Gcc-o main MAIN.C  -lsqlite3







After the command is finished, a running file called Main is generated, and the input is:




./main

You can see the 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 this conversion.






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



SQLite Learning Note 7:c using SQLite's Open database in the language



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.