Sqlite not INSERTING dynamic JSON data - sqlite

I am using Ionic v1 and trying to INSERT a few JSON records into a SQLite database on Android Kitkat. If I use a static variable it works but only inserts first item in array, but if I try to make the variable names dynamic I get an error:
Cannot read property 'mac_id' of undefined
I have tried the two following code segments, first works but only inserts the first item in the JSON array, second does not working:
Works:
But only inserts first item (multiple times)
for ( var i = 0; i < data.length; i++)
{
var mac_id = data[i].mac_id;
var device_type = data[i].device_type;
var machine_id = data[i].machine_id;
var machine_name = data[i].machine_name;
var date_created = data[i].date_created;
db.transaction(function (tx) {
var query = "INSERT INTO devices (mac_id, device_type,machine_id,machine_name,date_created) VALUES (?,?,?,?,?)";
tx.executeSql(query, [mac_id , device_type , machine_id , machine_name , date_created ], function(tx, res) {
},
function(tx, error) {
console.log(' device INSERT error: ' + error.message);
});
}, function(error) {
console.log(' device transaction error: ' + error.message);
}, function() {
console.log('device INSERT ok');
});
}
Not Working
for ( var i = 0; i < data.length; i++)
{
db.transaction(function (tx) {
var query = "INSERT INTO devices (mac_id, device_type,machine_id,machine_name,date_created) VALUES (?,?,?,?,?)";
tx.executeSql(query, [data[i].mac_id, data[i].device_type, data[i].machine_id, data[i].machine_name, data[i].date_created ], function(tx, res) {
},
function(tx, error) {
console.log(' device INSERT error: ' + error.message);
});
}, function(error) {
console.log(' device transaction error: ' + error.message);
}, function() {
console.log('device INSERT ok');
});
}
JSON array
[
{
"id": "3",
"mac_id": "fsdf324324",
"device_type": "redvfsdfds",
"machine_id": "3",
"machine_name": "sdfsdfsdf",
"date_created": "3322342"
},
{
"id": "2",
"mac_id": "243434",
"device_type": "fredssd",
"machine_id": "2",
"machine_name": "fdsfsdf",
"date_created": "43434"
},
{
"id": "1",
"mac_id": "1324324234",
"device_type": "bweight",
"machine_id": "1",
"machine_name": "dffdgf",
"date_created": "4324234"
}
]

db.transaction(function (tx) {
for ( var i = 0; i < $scope.data.length; i++)
{
var query = "INSERT INTO devices (mac_id, device_type,machine_id,machine_name,date_created) VALUES (?,?,?,?,?)";
tx.executeSql(query, [data[i].mac_id, data[i].device_type, data[i].machine_id, data[i].machine_name, data[i].date_created ], function(tx, res) {
},function(tx, error) {
console.log(' device INSERT error: ' + error.message);
});
}
}, function(error) {
console.log(' device transaction error: ' + error.message);
}, function() {
console.log('device INSERT ok');
});
You should use for loop inside transaction . Use it like this . hope this helps :)

Related

DynamoDB table seed works in cli but not AWS-SDK

I have a table that has more than 25 items and wrote a basic script to break them into sub arrays of 25 items each then loops thru that collection of sub arrays to run a batch write item command in the AWS DynamoDB Client. The issue I am getting is a returned validation error. When I run the same seed file via the aws-cli it seeds the table perfectly. This makes me think it has something to do with my script. See anything I am missing? Thanks in advance!
var { DynamoDB } = require('aws-sdk');
var db = new DynamoDB.DocumentClient({
region: 'localhost',
endpoint: 'http://localhost:8000',
});
const allItems = require('./allItems.json');
const tableName = 'some-table-name';
console.log({ tableName, allItems });
var batches = [];
var currentBatch = [];
var count = 0;
for (let i = 0; i < allItems.length; i++) {
//push item to the current batch
count++;
currentBatch.push(allItems[i]);
if (count === 25) {
batches.push(currentBatch);
currentBatch = [];
}
}
//if there are still items left in the curr batch, add to the collection of batches
if (currentBatch.length > 0 && currentBatch.length !== 25) {
batches.push(currentBatch);
}
var completedRequests = 0;
var errors = false;
//request handler for DynamoDB
function requestHandler(err, data) {
console.log('In the request handler...');
return function (err, data) {
completedRequests++;
errors = errors ? true : err;
//log error
if (errors) {
console.error('Request caused a DB error.');
console.error('ERROR: ' + err);
console.error(JSON.stringify(err, null, 2));
} else {
var res = {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Methods': 'GET,POST,OPTIONS',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
},
body: JSON.stringify(data),
isBase64Encoded: false,
};
console.log(`Success: returned ${data}`);
return res;
}
if (completedRequests == batches.length) {
return errors;
}
};
}
//Make request
var params;
for (let j = 0; j < batches.length; j++) {
//items go in params.RequestedItems.id array
//format for the items is {PutRequest : {Item: ITEM_OBJECT}}
params = '{"RequestItems": {"' + tableName + '": []}}';
params = JSON.parse(params);
params.RequestItems[tableName] = batches[j];
console.log('before db.batchWriteItem: ', params);
try {
//send to db
db.batchWrite(params, requestHandler(params));
} catch{
console.error(err)
}
}
Here is the formatted request object and the error:
before db.batchWriteItem:
{ RequestItems:
{ 'some-table-name': [ [Object], [Object], [Object], [Object] ] }
}
In the request handler...
Request caused a DB error.
ERROR: ValidationException: Invalid attribute value type
{
"message": "Invalid attribute value type",
"code": "ValidationException",
"time": "2020-08-04T10:51:13.751Z",
"requestId": "dd49628c-6ee9-4275-9349-6edca29636fd",
"statusCode": 400,
"retryable": false,
"retryDelay": 47.94198279972915
}
You are using the DocumentClient in the nodejs code. This will automatically convert the data format used by DynamoDB to a more easily consumable format.
e.g.
{
"id": {
"S": "A string value"
}
}
would become
{
"id": "A string value"
}
The CLI does not perform this data conversion.
You can use the regular DynamoDB client to not perform this conversion in Nodejs. e.g. const db = new Dynamodb()

