![]() |
|
[c] SQLite to XML - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: C, C++, & Obj-C (https://sinister.ly/Forum-C-C-Obj-C) +--- Thread: [c] SQLite to XML (/Thread-c-SQLite-to-XML) |
[c] SQLite to XML - titbang - 08-11-2017 This program dumps SQLite databases into XML files. Requires: boost, and sqlite3 Code: #include <iostream>
#include <vector>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <sqlite3.h>
using namespace std;
std::vector<std::string> options, tokens;
string buffer, out_buffer,
root_element,
attr_element, row;
sqlite3 *db;
bool dbg;
int item_count, rows;
static int callback( void *index, int argc, char **argv, char **column)
{
for( int i = 0; i < argc; i++ )
{
if( dbg ) {
printf("%s = %s\n", column[i], argv[i] ? argv[i] : "NULL");
}
buffer = argv[i] ? argv[i] : "NULL";
}
return 0;
}
int open( string filename )
{
if( sqlite3_open_v2( filename.c_str(), &db, SQLITE_OPEN_READONLY, NULL ) != SQLITE_OK ) {
sqlite3_close(db);
return 1;
}
return 0;
}
int exec( string statement )
{
char* err_msg=0;
if( sqlite3_exec(db, statement.c_str(), callback, 0, &err_msg) != SQLITE_OK) {
sqlite3_free(err_msg);
sqlite3_close(db);
return 1;
}
return 0;
}
int prepare()
{
for( int x = 1; x <= rows; x++ )
{
for( int y = 0; y != options.size(); y++ ) {
if( exec( "SELECT "+options[y]+" FROM "+root_element+" WHERE id=\'"+boost::lexical_cast<string>(x)+"\'") == 0 ) {
tokens.push_back( buffer );
} else {
return 1;
}
}
}
return 0;
}
int slice()
{
int y=0, c=1;
for(vector<int>::size_type i = 0; i != tokens.size(); i++ )
{
tokens[i] = boost::algorithm::replace_all_copy( tokens[i], "\"", "");
if( y == 0 ) {
out_buffer.append( "<" + attr_element + " id=\"" + boost::lexical_cast<std::string>( c ) + "\">\n" +
"<" + options[y] + ">" + tokens[i] + "</" + options[y] + ">\n" );
y++; c++;
} else if( y > 0 && y < item_count-1) {
out_buffer.append( "<" + options[y] + ">" + tokens[i] + "</" + options[y] + ">\n" );
y++;
} else {
out_buffer.append( "<" + options[y] + ">" + tokens[i] + "</" + options[y] + ">\n"
+ "</" + attr_element + ">\r\n\r\n" );
y=0;
}
}
return 0;
}
int output( string filename )
{
FILE* pFile = fopen( filename.c_str(), "w" );
if( pFile != NULL ) {
fwrite (out_buffer.c_str(), sizeof(char), out_buffer.size(), pFile);
fclose(pFile);
} else {
return 1;
}
return 0;
}
int cleanup()
{
if( db != NULL ) {
sqlite3_close( db );
}
options.clear();
tokens.clear();
}
int main(int argc, char* argv[])
{
if( argc > 5 )
{
root_element=argv[3];
attr_element=argv[4];
item_count = argc-5;
for( int x = 5; x <= argc-1; x++ ) {
options.push_back( argv[x] );
}
} else {
printf("sqlite2xml<: usage"
"<input_file>"
"<output_file>"
"<table / root_element>"
"<attr_element>"
"<element_names>\r\n");
return 1;
}
if( open( argv[1] ) != 0 ) {
printf("sqlite2xml<: unable to open file\r\n");
return 2;
}
if( exec( "SELECT COALESCE(MAX(id), 0) FROM " + root_element) == 0 )
{
rows = boost::lexical_cast<int>(buffer);
if( rows < 0 ) {
printf("sqlite2xml<: unable to select table\r\n");
return 3;
}
}
if( prepare( ) != 0 || tokens.size() < 0) {
printf( "sqlite2xml<: unable to select rows\r\n");
return 4;
} else {
slice();
}
if( output( argv[2] ) != 0 ) {
printf("sqlite2xml<: unable to save file\r\n");
return 5;
}
cleanup();
printf("sqlite2xml<: saved xml file to disk\r\n");
return 0;
} |