How do I save xml to mysql database? - sql

I found this from original link:
http://www.ecb.europa.eu/stats/exchange/eurofxref/html/index.en.html#dev
e.g.
<?php
function StartElement($parser, $name, $attrs) {
if (!empty($attrs['RATE'])) {
echo "1€=".$attrs['RATE']." ".$attrs['CURRENCY']."<br />";
}
}
$xml_parser= xml_parser_create();
xml_set_element_handler($xml_parser, "StartElement", "");
// for the following command you will need file_get_contents (PHP >= 4.3.0)
// and the config option allow_url_fopen=On (default)
xml_parse($xml_parser, file_get_contents ("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"));
xml_parser_free($xml_parser);
?>
then would be look like:
INSERT INTO `curr_table` (`curr_id`, `curr_title`, `Code`, `decimal_place`, `value`) VALUES
(1, 'EURO', 'EUR', '2', 1.3917),
(2, 'Japan Yen', 'JPY', '2', 112.88),
(3, 'Bla..bla', 'BGN', '2', 1.9558),
..............................etc
(20, 'Bla..bla.', 'CZK', '2', 24.575);
Any pointer and samples code would be appreciate and thanks in advance

If you are using MySQL 5.1 or newer, there is an XML support:
http://dev.mysql.com/tech-resources/articles/xml-in-mysql5.1-6.0.html#xml-5.1-in-and-out

Related

How i can generate a content table in a pdf using wkhtml2pdf in a django application

My goal is to generate a pdf in my django application, i would like to set a content table in the second page(page 2 of the pdf) using wkhtml2pdf,
I have add these new lines where i have put "2"(number of page where i want to display my content table) in the option --dump-default-toc-xsl, and i have put in the option (--dump-outline and -xsl-style-sheet) the file name of my html pdf that i want to render it to a pdf.
'--outline': True,
'--dump-outline':'/home/dev/.virtualenvs/django-projects/project1/templates/pages/generate_pdf.html',
'-xsl-style-sheet':'/home/dev/.virtualenvs/django-projects/project1/templates/pages/generate_pdf.html',
'--dump-default-toc-xsl':2,
this all the code in my settings.py
WKHTMLTOPDF_CMD = '/usr/local/bin/wkhtmltopdf'
WKHTMLTOPDF_CMD_OPTIONS = {
'quiet':True,
'disable-smart-shrinking':True,
'enable-local-file-access':True,
'page-size':'A4',
'margin-right':'0',
'margin-top':'15',
'margin-bottom':'10',
'margin-left':'3',
'encoding':"UTF-8",
#'--outline': None,
"zoom":1,
'javascript-delay':1000,
'footer-center':'[page]/[topage]',
"no-stop-slow-scripts":True,
'outline': True,
'dump-outline':'/home/dev/.virtualenvs/django-projects/project1/templates/pages/generate_pdf.html',
'xsl-style-sheet':'/home/dev/.virtualenvs/django-projects/project1/templates/pages/generate_pdf.html',
'dump-default-toc-xsl':2,
}
------> those lines that i have add generate me an error said:
CalledProcessError at /report/
Command '['/usr/local/bin/wkhtmltopdf', '----dump-default-toc-xsl', '2', '----dump-outline', '/home/dev/.virtualenvs/django-projects/project1/templates/pages/generate_pdf.html', '----outline', 'True', '---xsl-style-sheet', '/home/dev/.virtualenvs/django-projects/project1/templates/pages/generate_pdf.html', '--disable-smart-shrinking', '--enable-local-file-access', '--encoding', 'UTF-8', '--footer-center', '[page]/[topage]', '--javascript-delay', '1000', '--margin-bottom', '10', '--margin-left', '3', '--margin-right', '0', '--margin-top', '15', '--no-stop-slow-scripts', '--page-size', 'A4', '--quiet', '--zoom', '1', '/tmp/wkhtmltopdf3m5sidam.html', '-']' returned non-zero exit status 1.
Is it possible to generate a content table in a pdf using wkhtml2pdf in a django application!
any help will be appreciated am stuck on.thanks in advance.

Disable checkout page for not logged in users in specific countries

