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;
}