Sure! Here's an example of a simple console application that uses SQLite to manage students' marks and data. First, make sure you've installed SQLite in your C++ development environment.
#include <sqlite3.h>
#include <iostream>
using namespace std;
static int callback(void *data, int argc, char **argv, char **azColName){
int i;
for(i = 0; i<argc; i++) {
cout << azColName[i] << ": " << (argv[i] ? argv[i] : "NULL") << "\n";
}
cout << "\n";
return 0;
}
int main() {
sqlite3 *db;
char *zErrMsg = 0;
int rc;
const char *sql;
// Open database
rc = sqlite3_open("test.db", &db);
if(rc) {
cout << "Can't open database: " << sqlite3_errmsg(db) << "\n";
return(0);
} else {
cout << "Opened database successfully\n";
}
// Create SQL statement
sql = "CREATE TABLE STUDENTS(" \
"ID INT PRIMARY KEY NOT NULL," \
"NAME TEXT NOT NULL," \
"MARKS INT NOT NULL);";
// Execute SQL statement
rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
if(rc != SQLITE_OK) {
cout << "SQL error: " << zErrMsg << "\n";
sqlite3_free(zErrMsg);
} else {
cout << "Table created successfully\n";
}
// Insert data
sql = "INSERT INTO STUDENTS (ID,NAME,MARKS) " \
"VALUES (1, 'Paul', 85); " \
"INSERT INTO STUDENTS (ID,NAME,MARKS) " \
"VALUES (2, 'Allen', 70); " \
"INSERT INTO STUDENTS (ID,NAME,MARKS) " \
"VALUES (3, 'Teddy', 82); " \
"INSERT INTO STUDENTS (ID,NAME,MARKS) " \
"VALUES (4, 'Mark', 98);";
// Execute SQL statement
rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
if(rc != SQLITE_OK) {
cout << "SQL error: " << zErrMsg << "\n";
sqlite3_free(zErrMsg);
} else {
cout << "Records created successfully\n";
}
// Close database
sqlite3_close(db);
return 0;
}
In this code:
- We first open a connection to the SQLite database.
- We then create a table called "STUDENTS" with columns for the student ID, their name, and their marks.
- We then insert some sample data into the table.
- Finally, we close the connection to the database.
This is a very basic example. Depending on your requirements, you might want to add more functionality, such as the ability for users to add, update, or delete records, or to query the database for specific records.