Can't delete SQLite database using Qt with QML transaction

Trying to use localstorage example in Qt 5.14, the database is locked and can't be deleted.
on Qt documentation it's saying:
"Database connections are automatically closed during Javascript garbage collection."
but that is not the case...
function dbInit()
{
var db = LocalStorage.openDatabaseSync("Activity_Tracker_DB", "", "Track exercise", 1000000)
try {
db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS trip_log (date text,trip_desc text,distance numeric)')
})
} catch (err) {
console.log("Error creating table in database: " + err)
};
}
function dbGetHandle()
{
try {
var db = LocalStorage.openDatabaseSync("Activity_Tracker_DB", "",
"Track exercise", 1000000)
} catch (err) {
console.log("Error opening database: " + err)
}
return db
}
function dbInsert(Pdate, Pdesc, Pdistance)
{
var db = dbGetHandle()
var rowid = 0;
db.transaction(function (tx) {
tx.executeSql('INSERT INTO trip_log VALUES(?, ?, ?)',
[Pdate, Pdesc, Pdistance])
var result = tx.executeSql('SELECT last_insert_rowid()')
rowid = result.insertId
})
return rowid;
}
function dbReadAll()
{
var db = dbGetHandle()
db.transaction(function (tx) {var results = tx.executeSql(
'SELECT rowid,date,trip_desc,distance FROM trip_log order by rowid desc')
for (var i = 0; i < results.rows.length; i++) {
listModel.append({
id: results.rows.item(i).rowid,
checked: " ",
date: results.rows.item(i).date,
trip_desc: results.rows.item(i).trip_desc,
distance: results.rows.item(i).distance
})
}
})
}
How can I unlock/close the database?

Google Apps Scripts - How to get key value of document from Firestore?

