PDO insert into MySQL Database - mysql

I'm trying to insert a parsed table into a mysql database. But the table within the database stays empty. None of the values are inserted.
I already checked this PDO with INSERT INTO through prepared statements and tried to apply the answers to my case. But I'm still getting trouble. What am I missing?
This is my code so far:
// Simple Dom
require_once('simple_html_dom.php');
require_once 'config.php';
$host=$config['DB_HOST'];
$dbname=$config['DB_DATABASE'];
// Get Connection to HTML and DATABASE
$html = file_get_html('url');
$pdo = new PDO("mysql:host=$host;dbname=$dbname",$config['DB_USERNAME'],$config['DB_PASSWORD']);
// Find TABLE INSIDE HTML
$table = $html->find('table', 1);
// find CELLS of each ROW, starting at 2nd row
foreach($table ->find('tr') as $rowNumber => $row) { if ( $rowNumber < 1 ) continue;
$team = "Timberwolves";
$season = "9";
// Get all columns of the table
$pos = $row->find('td', 0);
$player = $row->find('td', 1)->plaintext;
$age = $row->find('td', 2);
$twoga = $row->find('td', 3);
$twopct = $row->find('td', 4);
$fta = $row->find('td', 5);
$ftpct = $row->find('td', 6);
$threega = $row->find('td', 7);
$threepct = $row->find('td', 8);
$orb = $row->find('td', 9);
$drb = $row->find('td', 10);
$ast = $row->find('td', 11);
$stl = $row->find('td', 12);
$tov = $row->find('td', 13);
$blk = $row->find('td', 14);
$oo = $row->find('td', 15);
$do = $row->find('td', 16);
$po = $row->find('td', 17);
$to = $row->find('td', 18);
$od = $row->find('td', 19);
$dd = $row->find('td', 20);
$pd = $row->find('td', 21);
$td = $row->find('td', 22);
// Echo some of the found values to test the code
echo "'$season', '$team', '$pos', '$player', '$age', '$twoga', '$twopct','$fta','$ftpct','$threega', '$threepct', '$orb','$drb','$ast','$stl','$tov','$blk','$oo','$do','$po','$to','$od','$dd','$pd','$td')<br>";
// INSERT for each row
$statement = $pdo->prepare("INSERT INTO `teams` (season,team,pos,player,age,2ga,2g%,fta,ft%,3ga,3g%,orb,drb,ast,stl,tov,blk,oo,do,po,to,od,dd,pd,td)
VALUES (:season,:team,:pos,:player,:age,:twoga,:twopct,:fta,:ftpct,:threega,:threepct,:orb,:drb,:ast,:stl,:tov,:blk,:oo,:do,:po,:to,:od,:dd,:pd,:td)");
$statement->execute(array(":season"=>$season,":team"=>$team,":pos"=>$pos,":player"=>$player,":age"=>$age,":twoga"=>$twoga,":twopct"=>$twopct,":fta"=>$fta,":ftpct"=>$ftpct,
":threega"=>$threega,":threepct"=>$threepct,":orb"=>$orb,":drb"=>$drb,":ast"=>$ast,":stl"=>$stl,":tov"=>$tov,":blk"=>$blk,":oo"=>$oo,":do"=>$do,":po"=>$po,":to"=>$to,":od"=>$od,":dd"=>$dd,":pd"=>$pd,":td"=>$td));
// END OF LOOP
}
The table-parsing works fine in my eyes. I'll get this as output:
'9', 'Timberwolves', 'PG', 'Mike Conley ', '29', '47', '50','51','86','61', '41', '8','28','61','56','58','8','8','8','6','7','6','6','2','7')
'9', 'Timberwolves', 'PG', 'Steve Blake ', '35', '15', '46','6','80','47', '34', '5','23','60','31','52','4','7','6','1','4','4','4','1','5')
'9', 'Timberwolves', 'SG', 'Isaiah Thomas ', '27', '59', '53','80','91','83', '38', '10','19','56','37','49','4','7','9','8','9','5','4','2','6')
'9', 'Timberwolves', 'SF', 'Rondae Hollis-Jefferson ', '22', '48', '46','45','75','12', '22', '34','62','27','64','60','25','3','5','7','3','8','6','7','6')
'9', 'Timberwolves', 'SF', 'Tony Snell ', '25', '14', '55','7','81','50', '41', '6','30','13','32','86','6','9','2','3','1','5','4','2','6')
'9', 'Timberwolves', 'PF', 'Joel Embiid ', '22', '77', '50','99','78','41', '37', '49','72','27','48','6','98','3','7','9','6','9','3','9','4')
'9', 'Timberwolves', 'PF', 'Draymond Green ', '26', '28', '49','27','71','35', '31', '25','63','69','87','53','43','5','7','4','4','9','7','7','8')
'9', 'Timberwolves', ' C', 'Marc Gasol ', '32', '64', '48','41','84','35', '39', '15','49','43','37','59','40','7','6','5','4','6','4','5','5')
'9', 'Timberwolves', ' C', 'Marreese Speights ', '29', '35', '52','31','88','65', '37', '40','63','15','23','70','29','7','4','7','1','5','1','6','2')
'9', 'Timberwolves', 'SF', 'Dante Cunningham ', '29', '19', '58','5','59','36', '39', '21','41','7','33','90','17','8','2','4','1','5','4','4','5')
'9', 'Timberwolves', ' C', 'Boban Marjanovic ', '28', '69', '55','53','81','0', '0', '84','74','8','24','82','35','4','5','9','2','9','4','9','4')
'9', 'Timberwolves', 'SF', 'Rasual Butler ', '36', '19', '62','10','69','32', '31', '3','32','15','36','91','46','7','3','4','1','8','6','5','6')
This is how the DB-table looks like:

