I need help with Unix. problem task2 help please.. stuck for 3 days.. thumb up (
ID: 3911613 • Letter: I
Question
I need help with Unix.
problem task2
help please.. stuck for 3 days.. thumb up
(cslinux:/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> Limport /etc/passwd pwtable sqlite> select from pwtable where user root, root.x:0:0 root:/root:/bin/bash sqlite> select from pwtable root:x:0:0:root:/root:/bin/bash binx1:1bin:/bin:/sbin/nologin daemon x:2:2:daemon:/sbin:/sbin/nologin sqlite>exit List your session log that you have done this part (in this document) and save it in a file (named "a2pltask1.txt") to be submitted in a zip file. Task #2 Your C program (a2pltask2.c) should connect to the database and should get all the entries in the pwtable (from Task# 1) and output each record in a file ("a2p1 task2.txt"). Each record should be formatted as shown below Record 1 user: root uid: 0 gid: 0 gecos: root home: /root shell: /bin/bashExplanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
static int callback(void data, int argc, char *argv, char **azColName){
int i;
fprintf(stderr, "%s: ", (const char*)data);
for(i = 0; i<argc; i++) {
printf("%s = %s ", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf(" ");
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("D://sqlite/passwd/password.db", &db);
if( rc ) {
fprintf(stderr, "Can't open database: %s ", sqlite3_errmsg(db));
return(0);
} else {
fprintf(stderr, "Opened database successfully ");
}
/ Create SQL statement /
sql = "SELECT * from password";
/ 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 ");
}
/ Create merged SQL statement /
sql = "UPDATE passowrd set comment = "My Apache Server for cs3376" where UID="48"; "
"SELECT * from COMPANY";
/ 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 ");
}
/ Create SQL statement /
sql = "SELECT * from password";
/ 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
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.