Embedded Experiment 3 (the code almost needs to be changed to run)

5.1 The first step of transplanting the embedded database sqlite
is to decompress the sqlite source code, command tar xzvf sqlite-autoconf-3080900.tar.gz , in the uncompressed folder, you can see that the source code files include _____ sqlite3.c and shell. c file, generate the configuration script file configure of Makefile , and check the Makefile file in the current folder (A. exists B. does not exist ).

tar xzvf sqlite-autoconf-3080900.tar.gz

insert image description here

The second step is to use the configure script file to generate a Makefile based on the ARM test bench. The specific command is ./configure CC=arm-linux-gcc -prefix=/opt/sqlite -host=arm-linux
stepping on the pit: if the subsequent database operation Use the sqlite generated by compilation, do not add a strikethrough statement, that is, use gcc to compile by default (assuming the installation directory is /opt/sqlite), and check the Makefile file in the current folder (A. exists B. does not exist).

./configure -prefix=/opt/sqlite 

The third step is to compile sqlite, the command is make , and the compiler used in the compilation process is arm-linux-gcc
Note: If the strikethrough statement is not added, the compiler used is gcc .
The fourth step is to install sqlit, the command is make install . After the installation is complete, go to the **/opt/sqlite** folder to view the relevant files, you can see that there are ____ bin , include , lib _____ and share folders under this folder , and the executable file sqlite3 is located in the bin folder , the library is located in the lib folder.

make
make install

The sixth step, the use of the database
Method 1: command to manipulate the database (the sqlite used for this operation is the latest version sqlite3 installed by itself)
create the database stucomm.db in the super terminal environment, the command is sqlite3 stucomm.db ;
create a data table stutable, The fields include id integer, name character, phoneNum character, the specific command is create table stable(id integer, name text, phoneNum text);
insert 2 records, the record information is as follows
001, zhangsan, 10086
002, lisi, 10000
specific The command is insert into stable values(1,zhangsan,10086);
insert into stable values(2,lisi,10000);

to query the relevant record information of phone number 10086, the command is select * from stable where phoneNum=10086;
Remember to add the ; sign to the sqlite statement

If the previously installed sqlite3 cannot be used, you can install the latest sqlite3 according to the system prompts for experimentation:

sudo apt install sqlite3
sqlite3 stucomm.db
create table  stutable(id integer ,name text,phoneNum text);
insert into stutable values(1,zhangsan,10086);
insert into stutable values(2,lisi,10000);
select * from stutable where  phoneNum=10086;

insert image description here

Method 2: Programming to manipulate the database
Use the C programming mode to complete the above functions, the code is as follows:

#include "sqlite3.h"
#include <stddef.h>
int main(int argc, char** argv)
{
    
    
    sqlite3 * db = NULL;
    char* zErrMsg = NULL;
    int rc;
    sqlite3_open( ":memory:", &db );     //打开内存数据库
    rc = sqlite3_exec(db, "create table employee(id integer primary key, age integer);",NULL,0, &zErrMsg);//创建数据库
    for( int i= 0; i < 10; i++ )
    {
    
    
      char* statement = sqlite3_mprintf( "insert into employee values(%d, %d);", NULL, 0, &zErrMsg, rand()%65535, rand()%65535 );
	rc = sqlite3_exec( db, statement, NULL, 0 , 0  );//插入数据
      sqlite3_free( statement );
    }
    rc = sqlite3_exec(db, "select * from employee;",0, 0, &zErrMsg );//查询,并调用回调函数
    sqlite3_close(db);
}

Assume that the above code file is named sqlite_exp.c, and the compilation command is
gcc -o sqlite_exp sqlite_exp.c -L /opt/sqlite/lib -l sqlite3
Note: gcc is required on this machine, and -l is followed by the path of the header file , I copied it, if not, write the correct path /opt/sqlite/include, the library is located under /opt/sqlite/lib.

gcc -o sqlite_exp sqlite_exp.c -L /opt/sqlite/lib -l sqlite3
gcc -o sqlite_exp sqlite_exp.c -L /opt/sqlite/lib -l /opt/sqlite/include/sqlite3

insert image description here

Guess you like

Origin blog.csdn.net/m0_51928767/article/details/122048370