I am working on a phonegap application and have the following code:
db = window.openDatabase("AjansDB", "1.0", "Ajans app DB", 2*1024*1024);
//creation
db.transaction(function (tx) {
var sql='CREATE TABLE IF NOT EXISTS USER_DATA (user_id INTEGER PRIMARY KEY, user_name VARCHAR(20))'
tx.executeSql(sql);
});
//insert
var sql="INSERT INTO USER_DATA (user_id,user_name) VALUES (?,?)";
tx.executeSql(sql,[1,"admin"], successCB, errorCB);
Now here is the problem: every time I select from the table I can retrieve the id but not the name ! The id would return "1" while the name would return "undefined"
db.transaction(function (tx) {
tx.executeSql('SELECT * FROM USER_DATA', [], function (tx, results) {
alert(results.rows.item(0).user_name);
});
});
What is going on wrong?
ps: I am not sure if this is gonna make any difference but I am using ionic and angular.js
I was so focused on sql errors that I missed a javascript syntax error. here it's:
var sql='CREATE TABLE IF NOT EXISTS USER_DATA (user_id INTEGER PRIMARY KEY, user_name VARCHAR(20))'
I missed a ";" .. a semicolon.
Related
I'm wondering if there is a way to do something like this:
string sql = "SELECT * FROM TABLE WHERE ID = #ID AND VALUE = #VALUE";
var listTest = await _dbConnection.QueryAsync<Example>(sql, ID, VALUE);
I want to query a table with a Composite PK.
var listTest = await _dbConnection.QueryAsync<Example>(sql, new { ID, VALUE });
let data = require('./../../config/db')
let connection = data.connection
let sequelize = data.sequelize
let sql = function(){
let clubMembers = connection.define('club_members',{
position:{
type :sequelize.ENUM('President','Treasurer') ,
allowNull : false
}
},
{
classMethods : {
associate : function(models){
let clubMembers = models.club_members
let club = models.club
let student = models.student
club.hasMany(clubMembers,{
foreignKey : "club_id"
})
student.hasMany(clubMembers,{
foreignKey : "student_id"
})
}
}
}
);
return clubMembers;
}
module.exports = sql;
I dont need id attribute in this table but sequelize automatically creates it . And combination of both the foreign keys should be treated as primary key i.e composite primary key .
You should use belongsToMany in the associate function
associate: function(models){
models.club.belongsToMany(models.student, { through: models.club_members, foreignKey: 'club_id' });
models.student.belongsToMany(models.club, { through: models.club_members, foreignKey: 'student_id' });
}
This would create the club_members table with composite primary key consisting of club_id and student_id, without the default id column.
> Am trying to provide login credentials with email and password using postgres database table.
postgres database tables. When i get records it should send 200 status
to my page.Am getting error on query. Kindly help me out how to use
select with where condition. Am missing some syntax.
getUser : function(req, res) {
var pg = require('pg');
var conString = process.env.DATABASE_URL || "postgres://test:test#localhost:5432/wallet";
var client = new pg.Client(conString);
client.connect();
console.log(req.query.email_id);
console.log(req.query.user_password);
var query = client.query("select * from pp_user_profile where email_id ="+req.query.email_id+ "and" + "user_password=" +req.query.password);
query.on("end", function (result) {
client.end();
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Success');
res.end();
});
},
Try the below syntax:
"select * from pp_user_profile where email_id = '"+req.query.email_id+"' and user_password= '"+req.query.password+"'";
Does anybody know, that how can I check an inserted record? I would like to create an intro page. When the user visits once, I wanna store it and check it.
$rootScope.insert = function(visited) {
var query = "INSERT INTO shoplist (visited) VALUES (?)";
$cordovaSQLite.execute(db, query, [visited]).then(function(res) {
console.log("INSERT ID -> " + res.insertId);
}, function (err) {
console.error(err);
});
}
$rootScope.insert(1);
Basically I would like to check that visited record is 0 or 1.
What should I do?
You can just write the query
Select from shoplist Where id = ?
Where id is the one your insert returned.
I have a 9800 simulator.
I mounted a directory as SDcard...
Now if i use
function populateDB(tx) {
tx.executeSql('DROP TABLE IF EXISTS DEMO');
tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');
}
function errorCB(err) {
alert("Error processing SQL: "+err.code);
}
function successCB() {
alert("success!");
}
var db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
db.transaction(populateDB, errorCB, successCB);
Then where will my database gets stored?
I needed to mount the SD card before installing the application and then i should execute my application...