(so easy) embedded database - sqlite

Table of contents

1. Database management tools operate on the database

2. Code creation using the database


Sqlite database management tool to operate sqlite database files

Sqlite opens the database management tool for operation

1. Database management tools operate on the database

Install the sqlite3 database management tool

sudo apt-get install sqlite3

Enter at the command line: sqlite3 - open the database management tool

(1) Database commands: start with ., which means using database management

.help: help, display all database commands

 

.open: Open a database, create it when the database file does not exist - open filename.db

.quit: Exit the database management tool

.database: View the currently open database

.tables: View the tables in the current database (the data in the database file is stored in tables)

(2) Database statements: perform data operations on the database (add, delete, check, and modify). end with

# Create table: create table table name (field 1, field 2, field 3...);

field: field name field type attribute

# Add data:

The first type: insert inio table name values ​​(field value 1, field value 2, field value 3....)

The second type: insert inio user table name (field 1, field n) values ​​(field name 1 value, field name n value)

# Query data

Select field 1 field 2... from table name (where condition), where not written means that all data meets the condition, * means all

# change the data

updata table name set field name = value, field 2 = value where condition;

 Indicates updating one or more pieces of corresponding data that meet the conditions

 

# delete data

delete from table name where condition;

One or more pieces of data that meet the conditions

# delete table

drop table table name;

 

2. Code creation using the database

Data is also stored and manipulated in the program, and a database can be used as storage. The database will be operated in the program - use the API function about the database

sudo apt-get install libsqlite3-dev

# open the database file

int sqlite3_open(const char *filename,sqlite3 **ppDb);

Parameter 1:

const char *filename: the path of the database file to be opened

Parameter 2:

sqlite3 **ppDb: The sqlite3* first-level pointer is used to identify an open database file. If you want to set the open database file through parameters, it means that you want to modify the first-level pointer. The address for passing the first-level pointer is sqlite3 **ppDb: set to open database file

return value:

        Success: return 0 (SQLITE_OK)

        Failure: return non-zero

#Operate the database file: execute the specified sql statement

int sqlite3_exec(

 sqlite3 * pdb, /* open database file to be operated */

 const char *sql, /*sql statement string for database operation*/

 int (*callback)(void*, int, char**, char**), /* function pointer, callback function, when there is an execution result when executing the sql statement, call and execute this function once*/

 void * arg, /* the first parameter provided to the callback function */

 char **errmsg/* The address of the first address of the error message, put the error message in the variable of the first address */

);

return value:

        Success: return 0

        Failure: return non-zero

The callback function is executed every time a record is found. When exec is executed, it is called when there is a result. The parameter is the result of exec execution

int callback(void * arg,int f_num,char **f_val,char**

f_name)

parameter:

Parameter 1:

void * arg: Specify the parameters passed to the callback in the exec function

Parameter 2:

int f_num: the number of fields contained in a result record

Parameter 3:

char **f_val: the first address of the pointer array, storing each field value in a result

Parameter 4:

char** f_name: Point to the first address of the array, storing the field name of each field in a result

# close the database file

int sqlite3_close(sqlite3 * pdb);

Parameter 1:

sqlite3 * pdb: handle, a first-level pointer to identify the open data file

return value:

Success: return 0 Failure: return non-zero

 

The overall implementation code is as follows:

//存储id、用户名、密码在数据库,用于操作(增、删、修、查询)

#include <stdio.h>

#include <stdlib.h>

#include <sqlite3.h>


int callok(void * arg,int f_num,char **f_val,char **f_name)

{

printf("%s\n",(char *)arg);

for(int i = 0; i < f_num;i++)

{

printf("%s|",f_name[i]);

}

printf("\n");


for(int i = 0; i < f_num;i++)

{

printf("%s|",f_val[i]);

}


printf("\n");

return 0;

}


int main()

{

//1、打开数据库文件,如果没有就创建

sqlite3 *pdb;//用于表示打开的数据库文件

if(sqlite3_open("user.db",&pdb) == SQLITE_OK)//打开数据库

{

printf("open success\n");

}

else

{

printf("open failed\n");

exit(-1);

}



//2、操作数据库文件

// char sql[100];

// sprintf(sql,"create table %s(%s,%s,%s)","stu","name text","age int","sex text");

// printf("%s\n",sql);

/*

char createsql[] = "create table users(id int not null,name text not null,passwd text)";

int ret = sqlite3_exec(pdb,createsql,NULL,NULL,NULL);

if(ret == 0)

{

printf("create ok\n");

}

else

{

printf("create error\n");

}


char sql[100];

memset(sql,0,100);

int id = 0;

char name[20] = "xiaozhang";

char passwd[20] = "123456";

//添加数据

sprintf(sql,"insert into users values(%d,\"%s\",'%s')",id,name,passwd);

printf("%s\n",sql);

sqlite3_exec(pdb,sql,NULL,NULL,NULL);

char sql[100];

char name[20] = "xiaoli";

char passwd[20] = "123456543";

sprintf(sql,"update users set name='%s',passwd='%s' where id=%d",name,passwd,0);

sqlite3_exec(pdb,sql,NULL,NULL,NULL);

*/

//查询 select from

char *msg = NULL;

int ret = sqlite3_exec(pdb,"select * from users ",callok,"ok",&msg);

if(msg != NULL && ret != 0)

{

printf("%s\n",msg);

}


//3、关闭数据库文件

if(sqlite3_close(pdb) == 0)

{

printf("close success\n");

}

else

{

printf("close failed\n");

exit(-1);

}

return 0;

}

Guess you like

Origin blog.csdn.net/qq_53676406/article/details/129288231