PART1 C/C++ programming with sqlite3 (with Week05 Activity – sqlite3 example) Pr
ID: 3866863 • Letter: P
Question
PART1
C/C++ programming with sqlite3 (with Week05 Activity – sqlite3 example)
Project Description
Design and implement C/C++ program (a3part1.c [or a3part1.cpp]) to do the following tasks and make a Makefile to compile the program, and to run the program to show each task done.
The following commands (cat /etc/passwd and cat/etc/passwd | wc) show the part of the passwd file and the number of the line of the file: 58.
{cslinux1:~} cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
…
{cslinux1:~} cat /etc/passwd | wc
58 135 3266
Your program is to do the following tasks.
Tasks/Steps to do.
Task #1.
Create a sqlite3 database (called mypasswd) and create a table (passwd) to load the passwd information from /etc/passwd file as shown below.
{cslinux1:~/sqlite/passwd} sqlite3 mypasswd
SQLite version 3.14.1 2016-08-11 18:53:32
Enter ".help" for usage hints.
sqlite> create table pwtable (user, pass, uid, gid, gecos, home, shell);
sqlite> .separator :
sqlite> .import /etc/passwd pwtable
sqlite> select * from pwtable where name='root';
root:x:0:0:root:/root:/bin/bash
sqlite> select * from pwtable;
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
…
sqlite> .exit
Write a C/C++ program (mypasswd.c or mypasswd.cpp) to interact with sqlite3 database file (mypasswd) in Step1.
Task #2.
Your C program connect to the database and get all the entries in the pwtable and output each record in a file (a3p1task2.txt). Each record should be formatted as shown below.
user: root
uid: 0
gid: 0
gecos: root
home: /root
shell: /bin/bash
For the first record: root:x:0:0:root:/root:/bin/bash
Task #3
Your program does an update to the sqlite3 database (update pwtable set gecos="MySQL Server" where uid="27";)
Your program does (interacting with sqlite3 databas):
(1) get the record (of uid=27) and print the record with the headings shown in Task#2,
(2) update the record (of uid=27) so that the gecos will be "MySQL Server", then
(3) get the updated record using select, and
(4) print the record to the console with a heading (as shown in Task #2).
Task #4.
Finally your program will get all the records from the pwtable and output to a file (a3p1task4.txt). Each record should be formatted as shown in Task#2.
Task #5. Provide a Makefile file to compile your program.
Part1.
Place your answer here for (a) your program listing and (b) its runs, with a proper headings for each part, and then also upload (submit) this document and all your codes and run-log in a .zip file.
Part1. Your program code listing here.
Part1. Makefile listing
Part1. Your program runs for Tasks mentioned above.
Place here only the relevant run log only, and for each task with a proper heading.
Part1. Your program code listing here.
Explanation / Answer
Task #1:mypasswd.c
#include <sqlite3.h>
#include <stdio.h>
int callback(void *, int, char **, char **);
int main(void) {
sqlite3 *db;
char *err_msg = 0;
int rc = sqlite3_open("mypasswd.db", &db);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot open database: %s ",
sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
char *sql = "SELECT name FROM sqlite_master WHERE type='table'";
rc = sqlite3_exec(db, sql, callback, 0, &err_msg);
if (rc != SQLITE_OK ) {
fprintf(stderr, "Failed to select data ");
fprintf(stderr, "SQL error: %s ", err_msg);
sqlite3_free(err_msg);
sqlite3_close(db);
return 1;
}
sqlite3_close(db);
return 0;
}
int callback(void *NotUsed, int argc, char **argv,
char **azColName) {
NotUsed = 0;
for (int i = 0; i < argc; i++) {
printf("%s ", argv[i] ? argv[i] : "NULL");
}
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------------------
Task #2:mypasswd.c
#include <sqlite3.h>
#include <stdio.h>
int callback(void *, int, char **, char **);
int main(void) {
sqlite3 *db;
char *err_msg = 0;
int rc = sqlite3_open("mypasswd.db", &db);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot open database: %s ",
sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
char *sql = "SELECT * FROM pwtable";
rc = sqlite3_exec(db, sql, callback, 0, &err_msg);
if (rc != SQLITE_OK ) {
fprintf(stderr, "Failed to select data ");
fprintf(stderr, "SQL error: %s ", err_msg);
sqlite3_free(err_msg);
sqlite3_close(db);
return 1;
}
sqlite3_close(db);
return 0;
}
int callback(void *NotUsed, int argc, char **argv,
char **azColName) {
NotUsed = 0;
FILE *ptr_file;
ptr_file =fopen("a3p1task2.txt", "w");
if (!ptr_file)
return 1;
for (int i = 0; i < argc; i++) {
fprintf("%s:%s ", azColName[i],argv[i]);
}
fprintf(" ");
fclose(ptr_file);
return 0;
}
----------------------------------------------------------------------------------------------------------------------------------------------------
Task #3
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
static int callback(void *data, int argc, char **argv, char **azColName){
FILE *ptr_file;
ptr_file =fopen("a3p1task2.txt", "w");
if (!ptr_file)
return 1;
for (int i = 0; i < argc; i++) {
fprintf("%s:%s ", azColName[i],argv[i]);
}
fprintf(" ");
fclose(ptr_file);
return 0;
}
int main(int argc, char* argv[]) {
sqlite3 *db;
char *zErrMsg = 0;
int rc;
char *sql;
const char* data = "Callback function called";
/* Open database */
rc = sqlite3_open("mypasswd.db", &db);
if( rc ) {
fprintf(stderr, "Can't open database: %s ", sqlite3_errmsg(db));
return(0);
} else {
fprintf(stderr, "Opened database successfully ");
}
/* Create merged SQL statement */
sql = "update pwtable set gecos='MySQL Server' where uid='27'; "
"SELECT * from pwtable";
/* Execute SQL statement */
rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);
if( rc != SQLITE_OK ) {
fprintf(stderr, "SQL error: %s ", zErrMsg);
sqlite3_free(zErrMsg);
} else {
fprintf(stdout, "Operation done successfully ");
}
sqlite3_close(db);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.