Where Clause for ionic SQLite Plugin - sqlite

I'm using Sqlite plugin in my ionic 3 app.
I have saved and fetched data successfully but when I use Where Clause for fetching data against specific ID using following Query:
const query = "SELECT * FROM " + `${table}` + " WHERE project_id=" + `${project_id}`;
this.database.executeSql(query, []).then((result) => {
console.log('RESULT: '+ JSON.stringify(result));
}, err => {
console.error('Error: '+ err);
});
What am I doing wrong?

Change your query as follows and you will get the response.
const query = "SELECT * FROM " + `${table}` + " WHERE project_id=?";
this.database.executeSql(query, [project_id]).then((result) => {
console.log('RESULT: '+ JSON.stringify(result));
}, err => {
console.error('Error: '+ err);
});

Related

Small issue in chat system

window.onload = function () {
const user_name =
NAME_ARRAY[Math.round(Math.random() * NAME_ARRAY.length)];
document.getElementById("userName_profile").innerHTML = user_name;
document.getElementById("userName").innerHTML = user_name;
const btn = document.querySelector("#sendMessage");
const textfield = document.querySelector("#messageInput");
const log = document.querySelector("#messageArea");
// +++++++++++++++ For chat messaging system ++++++++++++
NAF.connection.subscribeToDataChannel(
"chat",
(senderId, dataType, data, targetId) => {
console.log("msg", data, "from", senderId, targetId);
messageArea.innerHTML += senderId + ": " + data.txt + "<br>";
}
);
btn.addEventListener("click", (evt) => {
NAF.clientId = user_name;
messageArea.innerHTML +=
NAF.clientId + ": " + textfield.value + "<br>";
NAF.connection.broadcastData("chat", { txt: textfield.value });
document.getElementById("messageInput").value = "";
// NAF.easy = user_name;
});
}
When I've made chat system and working fine. The only, issue is that I want a user name rather than a client id. Please, help with this.

How to Delete Data React Native SQLite

I'm facing some problem and I'm still new with this react native and SQLite thing but how do I delete a data from database if the create method is like this?
simpanCatatan = async () => {
const { navigation } = this.props
const date = new Date()
const dateNow = date.getDate() + " " + bulan[date.getMonth()] + " " + date.getFullYear()
await Catatan.create({
title: this.state.title,
value: this.state.catatan,
user_id: this.state.user_id,
video_id: this.state.idVideo,
video_name: this.state.namaVideo,
materi_id: this.state.idMateri,
created_at:dateNow
})
navigation.goBack()
navigation.state.params.getAllCatatan()
}

React native sqlite how to render data (type JSON) from database on Flatlist?

Can anyone help me? My database has more field like this one:
[
{
"key":"word_1",
"Word":"a",
"Meaning":"a"
},
...
]
I already read it from database but how can I store it to state and render each element on Flatlist.
Here is way i get data from Database:
db.transaction(tx => {
var sql =
"SELECT * FROM tblVietAnh WHERE word LIKE '" +
this.state._inputValue.toLowerCase() +
"%' LIMIT 5";
tx.executeSql(sql, [], (tx, results) => {
var len = results.rows.length;
setState({ _records: results.row.item });
console.log(this.state._records);
for (let i = 0; i {
_records: prevState._records.push(row.id, row.word, row.meaning);
});
}
});
});
But by this way i just have an array with separate row. So I cant access to each element. How can I fix this ?
Thank you!

Meteor Email Attachments

