Created
November 18, 2014 22:57
-
-
Save kolodziej/17e1b4795e221cd09afc to your computer and use it in GitHub Desktop.
simple sqlite client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <sqlite3.h> | |
#include <string> | |
int callback(void *nu, int argc, char **argv, char **colName) | |
{ | |
for (int i = 0; i < argc; ++i) | |
{ | |
std::cout << colName[i] << " = "; | |
if (argv[i]) | |
{ | |
std::cout << argv[i]; | |
} else | |
{ | |
std::cout << "NULL"; | |
} | |
std::cout << "\n"; | |
} | |
std::cout << "\n"; | |
return 0; | |
} | |
int main(int argc, char **argv) | |
{ | |
if (argc < 2) | |
{ | |
std::cerr << "Usage: " << argv[0] << " [dbname]" << std::endl; | |
return 1; | |
} | |
std::string prompt = "> "; | |
if (argc == 3) | |
{ | |
prompt = argv[2]; | |
} | |
sqlite3 *database = nullptr; | |
char *sqlite3Error = nullptr; | |
if (sqlite3_open(argv[1], &database) == 0) | |
{ | |
std::cout << "Database " << argv[1] << " opened!\n"; | |
} else | |
{ | |
std::cerr << "Could not open databse " << argv[1] << "\n"; | |
} | |
std::string sql; | |
std::cout << prompt; | |
while (std::getline(std::cin, sql, ';')) | |
{ | |
if (sqlite3_exec(database, sql.data(), callback, 0, &sqlite3Error) == SQLITE_OK) | |
{ | |
std::cout << "OK!\n"; | |
} else | |
{ | |
std::cerr << "Error: " << sqlite3Error << "\n"; | |
} | |
std::cout << prompt; | |
} | |
sqlite3_close(database); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment