←Index
by Professor Petabyte
This lets you run 'sqlite3' from any command prompt window.
sqlite3
SQLite version 3.x.x 202x-xx-xx Enter ".help" for usage hints.
#include#include // SQLite library header // Callback function to handle query results static int callback(void* NotUsed, int argc, char** argv, char** azColName) { for (int i = 0; i < argc; i++) { std::cout << azColName[i] << " = " << (argv[i] ? argv[i] : "NULL") << "\n"; } std::cout << "-------------------\n"; return 0; } int main() { sqlite3* DB; char* errorMessage; // 1. Open database (creates file if it doesn't exist) int exit = sqlite3_open("example.db", &DB); if (exit) { std::cerr << "Error opening DB: " << sqlite3_errmsg(DB) << std::endl; return -1; } std::cout << "Opened database successfully!\n"; // 2. Create a table std::string createTable = "CREATE TABLE IF NOT EXISTS users(" "id INTEGER PRIMARY KEY AUTOINCREMENT," "name TEXT NOT NULL," "age INT NOT NULL);"; exit = sqlite3_exec(DB, createTable.c_str(), NULL, 0, &errorMessage); if (exit != SQLITE_OK) { std::cerr << "Error creating table: " << errorMessage << std::endl; sqlite3_free(errorMessage); } // 3.Insert sample data std::string insertData = "INSERT INTO users (name, age) VALUES('Alice', 30);" "INSERT INTO users (name, age) VALUES('Bob', 25);"; exit = sqlite3_exec(DB, insertData.c_str(), NULL, 0, &errorMessage); if (exit != SQLITE_OK) { std::cerr << "Error inserting data: " << errorMessage << std::endl; sqlite3_free(errorMessage); } // 4.Read & output the data std::string query = "SELECT * FROM users;"; std::cout << "\nQuery Results:\n"; exit = sqlite3_exec(DB, query.c_str(), callback, NULL, &errorMessage); if (exit != SQLITE_OK) { std::cerr << "Error selecting data: " << errorMessage << std::endl; sqlite3_free(errorMessage); } sqlite3_close(DB); return 0; }
Opened database successfully! Query Results: id = 1 name = Alice age = 30 ------------------- id = 2 name = Bob age = 25 -------------------
SQLite is an effective database for 'small' amounts of data, upto a few thousand records. The maximum theoretical size for an SQLite database is approximately 281 terabytes (TB), however practical limitations usually come into play before reaching this theoretical maximum:
It is if nothing else an excellent free tool to learn about basic SQL queries. It may not have all of the 'bells and whistles' that a full SQL database such as SQL-Server has, but it is nonetheless very capable for small projects and learning SQL.