First, go to the Oracle official website to download the Berkeleydb database source file unzip (of course you can also use some visualization tools to decompress), the steps are as follows in the command line Input tar-zxvfdb-5.3.15.t
1. First download the source file of the Berkeley db database from the Oracle official website.
Http://download.oracle.com/otn/berkeley-db/db-5.3.15.tar.gz
2. The downloaded file is a packaged file and needs to be decompressed using tar in the command line (of course you can use some visualization tools to decompress it). The steps are as follows:
Enter tar-zxvf db-5.3.15.tar.gz in the command line
Decompress the package and enter the db-5.3.15 directory with the following files and folders
Enter the build_unix directory
Cd build_unix
Then run ../dist/configure
The configure tool checks the environment and generates the files required to compile the program. After the tool runs successfully, enter
Make
After compilation, you need to run "make install (root permission required)". Then, the library files and required development files will be installed in your system.
The installed files are stored in the/usr/local/BerkeleyDB.5.3 directory of the system by default. To use the files in the programming environment, you need to use them in/etc/ld. so. add/usr/local/BerkeleyDB.5.3/lib to the conf file so that your program can find the library file correctly and finally run the ldconfig command to update your system. Now you can write the database code.
- # Include
- # Include
- # Include
- # Define DATABASE "employees. db"
- IntMain ()
- {
- DBT key, data;
- DB * dbp;
- IntRet;
- StructData_struct {
- IntEmpid;
- CharLastname [50];
- CharFirstname [50];
- FloatSalary;
- } Emp;
- Ret = db_create (& dbp, NULL, 0 );
- If(Ret! = 0)
- {
- Perror ("Create");
- Return1;
- }
- Ret = dbp-> open (dbp, NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0 );
- If(Ret! = 0)
- {
- Perror ("Open :");
- Return1;
- }
- While(1)
- {
- Printf ("Enter Employee ID :");
- Scanf ("% D", & Emp. empid );
- If(Emp. empid = 0)
- Break;
- Printf ("Enter Last name :");
- Scanf ("% S", & Amp; emp. lastname );
- Printf ("Enter First name :");
- Scanf ("% S", & Emp. firstname );
- Printf ("Enter Salary :");
- Scanf ("% F", & Emp. salary );
- Memset (& key, 0,Sizeof(DBT ));
- Memset (& data, 0,Sizeof(DBT ));
- Key. data = & (emp. empid );
- Key. size =Sizeof(Emp. empid );
- Data. data = & emp;
- Data. size =Sizeof(Emp );
- Ret = dbp-> put (dbp, NULL, & key, & data, DB_NOOVERWRITE );
- If(Ret! = 0)
- {
- Printf ("Employee ID exists \ n");
- }
- }
- Dbp-> close (dbp, 0 );
- Return0;
- }
Compile code
Gcc-I/usr/local/BerkeleyDB.5.3/include-o newemployee. c-L/usr/local/BerkeleyDB.5.3-ldb
According to the above compilation, an error will occur.
/Usr/bin/ld: cannot find-ldb
Collect2: ld returns 1
Enter the following language in the command line to solve the problem.
Ln-s/usr/local/BerkeleyDB.5.3/lib/libdb. so/usr/lib/libdb. so
Recompile
Gcc-I/usr/local/BerkeleyDB.5.3/include-o newemployee. c-L/usr/local/BerkeleyDB.5.3-ldb
Now, the code is compiled successfully.