当前位置:网站首页>Embedded database development programming (zero)
Embedded database development programming (zero)
2022-07-05 05:07:00 【Light chasing rain】
List of articles
One database
1.1 stay ubuntu Install database in
sudo apt-get install sqlite3 libsqlite3-dev
Install the test :
Input sqlite3, If you can successfully access the database , Description installation successful :
1.2 Operation of database
1.2.1 Classification of database commands
System commands : In order to . The first order , It is mainly used to operate the current database
Be careful : The system command cannot be followed by ;
A common order : In order to ; The order at the end , It mainly operates the tables in the database
Be careful : Ordinary commands cannot be preceded by .
1.2.2 Common system commands
.help: View help information , List all system commands
.exit: Exit database
.quit: Exit database
.databases: View the current database information
.tables: List the table names of all tables in the current database
.schema: List the structure of all tables in the database
1.2.3 Common statements in data
-- Get into sqlite3
sqlite3 student.db
-- Exit database
.exit
-- Create a table
create table stu(id int,name char,sex char,score int);
Be careful : The type of character or string is specified as char perhaps text
-- View table structure
.schema
-- insert data
insert into stu (id,name,sex,score) values(1001,"zhangsan",'m',98);
insert into stu values(1002,"lisi",'f',99);
insert into stu values(1003,"wanger",'m',100),(1004,"xiaoming",'f',101);
-- Query data
select * from stu; // Find all records
select *from stu where sex='f'; // Find records that match the criteria
select *from stu where sex='f' and id = 1002;
select *from stu where sex='f' or id = 1001;
select id,name from stu; // Query only id and name
-- Delete record
delete from stu where id = 1001;
-- Modify the record
update stu set score=77 where id = 1003;
-- Add a column
alter table stu add column vip int;
-- Delete a column
sqlites It is not allowed to delete a column directly
1) First create a new table
create table stu1 as select id,name from stu;
2) Delete the old table
drop table stu;
3) Rename the new table
alter table stu1 rename to stu;
-- Database primary key ( The set data will be unique )
create table usr(name text primary key,passwd text);
-- Delete table
drop table stu;
1.3 sqlite Commonly used in database api
1.3.1 sqlite3_open
The header file :#include <sqlite3.h>
Prototype :int sqlite3_open(const char *filename,sqlite3 **ppDb);
function : Open or create a database
Parameters :
filename: Database name
PPdb: Pointer to the operation database , Handle .
Return value :
success : return :SQLITE_OK
Failure :error_code It can be done by sqlite3_errmsg Get error messages
1.3.2 int sqlite3_close(sqlite3 * db)
function : Shut down a database
1.3.3 sqlite3_exec
The header file :#include <sqlite3.h>
Prototype :int sqlite3_exec(sqlite3 db,const char sql,int (callback)(void ,int,char,char),void *arg,char errmsg);
function : To perform a sql sentence
Parameters :
db: Handle pointer to the database
sql: Will be executed sql sentence
callback: Callback function , Only when querying statements , Will pass parameters to the callback function
arg: by callback Ginseng
errmsg: Error message address
Return value :
success : return :SQLITE_OK
Failure :error_code It can be done by sqlite3_errmsg Get error messages
******************************************************
int (*callback)(void *arg,int ncolumn,charf_value,char**f_name)
function : Get the results of the query
Parameters :
arg: Used for parameter passing of callback function
ncolumn: The number of fields the record contains ( Number of columns )
f_value: An array of pointers containing the values of each field
f_name: An array of pointers containing the names of each field
Return value :
success 0
error : Not 0
1.3.4 sqlite3_get_table
Prototype :int sqlite3_get_table(sqlite3 *db,const char *sql,char ***pazresult,int *pnRow,int *pnColumn,char **pzErrmsg);
function : Query the database , It creates a new memory area to store the result information of the query
Parameters :
db: Handle pointer to the database
sql: Will be executed sql sentence
pazresult: Result of query
pnRow: Row number
pnColumn Number of columns
pzErrmsg error message
Return value :
success 0
error :errcode
pnRow The value of is the number of qualified records ( Field name is not included ).
pnColumn The value of is the number of qualified fields .
1.3.5 void sqlite3_free_table(char ** result)
function : Free memory
#include <stdio.h>
#include <sqlite3.h>
#include <string.h>
int print(void *arg,int column,char **value,char **name) //column Is the number of columns ,name: Field name value: data
{
int i;
for(i = 0 ; i < column;i++)
{
printf("%s = %s ",name[i],value[i]);
}
printf("\n");
return 0;
}
int main(int argc, char const *argv[])
{
sqlite3 *ppdb;
// Open or create a database
int ret = sqlite3_open("stu.db",&ppdb);
if(ret != SQLITE_OK)
{
printf("sqlite3 open: %s\n",sqlite3_errmsg(ppdb));
return -1;
}
// Create table
char sql[128] = {
0};
sprintf(sql,"create table if not exists stu(id int,name char,sex char,score int);");
ret = sqlite3_exec(ppdb,sql,NULL,NULL,NULL);
if(ret != SQLITE_OK)
{
printf("sqlite3_exec: %s\n",sqlite3_errmsg(ppdb));
return -1;
}
// insert data
int i;
int id,score;
char name[32] = {
0};
char sex;
/*for(i = 0; i < 2;i++) { printf(" Please enter the student number 、 full name 、 Gender 、 fraction :\n"); scanf("%d%s %c %d",&id,name,&sex,&score); memset(sql,0,sizeof(sql)); sprintf(sql,"insert into stu values(%d,'%s','%c',%d);",id,name,sex,score); ret = sqlite3_exec(ppdb,sql,NULL,NULL,NULL); if(ret != SQLITE_OK) { printf("sqlite3_exec2: %s\n",sqlite3_errmsg(ppdb)); return -1; } }*/
memset(sql,0,sizeof(sql));
sprintf(sql,"select * from stu;");
/*ret = sqlite3_exec(ppdb,sql,print,NULL,NULL); if(ret != SQLITE_OK) { printf("sqlite3_exec: %s\n",sqlite3_errmsg(ppdb)); return -1; }*/
char **result;
int row,column;
ret = sqlite3_get_table(ppdb,sql,&result,&row,&column,NULL); //row: That's ok column: Column
if(ret != SQLITE_OK)
{
printf("sqlite3_get_table : %s\n",sqlite3_errmsg(ppdb));
return -1;
}
int j;
int Index = column;
for(i = 0; i < row;i++) // That's ok
{
for(j = 0; j < column;j++) // Column
{
printf("%s = %s ",result[j],result[Index]);
Index++;
}
putchar(10);
}
return 0;
}
边栏推荐
猜你喜欢
Unity parallax infinite scrolling background
Use assimp library to read MTL file data
Detailed introduction of OSPF header message
Number theoretic function and its summation to be updated
Establish cloth effect in 10 seconds
XSS injection
小程序直播+電商,想做新零售電商就用它吧!
Stm32cubemx (8): RTC and RTC wake-up interrupt
Unity check whether the two objects have obstacles by ray
Ue4/ue5 illusory engine, material part (III), material optimization at different distances
随机推荐
Pause and resume of cocos2dx Lua scenario
Stm32cubemx (8): RTC and RTC wake-up interrupt
win10虚拟机集群优化方案
Introduction to JVM principle and process
2022 U.S. college students' mathematical modeling e problem ideas / 2022 U.S. game e problem analysis
Simple HelloWorld color change
SQLServer 存储过程传递数组参数
Redis has four methods for checking big keys, which are necessary for optimization
669. Prune binary search tree ●●
xss注入
AutoCAD - scaling
C iterator
《动手学深度学习》学习笔记
Unity find the coordinates of a point on the circle
Cocos create Jiugongge pictures
Sixth note
3dsmax common commands
Transport connection management of TCP
2020-10-27
Research on the value of background repeat of background tiling