当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

PostgreSQL surpasses mysql, and the salary of "the best programming language in the world" is low

嵌入式数据库开发编程(六)——C API

AutoCAD -- dimension break

Stm32cubemx (8): RTC and RTC wake-up interrupt

小程序直播+電商,想做新零售電商就用它吧!

669. Prune binary search tree ●●
![Rip notes [rip three timers, the role of horizontal segmentation, rip automatic summary, and the role of network]](/img/e7/f699ee982ea325b8d04f8bd467a559.jpg)
Rip notes [rip three timers, the role of horizontal segmentation, rip automatic summary, and the role of network]

Pdf to DWG in CAD

Detailed introduction of OSPF header message

LeetCode之单词搜索(回溯法求解)
随机推荐
Unity check whether the two objects have obstacles by ray
Unity and database
The first topic of ape Anthropology
2020-10-27
Lua determines whether the current time is the time of the day
被舆论盯上的蔚来,何时再次“起高楼”?
PR first time
Kali 2018 full image download
2022/7/2做题总结
十年不用一次的JVM调用
Understand encodefloatrgba and decodefloatrgba
Unity enables mobile phone vibration
Unity intelligent NPC production -- pre judgment walking (method 1)
嵌入式数据库开发编程(五)——DQL
54. Spiral matrix & 59 Spiral matrix II ●●
Animation
LeetCode之單詞搜索(回溯法求解)
Listview pull-down loading function
Detailed introduction of OSPF header message
Cocos progress bar progresstimer