Connect Ruby and MySQL - mysql

trying to connect ruby and mysql. When running a basic script to access the database getting the error:
Unable to load driver 'Mysql' (underlying error: uninitialized constant DBI::DBD::Mysql) (DBI::InterfaceError)
Googled it and found solution:
gem install mysql
gem install dbd-mysql
First one, gives error:
checking for mysql_ssl_set()... yes
checking for rb_str_set_len()... yes
checking for rb_thread_start_timer()... no
checking for mysql.h... yes
creating Makefile
current directory: /home/wheatman/.rvm/gems/ruby-2.7.2/gems/mysql-2.9.1/ext/mysql_api
make "DESTDIR=" clean
current directory: /home/wheatman/.rvm/gems/ruby-2.7.2/gems/mysql-2.9.1/ext/mysql_api
make "DESTDIR="
compiling mysql.c
mysql.c:79:2: error: unknown type name ‘my_bool’
79 | my_bool *is_null;
| ^~~~~~~
mysql.c: In function ‘options’:
mysql.c:361:5: error: unknown type name ‘my_bool’; did you mean ‘bool’?
361 | my_bool b;
| ^~~~~~~
| bool
mysql.c:391:10: error: ‘MYSQL_SET_CLIENT_IP’ undeclared (first use in this function); did you mean ‘MYSQL_SET_CHARSET_DIR’?
391 | case MYSQL_SET_CLIENT_IP:
| ^~~~~~~~~~~~~~~~~~~
| MYSQL_SET_CHARSET_DIR
mysql.c:391:10: note: each undeclared identifier is reported only once for each function it appears in
mysql.c:398:10: error: ‘MYSQL_SECURE_AUTH’ undeclared (first use in this function); did you mean ‘MYSQL_DEFAULT_AUTH’?
398 | case MYSQL_SECURE_AUTH:
| ^~~~~~~~~~~~~~~~~
| MYSQL_DEFAULT_AUTH
mysql.c: In function ‘stmt_init’:
mysql.c:878:5: error: ‘my_bool’ undeclared (first use in this function)
878 | my_bool true = 1;
| ^~~~~~~
mysql.c:878:12: error: expected ‘;’ before numeric constant
878 | my_bool true = 1;
| ^
| ;
mysql.c:883:61: error: lvalue required as unary ‘&’ operand
883 | if (mysql_stmt_attr_set(s, STMT_ATTR_UPDATE_MAX_LENGTH, &true))
| ^
mysql.c: In function ‘stmt_bind_result’:
mysql.c:1320:74: error: ‘rb_cFixnum’ undeclared (first use in this function)
1320 | else if (argv[i] == rb_cNumeric || argv[i] == rb_cInteger || argv[i] == rb_cFixnum)
| ^~~~~~~~~~
mysql.c: In function ‘stmt_prepare’:
mysql.c:1681:32: warning: assignment to ‘_Bool *’ from incompatible pointer type ‘int *’ [-Wincompatible-pointer-types]
1681 | s->result.bind[i].is_null = &(s->result.is_null[i]);
| ^
In file included from /home/wheatman/.rvm/rubies/ruby-2.7.2/include/ruby-2.7.0/ruby.h:33,
from mysql.c:5:
mysql.c: In function ‘Init_mysql_api’:
mysql.c:2049:52: error: ‘MYSQL_SECURE_AUTH’ undeclared (first use in this function); did you mean ‘MYSQL_DEFAULT_AUTH’?
2049 | rb_define_const(cMysql, "SECURE_AUTH", INT2NUM(MYSQL_SECURE_AUTH));
| ^~~~~~~~~~~~~~~~~
/home/wheatman/.rvm/rubies/ruby-2.7.2/include/ruby-2.7.0/ruby/ruby.h:262:33: note: in definition of macro ‘RB_INT2FIX’
262 | #define RB_INT2FIX(i) (((VALUE)(i))<<1 | RUBY_FIXNUM_FLAG)
| ^
/home/wheatman/.rvm/rubies/ruby-2.7.2/include/ruby-2.7.0/ruby/ruby.h:1609:20: note: in expansion of macro ‘RB_INT2NUM’
1609 | #define INT2NUM(x) RB_INT2NUM(x)
| ^~~~~~~~~~
mysql.c:2049:44: note: in expansion of macro ‘INT2NUM’
2049 | rb_define_const(cMysql, "SECURE_AUTH", INT2NUM(MYSQL_SECURE_AUTH));
| ^~~~~~~
mysql.c:2050:61: error: ‘MYSQL_OPT_GUESS_CONNECTION’ undeclared (first use in this function); did you mean ‘MYSQL_OPT_RECONNECT’?
2050 | rb_define_const(cMysql, "OPT_GUESS_CONNECTION", INT2NUM(MYSQL_OPT_GUESS_CONNECTION));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
/home/wheatman/.rvm/rubies/ruby-2.7.2/include/ruby-2.7.0/ruby/ruby.h:262:33: note: in definition of macro ‘RB_INT2FIX’
262 | #define RB_INT2FIX(i) (((VALUE)(i))<<1 | RUBY_FIXNUM_FLAG)
| ^
/home/wheatman/.rvm/rubies/ruby-2.7.2/include/ruby-2.7.0/ruby/ruby.h:1609:20: note: in expansion of macro ‘RB_INT2NUM’
1609 | #define INT2NUM(x) RB_INT2NUM(x)
| ^~~~~~~~~~
mysql.c:2050:53: note: in expansion of macro ‘INT2NUM’
2050 | rb_define_const(cMysql, "OPT_GUESS_CONNECTION", INT2NUM(MYSQL_OPT_GUESS_CONNECTION));
| ^~~~~~~
mysql.c:2051:68: error: ‘MYSQL_OPT_USE_EMBEDDED_CONNECTION’ undeclared (first use in this function)
2051 | rb_define_const(cMysql, "OPT_USE_EMBEDDED_CONNECTION", INT2NUM(MYSQL_OPT_USE_EMBEDDED_CONNECTION));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/wheatman/.rvm/rubies/ruby-2.7.2/include/ruby-2.7.0/ruby/ruby.h:262:33: note: in definition of macro ‘RB_INT2FIX’
262 | #define RB_INT2FIX(i) (((VALUE)(i))<<1 | RUBY_FIXNUM_FLAG)
| ^
/home/wheatman/.rvm/rubies/ruby-2.7.2/include/ruby-2.7.0/ruby/ruby.h:1609:20: note: in expansion of macro ‘RB_INT2NUM’
1609 | #define INT2NUM(x) RB_INT2NUM(x)
| ^~~~~~~~~~
mysql.c:2051:60: note: in expansion of macro ‘INT2NUM’
2051 | rb_define_const(cMysql, "OPT_USE_EMBEDDED_CONNECTION", INT2NUM(MYSQL_OPT_USE_EMBEDDED_CONNECTION));
| ^~~~~~~
mysql.c:2052:66: error: ‘MYSQL_OPT_USE_REMOTE_CONNECTION’ undeclared (first use in this function)
2052 | rb_define_const(cMysql, "OPT_USE_REMOTE_CONNECTION", INT2NUM(MYSQL_OPT_USE_REMOTE_CONNECTION));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/wheatman/.rvm/rubies/ruby-2.7.2/include/ruby-2.7.0/ruby/ruby.h:262:33: note: in definition of macro ‘RB_INT2FIX’
262 | #define RB_INT2FIX(i) (((VALUE)(i))<<1 | RUBY_FIXNUM_FLAG)
| ^
/home/wheatman/.rvm/rubies/ruby-2.7.2/include/ruby-2.7.0/ruby/ruby.h:1609:20: note: in expansion of macro ‘RB_INT2NUM’
1609 | #define INT2NUM(x) RB_INT2NUM(x)
| ^~~~~~~~~~
mysql.c:2052:58: note: in expansion of macro ‘INT2NUM’
2052 | rb_define_const(cMysql, "OPT_USE_REMOTE_CONNECTION", INT2NUM(MYSQL_OPT_USE_REMOTE_CONNECTION));
| ^~~~~~~
mysql.c:2053:54: error: ‘MYSQL_SET_CLIENT_IP’ undeclared (first use in this function); did you mean ‘MYSQL_SET_CHARSET_DIR’?
2053 | rb_define_const(cMysql, "SET_CLIENT_IP", INT2NUM(MYSQL_SET_CLIENT_IP));
| ^~~~~~~~~~~~~~~~~~~
/home/wheatman/.rvm/rubies/ruby-2.7.2/include/ruby-2.7.0/ruby/ruby.h:262:33: note: in definition of macro ‘RB_INT2FIX’
262 | #define RB_INT2FIX(i) (((VALUE)(i))<<1 | RUBY_FIXNUM_FLAG)
| ^
/home/wheatman/.rvm/rubies/ruby-2.7.2/include/ruby-2.7.0/ruby/ruby.h:1609:20: note: in expansion of macro ‘RB_INT2NUM’
1609 | #define INT2NUM(x) RB_INT2NUM(x)
| ^~~~~~~~~~
mysql.c:2053:46: note: in expansion of macro ‘INT2NUM’
2053 | rb_define_const(cMysql, "SET_CLIENT_IP", INT2NUM(MYSQL_SET_CLIENT_IP));
| ^~~~~~~
error_const.h:2661:27: error: ‘ER_XPLUGIN_IP’ undeclared (first use in this function); did you mean ‘ER_PLUGIN_OOM’?
2661 | rb_define_mysql_const(ER_XPLUGIN_IP);
| ^~~~~~~~~~~~~
/home/wheatman/.rvm/rubies/ruby-2.7.2/include/ruby-2.7.0/ruby/ruby.h:262:33: note: in definition of macro ‘RB_INT2FIX’
262 | #define RB_INT2FIX(i) (((VALUE)(i))<<1 | RUBY_FIXNUM_FLAG)
| ^
/home/wheatman/.rvm/rubies/ruby-2.7.2/include/ruby-2.7.0/ruby/ruby.h:1609:20: note: in expansion of macro ‘RB_INT2NUM’
1609 | #define INT2NUM(x) RB_INT2NUM(x)
| ^~~~~~~~~~
mysql.c:2286:62: note: in expansion of macro ‘INT2NUM’
2286 | #define rb_define_mysql_const(s) rb_define_const(eMysql, #s, INT2NUM(s))
| ^~~~~~~
error_const.h:2661:5: note: in expansion of macro ‘rb_define_mysql_const’
2661 | rb_define_mysql_const(ER_XPLUGIN_IP);
| ^~~~~~~~~~~~~~~~~~~~~
mysql.c: At top level:
cc1: note: unrecognized command-line option ‘-Wno-self-assign’ may have been intended to silence earlier diagnostics
cc1: note: unrecognized command-line option ‘-Wno-parentheses-equality’ may have been intended to silence earlier diagnostics
cc1: note: unrecognized command-line option ‘-Wno-constant-logical-operand’ may have been intended to silence earlier diagnostics
make: *** [Makefile:245: mysql.o] Error 1
make failed, exit code 2
Gem files will remain installed in /home/wheatman/.rvm/gems/ruby-2.7.2/gems/mysql-2.9.1 for inspection.
Results logged to /home/wheatman/.rvm/gems/ruby-2.7.2/extensions/x86_64-linux/2.7.0/mysql-2.9.1/gem_make.out
And the second one gives another error:
Building native extensions. This could take a while...
ERROR: Error installing dbd-mysql:
ERROR: Failed to build gem native extension.
current directory: /home/wheatman/.rvm/gems/ruby-2.7.2/gems/mysql-2.9.1/ext/mysql_api
/home/wheatman/.rvm/rubies/ruby-2.7.2/bin/ruby -I /home/wheatman/.rvm/rubies/ruby-2.7.2/lib/ruby/2.7.0 -r ./siteconf20201219-250830-klyeov.rb extconf.rb
checking for mysql_ssl_set()... yes
checking for rb_str_set_len()... yes
checking for rb_thread_start_timer()... no
checking for mysql.h... yes
creating Makefile
current directory: /home/wheatman/.rvm/gems/ruby-2.7.2/gems/mysql-2.9.1/ext/mysql_api
make "DESTDIR=" clean
current directory: /home/wheatman/.rvm/gems/ruby-2.7.2/gems/mysql-2.9.1/ext/mysql_api
make "DESTDIR="
compiling mysql.c
mysql.c:79:2: error: unknown type name ‘my_bool’
79 | my_bool *is_null;
| ^~~~~~~
mysql.c: In function ‘options’:
mysql.c:361:5: error: unknown type name ‘my_bool’; did you mean ‘bool’?
361 | my_bool b;
| ^~~~~~~
| bool
mysql.c:391:10: error: ‘MYSQL_SET_CLIENT_IP’ undeclared (first use in this function); did you mean ‘MYSQL_SET_CHARSET_DIR’?
391 | case MYSQL_SET_CLIENT_IP:
| ^~~~~~~~~~~~~~~~~~~
| MYSQL_SET_CHARSET_DIR
mysql.c:391:10: note: each undeclared identifier is reported only once for each function it appears in
mysql.c:398:10: error: ‘MYSQL_SECURE_AUTH’ undeclared (first use in this function); did you mean ‘MYSQL_DEFAULT_AUTH’?
398 | case MYSQL_SECURE_AUTH:
| ^~~~~~~~~~~~~~~~~
| MYSQL_DEFAULT_AUTH
mysql.c: In function ‘stmt_init’:
mysql.c:878:5: error: ‘my_bool’ undeclared (first use in this function)
878 | my_bool true = 1;
| ^~~~~~~
mysql.c:878:12: error: expected ‘;’ before numeric constant
878 | my_bool true = 1;
| ^
| ;
mysql.c:883:61: error: lvalue required as unary ‘&’ operand
883 | if (mysql_stmt_attr_set(s, STMT_ATTR_UPDATE_MAX_LENGTH, &true))
| ^
mysql.c: In function ‘stmt_bind_result’:
mysql.c:1320:74: error: ‘rb_cFixnum’ undeclared (first use in this function)
1320 | else if (argv[i] == rb_cNumeric || argv[i] == rb_cInteger || argv[i] == rb_cFixnum)
| ^~~~~~~~~~
mysql.c: In function ‘stmt_prepare’:
mysql.c:1681:32: warning: assignment to ‘_Bool *’ from incompatible pointer type ‘int *’ [-Wincompatible-pointer-types]
1681 | s->result.bind[i].is_null = &(s->result.is_null[i]);
| ^
In file included from /home/wheatman/.rvm/rubies/ruby-2.7.2/include/ruby-2.7.0/ruby.h:33,
from mysql.c:5:
mysql.c: In function ‘Init_mysql_api’:
mysql.c:2049:52: error: ‘MYSQL_SECURE_AUTH’ undeclared (first use in this function); did you mean ‘MYSQL_DEFAULT_AUTH’?
2049 | rb_define_const(cMysql, "SECURE_AUTH", INT2NUM(MYSQL_SECURE_AUTH));
| ^~~~~~~~~~~~~~~~~
/home/wheatman/.rvm/rubies/ruby-2.7.2/include/ruby-2.7.0/ruby/ruby.h:262:33: note: in definition of macro ‘RB_INT2FIX’
262 | #define RB_INT2FIX(i) (((VALUE)(i))<<1 | RUBY_FIXNUM_FLAG)
| ^
/home/wheatman/.rvm/rubies/ruby-2.7.2/include/ruby-2.7.0/ruby/ruby.h:1609:20: note: in expansion of macro ‘RB_INT2NUM’
1609 | #define INT2NUM(x) RB_INT2NUM(x)
| ^~~~~~~~~~
mysql.c:2049:44: note: in expansion of macro ‘INT2NUM’
2049 | rb_define_const(cMysql, "SECURE_AUTH", INT2NUM(MYSQL_SECURE_AUTH));
| ^~~~~~~
mysql.c:2050:61: error: ‘MYSQL_OPT_GUESS_CONNECTION’ undeclared (first use in this function); did you mean ‘MYSQL_OPT_RECONNECT’?
2050 | rb_define_const(cMysql, "OPT_GUESS_CONNECTION", INT2NUM(MYSQL_OPT_GUESS_CONNECTION));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
/home/wheatman/.rvm/rubies/ruby-2.7.2/include/ruby-2.7.0/ruby/ruby.h:262:33: note: in definition of macro ‘RB_INT2FIX’
262 | #define RB_INT2FIX(i) (((VALUE)(i))<<1 | RUBY_FIXNUM_FLAG)
| ^
/home/wheatman/.rvm/rubies/ruby-2.7.2/include/ruby-2.7.0/ruby/ruby.h:1609:20: note: in expansion of macro ‘RB_INT2NUM’
1609 | #define INT2NUM(x) RB_INT2NUM(x)
| ^~~~~~~~~~
mysql.c:2050:53: note: in expansion of macro ‘INT2NUM’
2050 | rb_define_const(cMysql, "OPT_GUESS_CONNECTION", INT2NUM(MYSQL_OPT_GUESS_CONNECTION));
| ^~~~~~~
mysql.c:2051:68: error: ‘MYSQL_OPT_USE_EMBEDDED_CONNECTION’ undeclared (first use in this function)
2051 | rb_define_const(cMysql, "OPT_USE_EMBEDDED_CONNECTION", INT2NUM(MYSQL_OPT_USE_EMBEDDED_CONNECTION));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/wheatman/.rvm/rubies/ruby-2.7.2/include/ruby-2.7.0/ruby/ruby.h:262:33: note: in definition of macro ‘RB_INT2FIX’
262 | #define RB_INT2FIX(i) (((VALUE)(i))<<1 | RUBY_FIXNUM_FLAG)
| ^
/home/wheatman/.rvm/rubies/ruby-2.7.2/include/ruby-2.7.0/ruby/ruby.h:1609:20: note: in expansion of macro ‘RB_INT2NUM’
1609 | #define INT2NUM(x) RB_INT2NUM(x)
| ^~~~~~~~~~
mysql.c:2051:60: note: in expansion of macro ‘INT2NUM’
2051 | rb_define_const(cMysql, "OPT_USE_EMBEDDED_CONNECTION", INT2NUM(MYSQL_OPT_USE_EMBEDDED_CONNECTION));
| ^~~~~~~
mysql.c:2052:66: error: ‘MYSQL_OPT_USE_REMOTE_CONNECTION’ undeclared (first use in this function)
2052 | rb_define_const(cMysql, "OPT_USE_REMOTE_CONNECTION", INT2NUM(MYSQL_OPT_USE_REMOTE_CONNECTION));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/wheatman/.rvm/rubies/ruby-2.7.2/include/ruby-2.7.0/ruby/ruby.h:262:33: note: in definition of macro ‘RB_INT2FIX’
262 | #define RB_INT2FIX(i) (((VALUE)(i))<<1 | RUBY_FIXNUM_FLAG)
| ^
/home/wheatman/.rvm/rubies/ruby-2.7.2/include/ruby-2.7.0/ruby/ruby.h:1609:20: note: in expansion of macro ‘RB_INT2NUM’
1609 | #define INT2NUM(x) RB_INT2NUM(x)
| ^~~~~~~~~~
mysql.c:2052:58: note: in expansion of macro ‘INT2NUM’
2052 | rb_define_const(cMysql, "OPT_USE_REMOTE_CONNECTION", INT2NUM(MYSQL_OPT_USE_REMOTE_CONNECTION));
| ^~~~~~~
mysql.c:2053:54: error: ‘MYSQL_SET_CLIENT_IP’ undeclared (first use in this function); did you mean ‘MYSQL_SET_CHARSET_DIR’?
2053 | rb_define_const(cMysql, "SET_CLIENT_IP", INT2NUM(MYSQL_SET_CLIENT_IP));
| ^~~~~~~~~~~~~~~~~~~
/home/wheatman/.rvm/rubies/ruby-2.7.2/include/ruby-2.7.0/ruby/ruby.h:262:33: note: in definition of macro ‘RB_INT2FIX’
262 | #define RB_INT2FIX(i) (((VALUE)(i))<<1 | RUBY_FIXNUM_FLAG)
| ^
/home/wheatman/.rvm/rubies/ruby-2.7.2/include/ruby-2.7.0/ruby/ruby.h:1609:20: note: in expansion of macro ‘RB_INT2NUM’
1609 | #define INT2NUM(x) RB_INT2NUM(x)
| ^~~~~~~~~~
mysql.c:2053:46: note: in expansion of macro ‘INT2NUM’
2053 | rb_define_const(cMysql, "SET_CLIENT_IP", INT2NUM(MYSQL_SET_CLIENT_IP));
| ^~~~~~~
error_const.h:2661:27: error: ‘ER_XPLUGIN_IP’ undeclared (first use in this function); did you mean ‘ER_PLUGIN_OOM’?
2661 | rb_define_mysql_const(ER_XPLUGIN_IP);
| ^~~~~~~~~~~~~
/home/wheatman/.rvm/rubies/ruby-2.7.2/include/ruby-2.7.0/ruby/ruby.h:262:33: note: in definition of macro ‘RB_INT2FIX’
262 | #define RB_INT2FIX(i) (((VALUE)(i))<<1 | RUBY_FIXNUM_FLAG)
| ^
/home/wheatman/.rvm/rubies/ruby-2.7.2/include/ruby-2.7.0/ruby/ruby.h:1609:20: note: in expansion of macro ‘RB_INT2NUM’
1609 | #define INT2NUM(x) RB_INT2NUM(x)
| ^~~~~~~~~~
mysql.c:2286:62: note: in expansion of macro ‘INT2NUM’
2286 | #define rb_define_mysql_const(s) rb_define_const(eMysql, #s, INT2NUM(s))
| ^~~~~~~
error_const.h:2661:5: note: in expansion of macro ‘rb_define_mysql_const’
2661 | rb_define_mysql_const(ER_XPLUGIN_IP);
| ^~~~~~~~~~~~~~~~~~~~~
mysql.c: At top level:
cc1: note: unrecognized command-line option ‘-Wno-self-assign’ may have been intended to silence earlier diagnostics
cc1: note: unrecognized command-line option ‘-Wno-parentheses-equality’ may have been intended to silence earlier diagnostics
cc1: note: unrecognized command-line option ‘-Wno-constant-logical-operand’ may have been intended to silence earlier diagnostics
make: *** [Makefile:245: mysql.o] Error 1
make failed, exit code 2
Gem files will remain installed in /home/wheatman/.rvm/gems/ruby-2.7.2/gems/mysql-2.9.1 for inspection.
Results logged to /home/wheatman/.rvm/gems/ruby-2.7.2/extensions/x86_64-linux/2.7.0/mysql-2.9.1/gem_make.out
Am using Ubuntu 20.10, ruby 2.7.2 and mysql 8.0.22 and as far as I understand the problem in versions. Have someone encountered such problem? Is it versions issue? Any chance to connect ruby with mysql with my versions or I need to go back to older ones?

