当前位置:网站首页>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;
}
边栏推荐
- China needle coke industry development research and investment value report (2022 Edition)
- 被舆论盯上的蔚来,何时再次“起高楼”?
- "Measuring curve length" of CAD dream drawing
- Unity parallax infinite scrolling background
- mysql審計日志歸檔
- Create a pyGame window with a blue background
- The first topic of ape Anthropology
- MySQL audit log Archive
- 2020-10-27
- Time format conversion
猜你喜欢
随机推荐
2022 / 7 / 1 Résumé de l'étude
Simple HelloWorld color change
用 Jmeter 工具做个小型压力测试
On-off and on-off of quality system construction
嵌入式数据库开发编程(零)
AutoCAD - Center zoom
2022/7/1 learning summary
AutoCAD - feature matching
How much do you know about 3DMAX rendering skills and HDRI light sources? Dry goods sharing
Grail layout and double wing layout
AutoCAD - graphic input and output
"Measuring curve length" of CAD dream drawing
Pause and resume of cocos2dx Lua scenario
2022 thinking of mathematical modeling C problem of American college students / analysis of 2022 American competition C problem
Number theoretic function and its summation to be updated
Unity get component
Database under unity
数论函数及其求和 待更新
Use of snippets in vscode (code template)
Unity3d learning notes