Related

in Python 3, how can I slice JSON data where objects all start with same name?

I have a JSON string that returns device info and if devices are found, the devices will be listed as device0, device1, device2, etc. In this simple code below, how can I discover all devices found in the JSON and then print the the info below for each device? I currently lookup each device statically and I want this discovery to be dynamic and print the results for each one found.
r1 = requests.get(url = url_api, params = PARAMS)
devicedata = r1.json()
if 'device0' in devicedata:
print('')
device0Name = (devicedata['device0']['device_name'])
print(device0Name)
print('Temp: {}'.format (devicedata['device0']['obs'][0]['ambient_temp']))
print('Probe Temp: {}'.format (devicedata['device0']['obs'][0]['probe_temp']))
print('Humidity: {}%'.format (devicedata['device0']['obs'][0]['humidity']))
print('')
# JSON info looks like this...
{'device0': {'success': True, 'device_type': 'TX60', 'obs': [{'device_id': '1111', 'device_type': 'TX60', 'u_timestamp': '1580361017', 'ambient_temp': '45.7', 'probe_temp': '45.5', 'humidity': '82', 'linkquality': '100', 'lowbattery': '0', 'success': '9', 's_interval': '99', 'timestamp': '1/29/2020 11:10 PM', 'utctime': 1580361017}], 'alerts': {'miss': {'id': '520831', 'alert_type': 'miss', 's_id': '1111', 'max': '-100', 'min': '30', 'wet': '0', 'alert_id': '1', 'phone': 'yes', 'email': '', 'state': None}, 'batt': {'id': '520832', 'alert_type': 'batt', 's_id': '1111', 'max': '-100', 'min': '-100', 'wet': '0', 'alert_id': '1', 'phone': 'yes', 'email': '', 'state': None}}, 'ispws': 0, 'unit': {'temp': '°F', 'temp2': '°F', 'rh': '%'}, 'device_id': '1111', 'expired': '0', 'interval': '30', 'reg_date': '2020-01-17 22:06:48', 'create_date': 1579298808, 'device_name': 'Back Yard', 'assocGateway': '1', 'problem': False}, 'device1': {'success': True, 'device_type': 'TX60', 'obs': [{'device_id': '2222', 'device_type': 'TX60', 'u_timestamp': '1580360303', 'ambient_temp': '63.6', 'probe_temp': 'N/C', 'humidity': '64', 'linkquality': '100', 'lowbattery': '0', 'success': '9', 's_interval': '99', 'timestamp': '1/29/2020 10:58 PM', 'utctime': 1580360303}], 'alerts': {'miss': {'id': '520220', 'alert_type': 'miss', 's_id': '2222', 'max': '-100', 'min': '30', 'wet': '0', 'alert_id': '1', 'phone': 'yes', 'email': '', 'state': None}, 'batt': {'id': '520221', 'alert_type': 'batt', 's_id': '2222', 'max': '-100', 'min': '-100', 'wet': '0', 'alert_id': '1', 'phone': 'yes', 'email': '', 'state': None}}, 'ispws': 0, 'unit': {'temp': '°F', 'temp2': '°F', 'rh': '%'}, 'device_id': '3333', 'expired': '1', 'interval': '30', 'reg_date': '2016-03-19 01:45:04', 'create_date': 1500868369, 'device_name': 'Crawl Space', 'assocGateway': '1', 'problem': False}, 'device2': {'success': True, 'device_type': 'TX60', 'obs': [{'device_id': '3333', 'device_type': 'TX60', 'u_timestamp': '1580360195', 'ambient_temp': '70.2', 'probe_temp': 'N/C', 'humidity': '48', 'linkquality': '100', 'lowbattery': '0', 'success': '9', 's_interval': '99', 'timestamp': '1/29/2020 10:56 PM', 'utctime': 1580360195}], 'alerts': None, 'ispws': 0, 'unit': {'temp': '°F', 'temp2': '°F', 'rh': '%'}, 'device_id': '3333', 'expired': '0', 'interval': '15', 'reg_date': '2020-01-30 04:34:00', 'create_date': 1580358840, 'device_name': 'Basement', 'assocGateway': '2', 'problem': False}, 'tz': 'America/Chicago'}
The output for a single device looks like this..
Back Yard
Temp: 50.9
Probe Temp: 51.2
Humidity: 92%
Crawl Space
Temp: 65.4
Probe Temp: N/C
Humidity: 55%
Basement
Temp: 70
Probe Temp: N/C
Humidity: 48%
Found it.
for devKey in devicedata.keys():
if "device" in devKey:
dev = devicedata[devKey]
name = dev["device_name"]
obs = dev["obs"][0]
temp = obs["ambient_temp"]
probeTemp = obs["probe_temp"]
humidity = obs["humidity"]
print(name)
print('Temp: {}'.format(temp))
print('Probe Temp: {}'.format(probeTemp))
print('Humidity: {}%'.format(humidity))
print('')