You're using extremely outdated and abandoned tools. The mysql gem has not seen a new release since 2013 and dbd-mysql has not been updated since 2010. Its highly questionable if they will work with modern versions of Ruby and MySQL.
What you want is the the mysql2 gem which is actively maintained.
# https://bundler.io/guides/bundler_in_a_single_file_ruby_script.html
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'mysql2', '~> 0.5.2'
end
client = Mysql2::Client.new(host: "localhost", username: "root")
results = client.query("SELECT * FROM users")
results.each do |row|
# do something with row, it's ready to rock
end

Related

error: expected initializer before 'std' and other errors after adding one line to working code

The following program worked already before I added line 12, the line reading:
" std::cout<<" Natürlicher Logarithmus von ";std::cin>>X; ".
Now I get a lot of errors (see below)!
Please Help
// Berechnung des natürlichen Logarithmus (Grenzwert nach Hurwitz)
#include <iostream>
#include <cmath>
#include <iomanip>
double h{1.0},Result,oldResult,Limit{1.0e-8},X{2.0},Inkr;
int c{0}, prez;
int main()
std::cout<<" Natürlicher Logarithmus von ";std::cin>>X;
std::cout<<" Genauigkeit (Zehnerpotenz): ";
std::cin>>prez; prez=std::abs(prez);
Limit=std::pow(10,-prez-1);
Result=((std::pow(X,h))-1)/h;
do
{
oldResult=Result;
h=h/2.0;
Result=((std::pow(X,h))-1)/h;
Inkr=std::abs(oldResult-Result);
++c;
}
while (Inkr>Limit);
Result=std::round(Result*std::pow(10,prez))*std::pow(10,-prez);
std::cout<<std::setw(prez+3)<<std::setprecision(prez)<<std::fixed<<Result<<" \t"<<c<<'\n';
return(0);
}
Logarithmun.cpp:12:5: error: expected initializer before 'std'
12 | std::cout<<" Nat├╝rlicher Logarithmus von ";std::cin>>X;
| ^~~
Logarithmun.cpp:12:53: error: 'cin' in namespace 'std' does not name a type
12 | std::cout<<" Nat├╝rlicher Logarithmus von ";std::cin>>X;
| ^~~
In file included from Logarithmun.cpp:3:
C:/msys64/mingw64/include/c++/12.2.0/iostream:60:18: note: 'std::cin' declared here
60 | extern istream cin; /// Linked to standard input
| ^~~
Logarithmun.cpp:13:10: error: 'cout' in namespace 'std' does not name a type
13 | std::cout<<" Genauigkeit (Zehnerpotenz): ";
| ^~~~
C:/msys64/mingw64/include/c++/12.2.0/iostream:61:18: note: 'std::cout' declared here
61 | extern ostream cout; /// Linked to standard output
| ^~~~
Logarithmun.cpp:14:10: error: 'cin' in namespace 'std' does not name a type
14 | std::cin>>prez; prez=std::abs(prez);
| ^~~
C:/msys64/mingw64/include/c++/12.2.0/iostream:60:18: note: 'std::cin' declared here
60 | extern istream cin; /// Linked to standard input
| ^~~
Logarithmun.cpp:14:21: error: 'prez' does not name a type
14 | std::cin>>prez; prez=std::abs(prez);
| ^~~~
Logarithmun.cpp:15:5: error: 'Limit' does not name a type
15 | Limit=std::pow(10,-prez-1);
| ^~~~~
Logarithmun.cpp:16:5: error: 'Result' does not name a type
16 | Result=((std::pow(X,h))-1)/h;
| ^~~~~~
Logarithmun.cpp:18:5: error: expected unqualified-id before 'do'
18 | do
| ^~
Logarithmun.cpp:26:5: error: expected unqualified-id before 'while'
26 | while (Inkr>Limit);
| ^~~~~
Logarithmun.cpp:28:5: error: 'Result' does not name a type
28 | Result=std::round(Result*std::pow(10,prez))*std::pow(10,-prez);
| ^~~~~~
Logarithmun.cpp:29:10: error: 'cout' in namespace 'std' does not name a type
29 | std::cout<<std::setw(prez+3)<<std::setprecision(prez)<<std::fixed<<Result<<" \t"<<c<<'\n';
| ^~~~
C:/msys64/mingw64/include/c++/12.2.0/iostream:61:18: note: 'std::cout' declared here
61 | extern ostream cout; /// Linked to standard output
| ^~~~
Logarithmun.cpp:31:5: error: expected unqualified-id before 'return'
31 | return(0);
| ^~~~~~
Logarithmun.cpp:32:1: error: expected declaration before '}' token
32 | }
| ^
I did remove line 12, but the errors remain.
So I obviously changed inadvertently something fundamental but I cannot find the problem. I do not understand the error at all! I used similar code a lot already.

