Java factory social recruitment interview, [embedded experiment]

  • 5, Experimental steps

  • Experiment 1: transplant embedded database sqlite

    • 1 ~ 5: install sqlite database

    • 6: Use of database

      • Method 1: command to manipulate the database

      • Mode 2: program to manipulate the database

    • Outside the experiment (you can see if you are interested)

  • Experiment 2: embedded Qt experiment

  • 1, QT installation

More notes can be viewed Directory of basic notes for computer major

1, Experimental purpose

=========================================================================

Understand the basic methods of embedded software transplantation, master the steps of sqlite database software transplantation, master the use methods of two ways of sqlite development - command mode and C code development mode, and program to realize the simple address book query experiment.

2, Experimental content

=========================================================================

Experiment 4.1 porting embedded database sqlite

Experiment 4.2 example design and test of simple address book query

3, Preparatory knowledge

=========================================================================

Linux usage, database related knowledge, etc

4, Experimental equipment and tools (including software debugging tools)

======================================================================================

Hardware: ARM embedded development platform, PC Pentium100 or above, serial port cable.

Software: WinXP or UBUNTU development environment. (DeepIn I use)

5, Experimental steps

=========================================================================

Experiment 1: transplant embedded database sqlite

======================================================================================

1 ~ 5: install sqlite database

The first step is to extract the SQLite source code and command tar xvzf sqlite-autoconf-3080900 tar. gz;

In the unzipped folder, you can see that the source files include (shell.c) and (sql3.c) files;

Generate the configuration script file of Makefile/ configure --prefix=/opt/sqlite ,

And check the Makefile file in the current folder. (you can take a closer look at the previous figure, which was not available at that time.)

The second step is to use the configure script file to generate the Makefile based on the ARM test bench. The specific command is/ Configure CC = ARM linux GCC – prefix=/opt/sqlite – host = ARM linux, and check the Makefile file in the current folder.

The third step is to compile sqlite, the command is make, and the compiler used in the compilation process is arm linux GCC.

Step 4: install sqlit. The command is make insatll. After installation, 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. The executable file sqlite3 is located in the (. / bin) folder and the library is located in the (. / lib) folder.

Step 5: copy sqlite3 to the bin directory of the development board, and copy the files under the library to the lib directory of the development board [pay attention to the creation of link files]

6: Use of database

Method 1: command to manipulate the database

Create the database stucomm DB, the command is/ sqilte3 stucomm.db;

(if the environment variable is not configured, enter the command in the / opt/sqlite/bin directory)

Create a data table stutable. The fields include id integer, name character and phoneNum character. The specific commands are:

create table stutable(id integer, name text, phoneNum text); 

Insert two records with the following information (001, zhangsan, 10086), (002, lisi, 10000). The specific commands are:

insert into stutable values(1, 'zhangsan', 10086);

insert into stutable values(2, 'lisi', 10000); 

To query the relevant record information with telephone number 10086, the command is:

select * from stutable where phoneNum = 10086; 

Mode 2: program to manipulate the database

Use C programming mode to complete the above functions, and the code is as follows:

#include<stdio.h>

#include"sqlite/sqlite3.h"



/*

(1)Open database:

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

- filename: Name of database file to be opened (created)

- ppDb: sqlite3 Pointer to the database handle



(2)Operation database: sqlite can provide an interface, and SQL operation statements can be directly embedded into sqlite functions for execution.

int  sqlite3_exec(sqlite3* ppDb, const char *sql, 

    int(*callback)(void*, int, char**, char**), void*, char **errmsg);

- sqlite3* Is the pointer from the open function

- constchar*sql Is an sql statement ending with \ 0

- sqlite3_callback It is a callback. After this statement is executed, sqlite3 will call this function

- void*Is the pointer provided by the caller. The caller can pass any pointer parameter here,

        This parameter will eventually be passed to the callback function. If you do not need to pass a pointer to the callback function, you can fill in NULL

- char** errmsg This is an error message



(3)Close database: the database needs to be closed when it is not in use.

int sqlite3_close(sqlite3 *ppDb); 

*/



// Callback function

int callback(void *, int, char**, char**);

// Check whether the database operation is performed normally

int check_opt(sqlite3 *db, int nResult, char* errmsg) {

    sqlite3_close(db); // close database

    printf("%s", errmsg);

    sqlite3_free(errmsg);

    return 0;

}



int main() {

    sqlite3 * db;

    // Open memory database

    int nResult = sqlite3_open(":memory:", &db);

    if (nResult != SQLITE_OK) {

        printf("Failed to open database\n");

        return 0;

    } else {

        printf("Database opened successfully\n");

    }



    // error message

    char* errmsg;

    nResult = sqlite3_exc(db, "create table stutable(id integer, name text, phoneNum text);",

                            NULL, NULL, &errmsg);



    check_opt(db, nResult, &errmsg); // Check whether the operation is performed normally

    

    nResult = sqlite3_exc(db, "insert into stutable values(1, 'zhangsan', 10086);",

                            NULL, NULL, &errmsg);

    check_opt(db, nResult, &errmsg); // Check whether the operation is performed normally



    nResult = sqlite3_exc(db, "insert into stutable values(1, 'zhangsan', 10086);",

                            NULL, NULL, &errmsg);

    check_opt(db, nResult, &errmsg); // Check whether the operation is performed normally



    nResult = sqlite3_exec(db, "select * from stutable where phoneNum = 10086;",

                            callback, NULL, &errmsg);

    check_opt(db, nResult, &errmsg); // Check whether the operation is performed normally



    int callback(void*, int nCount, char** pValue, cahr** pName) {

        for (int i = 0; i < nCount; i++) {

            printf("[%s] = [%s]\n", pName[i], pValue[i]);

        }

        return 0;

    }





Keywords: Java Database SQLite Back-end

Added by riespies on Fri, 17 Dec 2021 19:42:26 +0200