error code: 1046 no database selected when creating database in MySQL

I am getting an error message "error code: 1046 no database selected" using MySQL
Below is the script that I wrote, Any suggestions on why I'm getting the error message?
DROP DATABASE IF EXISTS Bookstore;
Create Database Bookstore;
Use Bookstore;
Create Table Subject
(SubjectCode nvarchar(3) Not Null primary key,
Subject nvarchar(15) Null);
Create Table Book
(ISBN nvarchar(13) Not Null primary key,
Title nvarchar(50) Null,
Author nvarchar(30) Null,
Publisher nvarchar(30) Null,
Subject_Code nvarchar(3) Null References Subject (Subject_Code),
Shelf_Location nvarchar(7) Null,
Fiction bit Null);
Use Bookstore;
Insert Into Subject
Values
('ART', 'Art'),
('BSN', 'Business'),
('BSS', 'Best Seller'),
('EDC', 'Education'),
('FNT', 'Fantasy'),
('HMR', 'Humor'),
('MST', 'Myster'),
('PHL', 'Philosophy'),
('RLG', 'Religion'),
('RMN', 'Romance'),
('SCF', 'Science Fiction'),
('SLH', 'Self Help');
Use Bookstore;
Insert Into Book
Values
('0-111-11111-1', '89 Years in a Sand Trapxx', 'Beck, Fred', 'Hill and Wang', 'HMR', 'RC-1111', '0'),
('0-15-500139-6', 'Business Programming in C', 'Milsspaugh, A.C.', 'The Dryden Press', 'BSN', 'RC-1111', '0'),
('0-394-75843-9', 'Cultural Literacy', 'Hirsch, E.D. Jr.', 'Vintage', 'Bss', 'RC-1115', '0'),
('0-440-22284-2', 'Five Days in Paris', 'Steel, Daniels', 'Dell Publishing', 'RMN', 'RC-1114', '0'),
('0-446-51251-6', 'Megatrends', 'Naisbitt, John', 'Warner Books', 'PHL', 'RC-1114', '0'),
('0-446-51652-X', 'Bridges of Madison County', 'Waller, Robert James', 'Warner Books', 'BSS', 'RC-1114', '1'),
('0-446-60274-4', 'The Rules', 'Fein/Schneider', 'Warner Books', 'SLH', 'RC-1111', '0'),
('0-451-16095-9', 'The Stand', 'King, Stephen', 'Signet', 'MST', 'RC-1113', '0'),
('0-452-26011-6', 'Song of Solomon', 'Morrision, Toni', 'Plume/Penguin', 'BSS', 'Rc-1114', '1'),
('0-517-59905-8', 'How to Talk to Anyone, Anytime, Anywhere', 'King, Larry', 'Crown', 'SLH', 'RC-1113', '0'),
('0-534-26076-4', 'A Quick Guide to the Internet', 'Filus, Steve', 'Intergrated Media Group', 'BSN', 'RC-1111', '0'),
('0-553-24484-X', 'Prospering Woman', 'Ross, Ruth', 'Bantam Brooks', 'SLH', 'RC-1111', '0'),
('0-670-85332-1', 'How to be Hap-Hap-Happy Like Me', 'Markoe, Merrill', 'Viking', 'HMR', 'RC-1113', '1'),
('0-671-67158-8', 'Time Wars', 'Rifkin, Jeremy', 'Simon and Schuster', 'PHL', 'RC-1115', '0'),
('0-697-12897-0', 'Quick Basic and QBasic Using Modular Structure', 'Filus, Steve', 'B & E Tech', 'BSN', 'RC-1112', '0'),
('0-697-21361-7', 'Desktop Publishing Using PageMaker 5.0', 'Filus, Steve', 'B & E Tech', 'BSN', 'RC-1111', '0'),
('0-8007-1213-7', 'Secrets of Closing the Sale', 'Ziglar, Zig', 'Revel', 'BSN', 'RC-1112', '0'),
('0-8041-0753-X', 'The Kitchen God''s Wife', 'Tan, Amy', 'Ivy Books', 'BSS', 'RC-1113', '1'),
('0-8109-3158-3', 'Thomas Cole', 'Powell, Earl A.', 'Abrams', 'ART', 'RC-1112', '0'),
('0-8109-8052-5', 'The Art of Walt Disney', 'Finch, Christopher', 'Albradale', 'Art', 'RC-1112', '0'),
('0-8487-0504', 'Erica Wilson''s Quilts of America', 'Wilson, Erica', 'Oxmoor House', 'ART', 'RC-1112', '0'),
('0-87826-2', 'Know Your Lhaso Apso', 'Schneider, Earl', 'The Pet Library LTD', 'SLH', 'RC-1112', '0'),
('0-89997=087-7', 'Afoot and Afield in Orange County', 'Schad, Jerry', 'Wilderness Press', 'SLH', 'RC-1112', '0'),
('0-915391-40-6', 'Designing User Interfaces', 'Powell, James E.', 'Micontrend', 'BSN', 'RC-1114', '0'),
('0-917849-25-6', 'I am enough', 'Stortz, Margaret', 'Science of Mind', 'PHL', 'RC-115', '0'),
('0-934136-27-0', 'Wine Makers Guide', 'Nury/Fugelsang', 'Western Tanager', 'MST', 'RC-1112', '0'),
('0-9616878-6-X', 'Classics, US Aircraft of World War II', 'Meyer, Mark', 'Howell Press', 'ART', 'RC-1112', '0'),
('1-55615-484-4', 'Code Complete', 'Mc Connell, Steve', 'Microsoft Press', 'BSN', 'RC-1115', '0'),
('1-82423-2218-3', 'The Way', 'Chaney, Elana', 'Tyndale', 'RLG', 'RC-1111', '0');
make sure the database is created with command:
Create Database dbname