I've updated my meteor to 1.2, and I'm now trying to use the email attachment feature, but not sure how to.
Meteor's guide says refer to this, but it's not very helpful..
if(true == true){
var dataAttachments = attachment;
var dataText =
"Client Name: " + name +
"\rEmail: " + email +
"\rPhone: " + phone +
"\rCompany: " + company +
"\rDeliverables: " + deliverables +
"\rCopywriting: " + copywriting +
"\rPrint Services: " + print +
"\rIllustration: " + illustration +
"\rphotography: " + photography +
"\rTimelines: " + timelines +
"\rBudget: " + budget +
"\rDescription: " + description;
Meteor.call('sendEmail', dataText, dataAttachment);
//throwAlert is my helper method which creates popup with message
alert('Email sent');
}else{
alert('An error occurred. Sorry');
return false;
}
}
});
and
Meteor.methods({
sendEmail: function (text) {
check([text], [String]);
this.unblock();
Email.send({
to: 'jaeeun#antarcti.cc',
from: 'contact#myClientProject.com',
subject: 'New message from contact form',
text: text
});
Email.send().addAttachment(attachment);
}
});
I would suggest installing this package: https://atmospherejs.com/ashutosh/email-att
Then do:
var attachments = [];
attachments.push({filename: "xxx", filePath: "xxx"});
var email = {
from: "test#gmail.com",
to: "test2#gmail.com",
subject: "Test!",
text: "Text!"
attachmentOptions: attachments
};
Meteor.call('send_one_email_with_attachments', email, function(){});
Meteor.methods({
send_one_email_with_attachments: function(email){
this.unblock();
EmailAtt.send(email);
};
});
This made my life a lot easier after I fought Meteor's built-in email for a while. It even works side-by-side, so you can still use your old non-attachment email functions!

Cordova Android SQLLIteplugin.open exception

I am trying to use SQLLItePlugin for Android but its not working. I will list my steps:
1. I have installed cordova pjhonegap from phonegap. I am developing my mobile app Phonegap, html5, javascript, css3 using Netbeans as IDE.
2. Downloaded plugin from https://github.com/brodysoft/Cordova-SQLitePlugin.
3. Added SQLitePlugin.js to js folder of project.
4. Added com.brodysoft.sqlitePlugin.file=https://github.com/brodysoft/Cordova-SQLitePlugin.git in plugin.properties.
5. Am opening database on deviceready as
var app = {
initialize: function () {
this.bindEvents();
},
bindEvents: function () {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function () {
app.receivedEvent('deviceready');
var db = window.sqlitePlugin.openDatabase('gdata.db');
console.log('ready');
db.transaction(function (tx) {
tx.executeSql('DROP TABLE IF EXISTS test_table');
tx.executeSql('CREATE TABLE IF NOT EXISTS test_table (id integer primary key, data text, data_num integer)');
// demonstrate PRAGMA:
db.executeSql("pragma table_info (test_table);", [], function (res) {
console.log("PRAGMA res: " + JSON.stringify(res));
});
tx.executeSql("INSERT INTO test_table (data, data_num) VALUES (?,?)", ["test", 100], function (tx, res) {
console.log("insertId: " + res.insertId + " -- probably 1");
console.log("rowsAffected: " + res.rowsAffected + " -- should be 1");
db.transaction(function (tx) {
tx.executeSql("select count(id) as cnt from test_table;", [], function (tx, res) {
console.log("res.rows.length: " + res.rows.length + " -- should be 1");
console.log("res.rows.item(0).cnt: " + res.rows.item(0).cnt + " -- should be 1");
});
});
}, function (e) {
console.log("ERROR: " + e.message);
});
});
},
// Update DOM on a Received Event
receivedEvent: function (id) {
var parentElement = document.getElementById(id);
console.log('Received Event: ' + id);
}
};
app.initialize();
Running the build on android devvice directly.
It keeps on throwing the error
Uncaught TypeError: Object # has no method 'exec'
(13:52:13:450 | error, javascript)
at SQLitePlugin.open (www/js/libs/SQLitePlugin.js:112:15)
at SQLitePlugin (www/js/libs/SQLitePlugin.js:54:10)
at (anonymous function) (www/js/libs/SQLitePlugin.js:425:14)
at (anonymous function) (www/js/libs/SQLitePlugin.js:30:20)
at createandpopulatedb (www/js/dborarray.js:30:30)
at onDeviceReady3 (www/dborarray.html:96:33)
at onload (www/dborarray.html:16:155) SQLitePlugin openargs: {"name":"gdataenter code here.db"} (13:52:19:609) at
www/js/libs/SQLitePlugin.js:39
Can somebody help.
try thhis
window.sqlitePlugin.openDatabase({name: "gdata.db"});
instead of this
window.sqlitePlugin.openDatabase('gdata.db');

Resources