A self-made library file that operates a SQLite database

Source: Internet
Author: User
Tags printf sqlite sqlite database


A self-made library file that operates SQLite databases, and the IDE used to write is KDevelop3.3.4.



Header file:


#ifndef _SQLITE3LIB_H_
#define _SQLITE3LIB_H_

#include <stdio.h>
#include <stdlib.h>
#include<sqlite3.h>

Typedef struct
{
     Char **result;
     Int row;
     Int col;
     Char *errmsg;
}sqliteResSet;

/*
* Function: Execute sql statement, return 0 when the call succeeds, and release errmsg, which is suitable for executing the "add, delete, change" type of sql statement
*db: The database to be operated, no need to open first
*errmsg: information returned if an error occurs while executing the sql statement
*/
Int sqlite3_carrySql(const char *db, const char *sql, char *errmsg);


/*
* Function: Execute the sql statement of the query. When the query is successful, it returns 0 and stores a result set in the table.
*db: The database to be operated, no need to open first
*/
Int sqlite3_getResSet(const char *db, const char *sql, sqliteResSet *table);


#endif /*_SQLITE3LIB_H_*/



Running the Demo:


?
#include <stdio.h>
#include <stdlib.h>
#include<sqlite3Lib.h>

Int main(int argc, char *argv[])
{
   sqliteResSet table;
   Int result, i, j;
   Char *Errormsg;
  
   Char *sql="create table table1(id, name);insert into table1 values(1, 'Tom');insert into table1 values(2, 'Tom')";
   Result = sqlite3_carrySql("test.db", sql, Errormsg);
   If(result)
   {
       Printf("operation database failed!\n");
   }
  
   Sql = "select * from table1";
   Result = sqlite3_getResSet("test.db", sql, &table);
   If(result)
   {
       Printf("Query database failed!\n");
   }
   Else
   {
       Printf ("print all data for table1: \n");
       The data of the /*sqlite database table is equivalent to being stored in a one-dimensional array, and the column name of the first behavior table */
       For(i = 0; i < table.row + 1; i++)
       {
           For(j = 0; j < table.col; j++)
           {
   Printf("%s\t", table.result[j + i * table.col]);
           }
Printf("\n");
       }
   }

   Return EXIT_SUCCESS;
}

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.