I have a shop in WooCommerce and I try to disable the check-out page for some countries (France and Greece). These countries should be forced to log-in as b2b customers in order to continue their check-out.
I use this code, but unfortunately the action applies to all countries and not just the two I need (France and Greece):
add_action( 'template_redirect', 'checkout_redirect_non_logged_to_login_access');
function checkout_redirect_non_logged_to_login_access() {
global $woocommerce;
$msg_states = array( 'BE', 'BG', 'DE', 'DK', 'EE', 'FI', 'IT', 'HR', 'LI', 'LT', 'MT', 'IE', 'IS', 'NL', 'NO', 'PT', 'RO', 'CH', 'SK', 'SI', 'CZ', 'HU', 'GB', 'AT' );
// Here the conditions (woocommerce checkout page and unlogged user)
if( is_checkout() && !is_user_logged_in() && !in_array( WC()->customer->get_billing_country, $msg_states ) ) {
// Redirecting to your custom login area
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
// always use exit after wp_redirect() function.
exit;
}
}
Is there any possibility to differentiate the kind of log-in, from a normal user to b2b client?

{ scientistSeedError: 'syntax error at or near "["' }

I am trying to add some data to a table in postgresql through a seed file I've created. all the routes work and I already created another table. However, on this particular table I'm trying to add an array in one of the columns, but I keep getting this error. Does anyone has an idea of what it could be? or how can I inform a postgresql table I want to add some arrays:
```{ scientistSeedError: 'syntax error at or near "["' }````
below is the query to create the table
const query =
CREATE TABLE "scientist" (
"id" serial NOT NULL,
"first_name" varchar(255) NOT NULL,
"last_name" varchar(255) NOT NULL,
"area_expertise" varchar(255) NOT NULL,
"areaExpertise" TEXT ARRAY NOT NULL,
"field" TEXT ARRAY NOT NULL,
"wiki_link" varchar(255) NOT NULL,
"picture" varchar(255) NOT NULL,
"short_description" varchar(400),
CONSTRAINT "scientist_pk" PRIMARY KEY ("id")
) WITH (
OIDS=FALSE
);`
;
try {
await db.query(query);
res.send("Database successfully created");
} catch(e){
console.log(e.message)
}
};```
**and below a fragment of the query to add the data. As is too large to post here but**
```module.exports.seedPart2 = async (req, res, next) => {
// Scientist query
const scientistsQuery = `
INSERT INTO scientist (first_name,
last_name,
area_expertise,
field,
issue_tackled,
wiki_link,
picture) VALUES
('Eugenie', 'Clark', 'Ichthyologist', 'biology', 'Environmental preservation', 'https://en.wikipedia.org/wiki/Eugenie_Clark', 'http://www.alertdiver.com/cdn/13649.jpg'),
('Susan', 'Solomon', 'Atmospheric Chemist', 'Chemistry', ['Environmental preservation', 'Chemistry'], 'https://en.wikipedia.org/wiki/Susan_Solomon' , 'https://upload.wikimedia.org/wikipedia/commons/thumb/8/80/Susan_Solomon-Desk_With_Globe.jpg/1024px-Susan_Solomon-Desk_With_Globe.jpg'),
('Donna Theo', 'Strickland', 'Physics, Optics, and Lasers', 'Physics', ['Medicine', 'New technologies'], 'https://en.wikipedia.org/wiki/Donna_Strickland', 'https://upload.wikimedia.org/wikipedia/commons/thumb/3/38/Donna_Strickland_EM1B5760_%2846183560632%29_%28cropped%29.jpg/220px-Donna_Strickland_EM1B5760_%2846183560632%29_%28cropped%29.jpg'),
('Linda', 'Brown Buck', 'Biologist', 'Biology', 'Medicine', 'https://en.wikipedia.org/wiki/Linda_B._Buck', 'https://upload.wikimedia.org/wikipedia/commons/thumb/2/20/Linda_Buck_2015_%28cropped%29.jpg/200px-Linda_Buck_2015_%28cropped%29.jpg')`;
try {
const {rows} = await db.query(scientistsQuery);
res.status(201).json(rows)
} catch (e) {
console.log({ scientistSeedError: e.message });
}
};
Thank you for any help!!!
Array literals in PosgreSQL are denoted by curly braces, not square braces (see the documentation for details):
const scientistsQuery = `
INSERT INTO scientist (first_name,
last_name,
area_expertise,
field,
issue_tackled,
wiki_link,
picture) VALUES
('Eugenie', 'Clark', 'Ichthyologist', 'biology', 'Environmental preservation', 'https://en.wikipedia.org/wiki/Eugenie_Clark', 'http://www.alertdiver.com/cdn/13649.jpg'),
('Susan', 'Solomon', 'Atmospheric Chemist', 'Chemistry', '{"Environmental preservation", "Chemistry"}', 'https://en.wikipedia.org/wiki/Susan_Solomon' , 'https://upload.wikimedia.org/wikipedia/commons/thumb/8/80/Susan_Solomon-Desk_With_Globe.jpg/1024px-Susan_Solomon-Desk_With_Globe.jpg'),
('Donna Theo', 'Strickland', 'Physics, Optics, and Lasers', 'Physics', '{"Medicine", "New technologies"}', 'https://en.wikipedia.org/wiki/Donna_Strickland', 'https://upload.wikimedia.org/wikipedia/commons/thumb/3/38/Donna_Strickland_EM1B5760_%2846183560632%29_%28cropped%29.jpg/220px-Donna_Strickland_EM1B5760_%2846183560632%29_%28cropped%29.jpg'),
('Linda', 'Brown Buck', 'Biologist', 'Biology', 'Medicine', 'https://en.wikipedia.org/wiki/Linda_B._Buck', 'https://upload.wikimedia.org/wikipedia/commons/thumb/2/20/Linda_Buck_2015_%28cropped%29.jpg/200px-Linda_Buck_2015_%28cropped%29.jpg')`;

