int main(int argc, char **argv) { dbString stmt; dbDriver *driver; dbHandle handle; int ret; FILE *fd; int error; error = 0; parse_command_line(argc, argv); if (strcmp(parms.input, "-")) { fd = fopen(parms.input, "r"); if (fd == NULL) { G_fatal_error(_("Unable to open file <%s> for reading.\n" "Details: %s"), parms.input, strerror(errno)); } } else { fd = stdin; } driver = db_start_driver(parms.driver); if (driver == NULL) { G_fatal_error(_("Unable to start driver <%s>"), parms.driver); } db_init_handle(&handle); db_set_handle(&handle, parms.database, parms.schema); if (db_open_database(driver, &handle) != DB_OK) G_fatal_error(_("Unable to open database <%s>"), parms.database); while (get_stmt(fd, &stmt)) { if (!stmt_is_empty(&stmt)) { G_debug(3, "sql: %s", db_get_string(&stmt)); ret = db_execute_immediate(driver, &stmt); if (ret != DB_OK) { if (parms.i) { /* ignore SQL errors */ G_warning(_("Error while executing: '%s'"), db_get_string(&stmt)); error++; } else G_fatal_error(_("Error while executing: '%s'"), db_get_string(&stmt)); } } } db_close_database(driver); db_shutdown_driver(driver); exit(error ? EXIT_FAILURE : EXIT_SUCCESS); }
void install_service_name_table() { sqlite3_stmt *stmt; struct serviceent *e; uuid_t uuid; int error ; stmt = get_stmt("INSERT INTO service_name (uuid, name) VALUES ($1 , $2) "); for(e = srvent; e->name != NULL; e++){ btuuid16(e->uuid, &uuid); my_bind_uuid(stmt, 1, &uuid); sqlite3_bind_text(stmt, 2, e->name, strlen(e->name), SQLITE_STATIC); error = sqlite3_step(stmt); sqlite3_reset(stmt); } sqlite3_finalize(stmt); }
void sqlite_source::prepare_data( const std::string & database_name, const std::string & sql) throw( std::logic_error, std::invalid_argument) { check_not_running(); delete_data(); sqlite3_stmt* data = get_stmt(database_name, sql); int column_count = sqlite3_column_count(data); if (column_count == 0) //statement is no SELECT statement { throw std::invalid_argument("Statement is no SQL SELECT statement."); } m_data = data; m_column_count = column_count; m_time_column = sqlite3_bind_parameter_index(m_data, get_duration_string().c_str()); m_database_name = database_name; m_sql = sql; std::vector<column_info> column_infos; sqlite3* db_handle = sqlite3_db_handle(m_data); char const* data_type = NULL; char const* coll_seq = NULL; int not_null = 0; int primary_key = 0; int auto_inc = 0; for (int i = 0; i < m_column_count; ++i) { std::string column_name = sqlite3_column_origin_name(m_data, i); int result_code = sqlite3_table_column_metadata( db_handle, sqlite3_column_database_name(m_data, i), sqlite3_column_table_name(m_data, i), column_name.c_str(), &data_type, &coll_seq, ¬_null, &primary_key, &auto_inc); if (result_code != SQLITE_OK) { //TODO: do something with the error...this error can only be an internal db error } if (i + 1 != m_time_column) { std::string type(data_type); column_info info(column_name, m_source_name, get_data_type(data_type)); column_infos.push_back(info); } } m_stream_schema = stream_schema_ptr(new stream_schema(m_source_name, column_infos)); }
int main(int argc, char **argv) { dbString stmt; dbDriver *driver; dbHandle handle; int stat; FILE *fd; parse_command_line(argc, argv); if (parms.table) { if (!db_table_exists(parms.driver, parms.database, parms.table)) { G_warning(_("Table <%s> not found in database <%s> using driver <%s>"), parms.table, parms.database, parms.driver); exit(EXIT_FAILURE); } } /* read from file or stdin ? */ if (parms.input && strcmp(parms.input, "-") != 0) { fd = fopen(parms.input, "r"); if (fd == NULL) { G_fatal_error(_("Unable to open file <%s>: %s"), parms.input, strerror(errno)); } } else fd = stdin; /* open DB connection */ db_init_string(&stmt); driver = db_start_driver(parms.driver); if (driver == NULL) { G_fatal_error(_("Unable to start driver <%s>"), parms.driver); } db_init_handle(&handle); db_set_handle(&handle, parms.database, NULL); if (db_open_database(driver, &handle) != DB_OK) G_fatal_error(_("Unable to open database <%s>"), parms.database); db_set_error_handler_driver(driver); /* check for sql, table, and input */ if (parms.sql) { /* parms.sql */ db_set_string(&stmt, parms.sql); stat = sel(driver, &stmt); } else if (parms.table) { /* parms.table */ db_set_string(&stmt, "SELECT * FROM "); db_append_string(&stmt, parms.table); stat = sel(driver, &stmt); } else { /* -> parms.input */ stat = DB_OK; while (stat == DB_OK && get_stmt(fd, &stmt)) { if (!stmt_is_empty(&stmt)) stat = sel(driver, &stmt); } } if (parms.test_only) G_verbose_message(_("Test %s."), stat ? _("failed") : _("succeeded")); db_close_database(driver); db_shutdown_driver(driver); exit(stat == DB_OK ? EXIT_SUCCESS : EXIT_FAILURE); }