How do I iterate a table in Jade assigning each <td> with a unique id?

I am trying to create a table containing 9 rows with 9 columns. I want to create this by iterating through it with the help of Jade. I'm quite new to Jade so I am probably way off here, but this is my code now;
var test1 = ['0', '1', '2', '3', '4', '5', '6', '7', '8']
var test2 = ['0', '1', '2', '3', '4', '5', '6', '7', '8']
for (var i = 0; i < test1.length; i++) {
tr
for (var o = 0; i < test2.length; o++) {
td(id='square-'+test1[i]+test2[o])
}
}
This (obviously?) gives me a syntax error.
How do I go about to iterate a table with 9 rows and 9 cols and give them id=square00, square01, square02 etc..?
Something like this should do the trick:
- var rows = ['0', '1', '2', '3', '4', '5', '6', '7', '8'];
- var columns = ['0', '1', '2', '3', '4', '5', '6', '7', '8'];
table
tbody
each row in rows
tr
each column in columns
td(id="square-" + row + "-" + column) #{row} - #{column}
Note that the javascript is prefaced by - , with the exception of the lines that are doing the iterating. That's because each is a reserved keyword that Jade (now Pug) recognizes as meaning you want to iterate.
Good luck!

Getting data if one value is NULL

I am having an issue with the code below and my issue is the following:
I need to be able to select the shop_shipping_rule_region_code if the country_iso is NULL but I also need to get the $country_iso data as well if present
What I am after:
The Final Result will have:
Overnight UPS
NZ Snail Mail
Whole World (in this example)
DB:
`shop_shipping_rules` (`shop_shipping_rule_id`, `shop_shipping_rule_country_iso`, `shop_shipping_rule_region_code`, `shop_shipping_rule_name`,
`shop_shipping_rule_type_single`, `shop_shipping_rule_item_single`, `shop_shipping_rule_type_multi`,
`shop_shipping_rule_item_multiple`,`shop_shipping_rule_created`, `shop_shipping_rule_modified`, `website_id`)
VALUES
(41, 'NZ', 'ALL', 'Overnight UPS', 'single', 2.00, 'multi', 4.00, '2013-11-05 02:30:19', '2013-11-05 03:00:27', 64),
(44, 'NZ', NULL, 'NZ Snail Mail', 'single', 25.00, 'multi', 35.00, '2013-11-14 03:57:06', NULL, 64),
(45, NULL, 'ALL', 'Whole World', 'single', 90.00, 'multi', 150.00, '2013-11-14 05:05:53', NULL, 64),
(46, NULL, 'EU', 'EU Ship', 'single', 28.00, 'multi', 35.00, '2013-11-15 01:04:01', NULL, 64);
if (isset($country_iso))
$sql = "SELECT * FROM shop_shipping_rules WHERE country_iso IS NULL OR country_iso = '$country_iso' ";
else
$sql = "SELECT * FROM shop_shipping_rules WHERE country_iso IS NULL";