How to handle errors when extracting data from untyped JSON in serde_json?

I have a serde_json::Value which I expect to contain an array of objects. From those objects I want to extract 2 values and return an error if anything fails. This is my code so far:
use std::collections::HashMap;
use anyhow::Result;
fn get_stock(response: serde_json::Value) -> Result<HashMap<String, u32>>{
response["products"]
.as_array()?.iter()
.map(|product| {
let product = product.as_object()?;
(
product["name"].as_str()?.to_owned(),
//as_u64 fails for some reason
product["stock"].as_str()?.parse::<u32>()?,
)
})
.collect()?
}
When I used .unwrap() this worked fine, but after changing the return type to Result and replacing unwraps with ? I'm getting the following compilation errors:
error[E0277]: the `?` operator can only be used on `Result`s, not `Option`s, in a function that returns `Result`
--> src/bin/products.rs:7:20
|
5 | / fn get_stock(response: serde_json::Value) -> Result<HashMap<String, u32>>{
6 | | response["products"]
7 | | .as_array()?.iter()
| | ^ use `.ok_or(...)?` to provide an error compatible with `Result<HashMap<std::string::String, u32>, anyhow::Error>`
8 | | .map(|product| {
... |
16 | | .collect()?
17 | | }
| |_- this function returns a `Result`
|
= help: the trait `FromResidual<Option<Infallible>>` is not implemented for `Result<HashMap<std::string::String, u32>, anyhow::Error>`
= note: required by `from_residual`
error[E0277]: the `?` operator can only be used in a closure that returns `Result` or `Option` (or another type that implements `FromResidual`)
--> src/bin/products.rs:9:46
|
8 | .map(|product| {
| ______________-
9 | | let product = product.as_object()?;
| | ^ cannot use the `?` operator in a closure that returns `(std::string::String, u32)`
10 | | (
11 | | product["name"].as_str()?.to_owned(),
... |
14 | | )
15 | | })
| |_________- this function should return `Result` or `Option` to accept `?`
|
= help: the trait `FromResidual<Option<Infallible>>` is not implemented for `(std::string::String, u32)`
= note: required by `from_residual`
error[E0277]: the `?` operator can only be used in a closure that returns `Result` or `Option` (or another type that implements `FromResidual`)
--> src/bin/products.rs:11:41
|
8 | .map(|product| {
| ______________-
9 | | let product = product.as_object()?;
10 | | (
11 | | product["name"].as_str()?.to_owned(),
| | ^ cannot use the `?` operator in a closure that returns `(std::string::String, u32)`
... |
14 | | )
15 | | })
| |_________- this function should return `Result` or `Option` to accept `?`
|
= help: the trait `FromResidual<Option<Infallible>>` is not implemented for `(std::string::String, u32)`
= note: required by `from_residual`
error[E0277]: the `?` operator can only be used in a closure that returns `Result` or `Option` (or another type that implements `FromResidual`)
--> src/bin/products.rs:13:42
|
8 | .map(|product| {
| ______________-
9 | | let product = product.as_object()?;
10 | | (
11 | | product["name"].as_str()?.to_owned(),
12 | | //as_u64 fails for some reason
13 | | product["stock"].as_str()?.parse::<u32>()?,
| | ^ cannot use the `?` operator in a closure that returns `(std::string::String, u32)`
14 | | )
15 | | })
| |_________- this function should return `Result` or `Option` to accept `?`
|
= help: the trait `FromResidual<Option<Infallible>>` is not implemented for `(std::string::String, u32)`
= note: required by `from_residual`
error[E0277]: the `?` operator can only be used in a closure that returns `Result` or `Option` (or another type that implements `FromResidual`)
--> src/bin/products.rs:13:58
|
8 | .map(|product| {
| ______________-
9 | | let product = product.as_object()?;
10 | | (
11 | | product["name"].as_str()?.to_owned(),
12 | | //as_u64 fails for some reason
13 | | product["stock"].as_str()?.parse::<u32>()?,
| | ^ cannot use the `?` operator in a closure that returns `(std::string::String, u32)`
miav#battlestation catbot % cargo run --bin products
Compiling catbot v0.1.0 (/Users/miav/Documents/Personal/Projects/programming/catbot)
error[E0277]: the `?` operator can only be used on `Result`s, not `Option`s, in a function that returns `Result`
--> src/bin/products.rs:7:20
|
5 | / fn get_stock(response: serde_json::Value) -> Result<HashMap<String, u32>>{
6 | | response["products"]
7 | | .as_array()?.iter()
| | ^ use `.ok_or(...)?` to provide an error compatible with `Result<HashMap<std::string::String, u32>, anyhow::Error>`
8 | | .map(|product| {
... |
16 | | .collect()?
17 | | }
| |_- this function returns a `Result`
|
= help: the trait `FromResidual<Option<Infallible>>` is not implemented for `Result<HashMap<std::string::String, u32>, anyhow::Error>`
= note: required by `from_residual`
error[E0277]: the `?` operator can only be used in a closure that returns `Result` or `Option` (or another type that implements `FromResidual`)
--> src/bin/products.rs:9:46
|
8 | .map(|product| {
| ______________-
9 | | let product = product.as_object()?;
| | ^ cannot use the `?` operator in a closure that returns `(std::string::String, u32)`
10 | | (
11 | | product["name"].as_str()?.to_owned(),
... |
14 | | )
15 | | })
| |_________- this function should return `Result` or `Option` to accept `?`
|
= help: the trait `FromResidual<Option<Infallible>>` is not implemented for `(std::string::String, u32)`
= note: required by `from_residual`
error[E0277]: the `?` operator can only be used in a closure that returns `Result` or `Option` (or another type that implements `FromResidual`)
--> src/bin/products.rs:11:41
|
8 | .map(|product| {
| ______________-
9 | | let product = product.as_object()?;
10 | | (
11 | | product["name"].as_str()?.to_owned(),
| | ^ cannot use the `?` operator in a closure that returns `(std::string::String, u32)`
... |
14 | | )
15 | | })
| |_________- this function should return `Result` or `Option` to accept `?`
|
= help: the trait `FromResidual<Option<Infallible>>` is not implemented for `(std::string::String, u32)`
= note: required by `from_residual`
error[E0277]: the `?` operator can only be used in a closure that returns `Result` or `Option` (or another type that implements `FromResidual`)
--> src/bin/products.rs:13:42
|
8 | .map(|product| {
| ______________-
9 | | let product = product.as_object()?;
10 | | (
11 | | product["name"].as_str()?.to_owned(),
12 | | //as_u64 fails for some reason
13 | | product["stock"].as_str()?.parse::<u32>()?,
| | ^ cannot use the `?` operator in a closure that returns `(std::string::String, u32)`
14 | | )
15 | | })
| |_________- this function should return `Result` or `Option` to accept `?`
|
= help: the trait `FromResidual<Option<Infallible>>` is not implemented for `(std::string::String, u32)`
= note: required by `from_residual`
error[E0277]: the `?` operator can only be used in a closure that returns `Result` or `Option` (or another type that implements `FromResidual`)
--> src/bin/products.rs:13:58
|
8 | .map(|product| {
| ______________-
9 | | let product = product.as_object()?;
10 | | (
11 | | product["name"].as_str()?.to_owned(),
12 | | //as_u64 fails for some reason
13 | | product["stock"].as_str()?.parse::<u32>()?,
| | ^ cannot use the `?` operator in a closure that returns `(std::string::String, u32)`
14 | | )
15 | | })
| |_________- this function should return `Result` or `Option` to accept `?`
|
= help: the trait `FromResidual<Result<Infallible, ParseIntError>>` is not implemented for `(std::string::String, u32)`
= note: required by `from_residual`
error: aborting due to 5 previous errors
I don't know the exact structure of the JSON in advance, so I cannot parse it into a struct. All I know is that it is most likely an array of objects which have a name string field and a stock integer field, which I want to extract into a map. If that doesn't happen to be the case, then I want to return an error. What is the simplest way to do this?
UPDATE:
After following Lagerbaer's suggestions and doing some digging I came up with the following solution.
use anyhow::{anyhow, Result};
use std::collections::HashMap;
fn get_stock(response: serde_json::Value) -> Result<HashMap<String, u32>> {
response
.get("products")
.ok_or(anyhow!("'products' not found"))?
.as_array()
.ok_or(anyhow!("'products' is not an array"))?
.iter()
.map(|product| -> Result<(String, u32)> {
let product = product.as_object().unwrap();
Ok((
product
.get("name")
.ok_or(anyhow!("'name' not found"))?
.as_str()
.ok_or(anyhow!("'name' is not a string"))?
.trim()
.to_owned(),
//as_u64 fails for some reason
product
.get("stock")
.ok_or(anyhow!("'stock' not found"))?
.as_str()
.ok_or(anyhow!("'stock' is not a string"))?
.parse::<u32>()?,
))
})
.collect()
}
Used ok_or() to map Option to Result, had to use the anyhow! macro to make it compatible with the anyhow result type.
It also turns out that collect() actually accepts an iterator of
Result<(String, u32)> to produce a Result<HashMap<String, u32>> with the exact behavior I wanted, that is, returning the first error or the complete hash map if there were no errors.
So there's a couple issues here and the compiler tries its best to tell you.
I'll get you started on the first, and then I encourage you to try going it alone for a bit.
So the very first error is that, apparently, as_array returns an Option and not Result and, hence, you can't use the ? operator there.
Luckily, the compiler tells you what to do: Option has a method ok_or that you should call. It will turn the Option into Result. If the Option is Some(value) you'll get a Ok(value), and if the Option is None, you'll get whatever error you specified in the ok_or argument.
Another easy to spot mistake is this: Your function returns a Result, so the return value should, obviously, be a Result. Hence the very final last ? should be removed, because that ? would take a Result and turn it into the "pure" value (if the result was Ok).
And then what's left is to figure out exactly what the closure should return. Maybe you can try that after fixing the other mistakes that I explained above.

I am trying add a background image in css, but I'm getting the following error

Failed to compile.
ERROR :
./src/app/shopping-list/shopping-edit/shopping-edit.component.css
Module Error (from ./node_modules/postcss-loader/src/index.js):
(Emitted value instead of an instance of Error) CssSyntaxError:
D:\demoApp\src\app\shopping-list\shopping-edit\shopping-edit.component.css:98:26:
Can't resolve 'assets/header.png' in
'D:\demoApp\src\app\shopping-list\shopping-edit'
CODE :
96 | }
97 | .section-testimonials{
> 98 | background-image:url(assets/header.png); ;
| ^
99 | }
100 |
Use quotes and a forward slash in the location:
96 | }
97 | .section-testimonials{
> 98 | background-image:url("./assets/header.png"); ;
|
99 | }
100 |

Octave: Installing package not successful

Trying to install the package communications-1.2.1.tar.gz on my windows-64-system leads to the following error message. What do I have to do to get it running?
genqamdemod.cc: In function 'octave_value_list Fgenqamdemod(const octave_value_list&, int)':
genqamdemod.cc:39:23: warning: 'int empty_arg(const char*, octave_idx_type, octave_idx_type)' is deprecated (declared at C:\Program
s\Octave\include\octave-4.2.1\octave\../octave/utils.h:58): use 'octave_value::is_empty' instead [-Wdeprecated-declarations]
int arg_is_empty1 = empty_arg ("genqamdemod", nr1, nc1);
^
genqamdemod.cc:39:57: warning: 'int empty_arg(const char*, octave_idx_type, octave_idx_type)' is deprecated (declared at C:\Program
s\Octave\include\octave-4.2.1\octave\../octave/utils.h:58): use 'octave_value::is_empty' instead [-Wdeprecated-declarations]
int arg_is_empty1 = empty_arg ("genqamdemod", nr1, nc1);
^
In file included from galois.h:24:0,
from galois-def.cc:21:
C:\Programs\Octave\include\octave-4.2.1\octave\../octave/config.h:28:2: warning: #warning "config.h has been deprecated; there is n
o need to include it directly" [-Wcpp]
#warning "config.h has been deprecated; there is no need to include it directly"
^
In file included from galois-def.cc:21:0:
galois.h:25:28: fatal error: octave/base-lu.h: No such file or directory
#include <octave/base-lu.h>
^
compilation terminated.
make: *** [galois-def.o] Error 1
make: Entering directory `/tmp/oct-2OnYWD/communications-1.2.1/src'
C:/Programs/Octave/bin/mkoctfile-4.2.1.exe -DGALOIS_DISP_PRIVATES __errcore__.cc -o __errcore__.oct
C:/Programs/Octave/bin/mkoctfile-4.2.1.exe -DGALOIS_DISP_PRIVATES __gfweight__.cc -o __gfweight__.oct
C:/Programs/Octave/bin/mkoctfile-4.2.1.exe -DGALOIS_DISP_PRIVATES cyclgen.cc -o cyclgen.oct
C:/Programs/Octave/bin/mkoctfile-4.2.1.exe -DGALOIS_DISP_PRIVATES cyclpoly.cc -o cyclpoly.oct
C:/Programs/Octave/bin/mkoctfile-4.2.1.exe -DGALOIS_DISP_PRIVATES genqamdemod.cc -o genqamdemod.oct
C:/Programs/Octave/bin/mkoctfile-4.2.1.exe -DGALOIS_DISP_PRIVATES -c galois-def.cc -o galois-def.o
make: Leaving directory `/tmp/oct-2OnYWD/communications-1.2.1/src'
pkg: error running `make' for the communications package.
error: called from
configure_make at line 95 column 9
install at line 192 column 7
pkg at line 394 column 9
The following table shows the result of pkg list.
Package Name | Version | Installation directory
---------------------+---------+-----------------------
control | 3.0.0 | C:\Programs\Octave\share\octave\packages\control-3.0.0
data-smoothing | 1.3.0 | C:\Programs\Octave\share\octave\packages\data-smoothing-1.3.0
database | 2.4.2 | C:\Programs\Octave\share\octave\packages\database-2.4.2
dataframe | 1.1.0 | C:\Programs\Octave\share\octave\packages\dataframe-1.1.0
dicom | 0.1.1 | C:\Programs\Octave\share\octave\packages\dicom-0.1.1
financial | 0.5.0 | C:\Programs\Octave\share\octave\packages\financial-0.5.0
fits | 1.0.7 | C:\Programs\Octave\share\octave\packages\fits-1.0.7
fuzzy-logic-toolkit | 0.4.5 | C:\Programs\Octave\share\octave\packages\fuzzy-logic-toolkit-0.4.5
ga | 0.10.0 | C:\Programs\Octave\share\octave\packages\ga-0.10.0
general | 2.0.0 | C:\Programs\Octave\share\octave\packages\general-2.0.0
generate_html | 0.1.13 | C:\Programs\Octave\share\octave\packages\generate_html-0.1.13
geometry | 2.1.1 | C:\Programs\Octave\share\octave\packages\geometry-2.1.1
gsl | 2.0.0 | C:\Programs\Octave\share\octave\packages\gsl-2.0.0
image | 2.6.1 | C:\Programs\Octave\share\octave\packages\image-2.6.1
instrument-control | 0.2.3 | C:\Programs\Octave\share\octave\packages\instrument-control-0.2.3
interval | 2.1.0 | C:\Programs\Octave\share\octave\packages\interval-2.1.0
io | 2.4.5 | C:\Programs\Octave\share\octave\packages\io-2.4.5
linear-algebra | 2.2.2 | C:\Programs\Octave\share\octave\packages\linear-algebra-2.2.2
lssa | 0.1.2 | C:\Programs\Octave\share\octave\packages\lssa-0.1.2
ltfat | 2.2.0 | C:\Programs\Octave\share\octave\packages\ltfat-2.2.0
mapping | 1.2.1 | C:\Programs\Octave\share\octave\packages\mapping-1.2.1
miscellaneous | 1.2.1 | C:\Programs\Octave\share\octave\packages\miscellaneous-1.2.1
nan | 3.1.2 | C:\Programs\Octave\share\octave\packages\nan-3.1.2
netcdf | 1.0.11 | C:\Programs\Octave\share\octave\packages\netcdf-1.0.11
nurbs | 1.3.10 | C:\Programs\Octave\share\octave\packages\nurbs-1.3.10
ocs | 0.1.5 | C:\Programs\Octave\share\octave\packages\ocs-0.1.5
odepkg | 0.8.5 | C:\Programs\Octave\share\octave\packages\odepkg-0.8.5
optim | 1.5.2 | C:\Programs\Octave\share\octave\packages\optim-1.5.2
quaternion | 2.4.0 | C:\Programs\Octave\share\octave\packages\quaternion-2.4.0
queueing | 1.2.5 | C:\Programs\Octave\share\octave\packages\queueing-1.2.5
signal | 1.3.2 | C:\Programs\Octave\share\octave\packages\signal-1.3.2
sockets | 1.2.0 | C:\Programs\Octave\share\octave\packages\sockets-1.2.0
sparsersb | 1.0.2 | C:\Programs\Octave\share\octave\packages\sparsersb-1.0.2
specfun | 1.1.0 | C:\Programs\Octave\share\octave\packages\specfun-1.1.0
splines | 1.3.2 | C:\Programs\Octave\share\octave\packages\splines-1.3.2
statistics | 1.3.0 | C:\Programs\Octave\share\octave\packages\statistics-1.3.0
stk | 2.3.4 | C:\Programs\Octave\share\octave\packages\stk-2.3.4
strings | 1.2.0 | C:\Programs\Octave\share\octave\packages\strings-1.2.0
struct | 1.0.14 | C:\Programs\Octave\share\octave\packages\struct-1.0.14
tisean | 0.2.3 | C:\Programs\Octave\share\octave\packages\tisean-0.2.3
tsa | 4.4.5 | C:\Programs\Octave\share\octave\packages\tsa-4.4.5
video | 1.2.3 | C:\Programs\Octave\share\octave\packages\video-1.2.3
windows | 1.2.4 | C:\Programs\Octave\share\octave\packages\windows-1.2.4
zeromq | 1.2.1 | C:\Programs\Octave\share\octave\packages\zeromq-1.2.1
I believe just a re-installation will do --> https://www.gnu.org/software/octave/#install
following the instructions exactly.

fir1 in octave with boxcar

I have to make filter, this is my code:
octave:1> x=randn(500);
octave:2> b=fir1(50,0.2,'stop',boxcar(51));
error: `boxcar' undefined near line 2 column 22
error: evaluating argument list element number 4
What is problem here?
Also Low-pass filter is 'low' or 'stop' in Octave?
#edit
New error:
octave:2> b=fir1(50,0.2,'stop');
error: `fir1' undefined near line 2 column 3
is it possible I don't have something?
You probably need to load the package (see the Octave FAQ). Run pkg list to confirm the package is installed and whether it is loaded. It will display a table of all installed packages with an asterisk in front of the packages that are loaded. You won't be able to use packages that are not loaded. See the following Octave session.
octave> partcnt (7) # does not work because function is not in search path
error: `partcnt' undefined near line 9 column 1
octave> pkg list # show list of packages
Package Name | Version | Installation directory
---------------+---------+-----------------------
control | 2.4.1 | /home/carandraug/octave/control-2.4.1
general | 1.3.2 | /home/carandraug/octave/general-1.3.2
generate_html | 0.1.5 | /home/carandraug/octave/generate_html-0.1.5
image | 2.1.0 | /home/carandraug/octave/image-2.1.0
miscellaneous | 1.2.0 | /home/carandraug/octave/miscellaneous-1.2.0
optim | 1.2.2 | /home/carandraug/octave/optim-1.2.2
signal | 1.2.0 | /home/carandraug/octave/signal-1.2.0
specfun | 1.1.0 | /home/carandraug/octave/specfun-1.1.0
struct | 1.0.10 | /home/carandraug/octave/struct-1.0.10
octave> pkg load miscellaneous # load package
octave> partcnt (7) # it works now
ans = 15
octave> pkg list # confirm that package is loaded (* in front of the name)
Package Name | Version | Installation directory
---------------+---------+-----------------------
control | 2.4.1 | /home/carandraug/octave/control-2.4.1
general *| 1.3.2 | /home/carandraug/octave/general-1.3.2
generate_html | 0.1.5 | /home/carandraug/octave/generate_html-0.1.5
image | 2.1.0 | /home/carandraug/octave/image-2.1.0
miscellaneous *| 1.2.0 | /home/carandraug/octave/miscellaneous-1.2.0
optim | 1.2.2 | /home/carandraug/octave/optim-1.2.2
signal | 1.2.0 | /home/carandraug/octave/signal-1.2.0
specfun | 1.1.0 | /home/carandraug/octave/specfun-1.1.0
struct | 1.0.10 | /home/carandraug/octave/struct-1.0.10
octave> pkg unload all # unload the package
octave> partcnt (7) # no longer works because it was removed from path
error: `partcnt' undefined near line 15 column 1
octave> pkg list
Package Name | Version | Installation directory
---------------+---------+-----------------------
control | 2.4.1 | /home/carandraug/octave/control-2.4.1
general | 1.3.2 | /home/carandraug/octave/general-1.3.2
generate_html | 0.1.5 | /home/carandraug/octave/generate_html-0.1.5
image | 2.1.0 | /home/carandraug/octave/image-2.1.0
miscellaneous | 1.2.0 | /home/carandraug/octave/miscellaneous-1.2.0
optim | 1.2.2 | /home/carandraug/octave/optim-1.2.2
signal | 1.2.0 | /home/carandraug/octave/signal-1.2.0
specfun | 1.1.0 | /home/carandraug/octave/specfun-1.1.0
struct | 1.0.10 | /home/carandraug/octave/struct-1.0.10
The reason why the general package is loaded automatically when you load miscellaneous, is because general is a dependency of miscellaneous.
From the man page:
usage: b = fir1(n, w [, type] [, window] [, noscale])
You probably want e.g.:
b=fir1(50, [0.1 0.2], 'stop');