Nodejs Mysql Insert is adding backslashes to strings and fails

I'm trying to do a simple sql insert using nodejs and express trying various formats I've found online, but this appears to be the accepted way. When I view the error it's adding extra backslashes to the query and failing.
The code:
console.log(request.body);
var post = {uid: request.session.uid, title: request.body.title, created: request.body.createdAt};
connection.query('INSERT INTO projects (uid, title, created) SET ? ', post, function (error, results, fields) {
console.log(error);
});
The first body console.log:
{ title: 'aewgawegr', createdAt: '1574219119301' }
The error message:
sqlMessage:
'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'SET `uid` = 1, `title` = \'aewgawegr\', `created` = \'1574219119301\'\' at line 1',
sqlState: '42000',
index: 0,
sql:
'INSERT INTO projects (uid, title, created) SET `uid` = 1, `title` = \'aewgawegr\', `created` = \'1574219119301\' ' }
For reference: https://dev.mysql.com/doc/refman/8.0/en/insert.html
You cannot combine the two usage syntax:
INSERt INTO `table` (column1, ...) VALUES (value1, ...
with
INSERT INTO `table` SET `column1`='value1', ....
You can do something like this instead of passing json object
"INSERT INTO projects (uid, title, created) VALUES (1, 'Michelle', 'Blue Village 1')";
e.g in your case you can use string interpolation:
`INSERT INTO projects (uid, title, created) VALUES ('${request.session.uid}', '${request.body.title}', '${request.body.createdAt}')`;

Error: INSERT INTO MariaDB

my code:
<?php
$servername = "127.0.0.1";
$database = "";
$username = "";
$password = "";
$conn = mysqli_connect($servername, $username, $password, $database);
if (!$conn) {
die("failed: " . mysqli_connect_error());
}
echo "connect<br>";
$sql = "INSERT INTO test2 (1, 2, 3) VALUES ('1', '2', '3')";
if (mysqli_query($conn, $sql)) {
echo "add";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
i got error:
connect
Error: INSERT INTO test2 (1, 2, 3) VALUES ('1', '2', '3')
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '1, 2, 3) VALUES ('1', '2', '3')' at line 1
how to fix it?
If you name your columns using only numbers, then you'll have to use backticks around them, e.g. to quote them. See the official chapter Identifier Names, which states:
Further rules:
Identifier names may begin with a numeral, but can't only contain
numerals unless quoted.
So, correct would be:
$sql = "INSERT INTO test2 (`1`, `2`, `3`) VALUES ('1', '2', '3')";
Or, if the columns are of type INT (or similar):
$sql = "INSERT INTO test2 (`1`, `2`, `3`) VALUES (1, 2, 3)";
In the first set of parentheses, '1', '2', '3' are expected as column names. I'm thinking you intended these to be values instead.