Updating the database from 1.5.1.3 to 1.5.3.1 OpenCart

I'm trying to pass data from my table OpenCart version 1.5.1.3 to version 1.5.3.1. I'll be using a new theme and only a few of the modules used in the previous version, but do not want to lose the data records of customers, sales and products.
Bearing this in mind I guess the easiest way would be estrair the table to a SQL script (I used phpmyadmin), but there are some differences between the versions for the database schema and data so I am not able to pass data from a table another.
As an example I'll use the table "address" 1.5.1.3 version that has the following structure when exported by phpmyadmin:
INSERT INTO `address` (`address_id`, `customer_id`, `firstname`, `lastname`, `company`, `address_1`, `address_2`, `city`, `postcode`, `country_id`, `zone_id`) VALUES
(6, 6, 'Fulano', 'Silva', '', 'My Street, 455', 'Neighborhood 1', 'City 1', 'd9c 5t7', 30, 464),
(2, 2, 'Cicrano', 'Souza', '', 'My Avenue, 921', 'Neighborhood 2', 'City 2', 'd9c 5t7', 30, 464),
(4, 4, 'Beltrano', 'Cabrito', '', 'My Street 2, 191', 'Neighborhood 3', 'City 3', 'd9c 3t7', 30, 464);
And the database version 1.5.3.1 follows this model:
INSERT INTO `address` (`address_id`, `customer_id`, `firstname`, `lastname`, `apelido`, `company`, `company_id`, `tax_id`, `address_1`, `numero`, `address_2`, `complemento`, `city`, `postcode`, `country_id`, `zone_id`) VALUES
(6, 6, 'Fulano', 'Silva', '', '', '', '', 'My Street', '455', 'Neighborhood 1', '', 'City 1', 'd9c 5t7', 30, 464),
(2, 2, 'Beltrano', 'Cabrito', '', '', '', '', '', 'My Street 2', '191', 'Neighborhood 3', '', 'City 3', 'd9c 3t7', 30, 464);
This is repeated in several tables, now how do I get only the data you want from v1.5.1.3 to v1.5.3.1 using SQL or other simpler if any?
This is why the upgrade script is there. You can find out how to upgrade here