I'm trying to get the key values of the documents in my Firestore database, but I'm not getting it.
This value below:
This my code:
function objectsToArray(objects) {
var outputArray = [];
for (var i in objects){
outputArray.push([
objects[i].fields.id, objects[i].fields.data, objects[i].fields.acao,
objects[i].fields.categoria, objects[i].fields.movimentos, objects[i].fields.descricao
]);
}
return outputArray;
}
My output Logger.log of JSON:
[20-01-11 19:42:06:370 CET] [{"name":"projects/orcamento- b37bb/databases/(default)/documents/orcamento/0MwgqEm9abho3bpB5yCc","fields":
{"categoria":"SUPERMERCADO","data":"2019-07- 31T00:00:00.000Z","descricao":"","acao":"Despesa","movimentos":23.82,"id":107},
"createTime":"2019-12-31T14:35:47.959299Z","updateTime":"2019-12- 31T14:35:47.959299Z"},
any suggestions?
Thanks
The original data seems to be structured like this:
[
{
"name":"projects/orcamento- b37bb/databases/(default)/documents/orcamento/0MwgqEm9abho3bpB5yCc",
"fields": {
"categoria":"SUPERMERCADO",
"data":"2019-07- 31T00:00:00.000Z",
"descricao":"",
"acao":"Despesa",
"movimentos":23.82,
"id":107
},
"createTime":"2019-12-31T14:35:47.959299Z",
"updateTime":"2019-12- 31T14:35:47.959299Z"
},
You want the value: 0MwgqEm9abho3bpB5yCc
Which is in the element with the name property key.
function objectsToArray(objects) {
var L,nameValue,finalValue;
objects = [
{
"name":"projects/orcamento- b37bb/databases/(default)/documents/orcamento/0MwgqEm9abho3bpB5yCc",
"fields": {
"categoria":"SUPERMERCADO",
"data":"2019-07- 31T00:00:00.000Z",
"descricao":"",
"acao":"Despesa",
"movimentos":23.82,
"id":107
},
"createTime":"2019-12-31T14:35:47.959299Z",
"updateTime":"2019-12- 31T14:35:47.959299Z"
},
]
var outputArray = [];
L = objects.length;
for (var i=0;i<L;i++){
nameValue = objects[i].name;
Logger.log('nameValue: ' + nameValue)
finalValue = nameValue.slice(nameValue.lastIndexOf("/")+1);
Logger.log('finalValue: ' + finalValue)
outputArray.push(finalValue);
}
Logger.log('outputArray: ' + JSON.stringify(outputArray))
return outputArray;
}

InvalidParameterType: Expected params.ExpressionAttributeValues[':et1'].N to be a string

Here is my code that I'm using for making queries:
var scanParams = {
TableName : 'xxxx',
FilterExpression : '( (event = :e0) AND (event = :e1 AND eventTime > :et1 AND eventTime < :et2) )',
ExpressionAttributeValues: {
':e0': { S: 'ME 21' },
':e1': { S: 'ME 21' },
':et1': { N: 1509267218 },
':et2': { N: 1509353618 }
},
ProjectionExpression: "event, customer_id, visitor",
};
In configuration of the respective dynamodb table it seems like I've added Nummber for eventTime column.
Here is the error:
error happened { MultipleValidationErrors: There were 2 validation errors:
* InvalidParameterType: Expected params.ExpressionAttributeValues[':et1'].N to be a string
* InvalidParameterType: Expected params.ExpressionAttributeValues[':et2'].N to be a string
at ParamValidator.validate (/home/jahidul/workspace/backstage-opticon/node_modules/aws-sdk/lib/param_validator.js:40:28)
at Request.VALIDATE_PARAMETERS (/home/jahidul/workspace/backstage-opticon/node_modules/aws-sdk/lib/event_listeners.js:125:42)
at Request.callListeners (/home/jahidul/workspace/backstage-opticon/node_modules/aws-sdk/lib/sequential_executor.js:105:20)
at callNextListener (/home/jahidul/workspace/backstage-opticon/node_modules/aws-sdk/lib/sequential_executor.js:95:12)
at /home/jahidul/workspace/backstage-opticon/node_modules/aws-sdk/lib/event_listeners.js:85:9
at finish (/home/jahidul/workspace/backstage-opticon/node_modules/aws-sdk/lib/config.js:315:7)
at /home/jahidul/workspace/backstage-opticon/node_modules/aws-sdk/lib/config.js:333:9
at SharedIniFileCredentials.get (/home/jahidul/workspace/backstage-opticon/node_modules/aws-sdk/lib/credentials.js:126:7)
at getAsyncCredentials (/home/jahidul/workspace/backstage-opticon/node_modules/aws-sdk/lib/config.js:327:24)
at Config.getCredentials (/home/jahidul/workspace/backstage-opticon/node_modules/aws-sdk/lib/config.js:347:9)
at Request.VALIDATE_CREDENTIALS (/home/jahidul/workspace/backstage-opticon/node_modules/aws-sdk/lib/event_listeners.js:80:26)
at Request.callListeners (/home/jahidul/workspace/backstage-opticon/node_modules/aws-sdk/lib/sequential_executor.js:101:18)
at Request.emit (/home/jahidul/workspace/backstage-opticon/node_modules/aws-sdk/lib/sequential_executor.js:77:10)
at Request.emit (/home/jahidul/workspace/backstage-opticon/node_modules/aws-sdk/lib/request.js:683:14)
at Request.transition (/home/jahidul/workspace/backstage-opticon/node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (/home/jahidul/workspace/backstage-opticon/node_modules/aws-sdk/lib/state_machine.js:14:12)
at Request.runTo (/home/jahidul/workspace/backstage-opticon/node_modules/aws-sdk/lib/request.js:403:15)
at /home/jahidul/workspace/backstage-opticon/node_modules/aws-sdk/lib/request.js:791:12
at Request.promise (/home/jahidul/workspace/backstage-opticon/node_modules/aws-sdk/lib/request.js:777:12)
at DynamoDBService.scanItem (/home/jahidul/workspace/backstage-opticon/shared/services/dynamodb/dynamodb.service.ts:52:39)
at /home/jahidul/workspace/backstage-opticon/job-scripts/dyno-test.js:57:12
at sailsReady (/home/jahidul/workspace/backstage-opticon/node_modules/sails/lib/app/lift.js:49:12)
at /home/jahidul/workspace/backstage-opticon/node_modules/async/lib/async.js:251:17
at /home/jahidul/workspace/backstage-opticon/node_modules/async/lib/async.js:154:25
at /home/jahidul/workspace/backstage-opticon/node_modules/async/lib/async.js:248:21
at /home/jahidul/workspace/backstage-opticon/node_modules/async/lib/async.js:612:34
Any idea? Thanks in advance.
Use the parameter below:
var scanParams = {
TableName : 'xxxx',
FilterExpression : '( (event = :e0) AND (event = :e1 AND eventTime > :et1 AND eventTime < :et2) )',
ExpressionAttributeValues: {
':e0': { "S": "ME 21" },
':e1': { "S": "ME 21" },
':et1': { "N": "1509267218" },
':et2': { "N": "1509353618" }
},
ProjectionExpression: "event, customer_id, visitor",
};
In Dynamo DB, the type (Number) is represented by "N" and the value must be in string format "1509353618". Hope this will resolve your problem.
Actually, you don't need to include the data type for et1 and et2 variables. The DynamoDB API should be able to automatically interpret it as NUMBER. Similarly, for variables e0 and e1.
Please try the below code.
var scanParams = {
TableName : 'xxxx',
FilterExpression : '( (event = :e0) AND (event = :e1 AND eventTime > :et1 AND eventTime < :et2) )',
ExpressionAttributeValues: {
':e0': 'ME 21',
':e1': 'ME 21',
':et1': 1509267218,
':et2': 1509353618
},
ProjectionExpression: "event, customer_id, visitor",
};
Full Tested Code:-
You may need to change the table name and key attribute names in the below code.
var AWS = require("aws-sdk");
var creds = new AWS.Credentials('akid', 'secret', 'session');
AWS.config.update({
region: "us-west-2",
endpoint: "http://localhost:8000",
credentials: creds
});
var docClient = new AWS.DynamoDB.DocumentClient();
var params = {
TableName: "table4",
FilterExpression: "userid = :user_id1 AND (userid = :user_id2 AND ts > :et1 AND ts < :et2)",
ExpressionAttributeValues: { ":user_id1": 'ME 21',
":user_id2": 'ME 21',
":et1" : '1509267216',
":et2" : 1509353618,
}
};
docClient.scan(params, onScan);
var count = 0;
function onScan(err, data) {
if (err) {
console.error("Unable to scan the table. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("Scan succeeded.");
data.Items.forEach(function (itemdata) {
console.log("Item :", ++count, JSON.stringify(itemdata));
});
// continue scanning if we have more items
if (typeof data.LastEvaluatedKey != "undefined") {
console.log("Scanning for more...");
params.ExclusiveStartKey = data.LastEvaluatedKey;
docClient.scan(params, onScan);
}
}
}

send data from phone to server

I am trying to develop an android mobile application using the PhoneGap framework, and I want to synchronize my table of local database(of my phone) to the server database.
this is my code, but this code allow me to send only one line of table how can i send all line of table.
$.ajax({
type: 'POST',
data: col1+'&lid='+col2,
url: 'http://your-domain.com/comments/save.php',
success: function(data){
console.log(data);
alert('Your data was successfully added');
},
error: function(){
console.log(data);
alert('There was an error adding your data');
}
});
Try to select the data from the localDatabase and send it row by row, here's an example:
db.transaction(function(tx) {
Squery = 'SELECT * FROM news WHERE category_id ='+lid;
tx.executeSql(Squery,
null,
function(tx, results)
{
for(i=0; i<results.rows. length; i++){
row = results.rows.item(0);
$.ajax({ -- your code using row['name_of_column'] -- })
}
},
console.log('error')
});});
sorry for my miss understoods...
If you are using phonegap i guess you are using the Storage feature: http://docs.phonegap.com/en/2.2.0/cordova_storage_storage.md.html#Storage
folowing one of theirs examples, you can do something like:
function queryDB(tx) {
tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);
}
function querySuccess(tx, results) {
//do you ajax request....
...
data: {
rows : results.rows
}
...
}
function errorCB(err) {
alert("Error processing SQL: "+err.code);
}
var db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
db.transaction(queryDB, errorCB);
other option is to send a simple array...
function querySuccess(tx, results) {
var myRowsIds = [];
var len = results.rows.length;
for (var i=0; i<len; i++){
myRowsIds .push( results.rows.item(i).id )
}
//do you ajax request....
...
data: {
rows : myRowsIds
}
...
}
hope it helps!

Resources