I have a question about SQL Server transactions
I want to insert datas into Table_A and Table_B.
Table_B has a Table_A's key.
Table_B's records size (has Table_A's key) is dynamic.
[Table_A]
id: ,
title:
[Table_B]
id: ,
tableA_id:,
title:
My code work only the case insert data is static size.
like this
var tableBtitles = ['abc','def','ghi'] //this size is dynamic
const transaction = new sql.Transaction()
transaction.begin(err => {
const request = new sql.Request(transaction)
request.query("insert into tableA (title) output Inseted.Id values('a')" , (err, result) => {
const request = new sql.Request(transaction)
request.input('tableA_id',mssql.NVarChar,
result['recordset'][0]['id']);
request.input('title1',mssql.NVarChar,
tableBtitles[0]);
request.input('title2',mssql.NVarChar,
tableBtitles[1]);
request.input('title3',mssql.NVarChar,
tableBtitles[2]);
request.query('insert into tableB (tableA_id,title) values(#tableA_id,#title1),(#tableA_id,#title2),(#tableA_id,#title2)), (err, result) => {
transaction.commit(err => {
})
})
})
Please tell me how to do it.
try this syntax
insert into [Table_A] ([data])
output inserted.id, inserted.data into [Table_B]
--check here
https://stackoverflow.com/a/3712735/8543453
How to initialize sqlite db when using only angular.js and cordova without ionic?
Below is the sequence of existing app initialization.
$(function() {
new init();
});
var init = function() {
// Check platform - web browser, iOS, android
if (navigator.userAgent.match(/(iOS|iPad|iPhone|Android)/)) {
document.addEventListener("deviceready", onDeviceReady, false);
} else {
initializeApp('browser');
}
// Device Ready event for cordova
function onDeviceReady() {
initializeApp('device');
}
// Bootstrapping Angular manually.
function initializeApp(platform) {
angular.element(document).ready(function() {
angular.bootstrap(document, ['myApp']);
});
}
}
// Angular Module and configurations
var myApp = angular.module('myApp', ['ngAnimate','ui.router', 'myApp.constants', 'myApp.helper','myApp.directives', 'myApp.utils', 'myApp.services','ngSanitize', 'ui.bootstrap', 'myApp.filter','chart.js']);
Yes you can use .run method for creating db. here i attach my code for sqlite for web and cordova also so you can sqlite db in browser when you use ionic serve
var db = null;
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services','ngCordova','timer','ionic-datepicker','ionic-timepicker'])
.run(function($ionicPlatform,$cordovaSQLite,$ionicPopup)
{
$ionicPlatform.ready(function()
{
if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard)
{
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if (window.StatusBar)
{
StatusBar.styleLightContent();
}
if(window.cordova)
{
// App syntax
db = $cordovaSQLite.openDB("myapp.db");
}
else
{
// Ionic serve syntax
db = window.openDatabase("myapp.db", "1.0", "My app", -1);
}
$cordovaSQLite.execute(db,"CREATE TABLE IF NOT EXISTS Users ( UserId INTEGER NOT NULL UNIQUE PRIMARY KEY, CloudId INTEGER, Email TEXT NOT NULL UNIQUE, IsEmailVerified TEXT DEFAULT 'N' NOT NULL, FirstName TEXT NOT NULL, LastName TEXT NOT NULL, UserLanguage TEXT NOT NULL DEFAULT English, IsAutoSyncOn BOOL NOT NULL DEFAULT 1, DateUpdated TIMESTAMP DEFAULT CURRENT_TIMESTAMP, DateAdded TIMESTAMP DEFAULT CURRENT_TIMESTAMP, DateDeleted TIMESTAMP)");
$cordovaSQLite.execute(db,"CREATE TABLE IF NOT EXISTS Photos( PhotoId INTEGER NOT NULL UNIQUE PRIMARY KEY, Source TEXT 'Standard' NOT NULL, Category TEXT, ThumbnailURL TEXT NOT NULL, PhotoURL TEXT NOT NULL, DateAdded TIMESTAMP DEFAULT CURRENT_TIMESTAMP, DateUpdated TIMESTAMP DEFAULT CURRENT_TIMESTAMP, DateDeleted TIMESTAMP)");
$cordovaSQLite.execute(db,"CREATE TABLE IF NOT EXISTS Devices( DeviceId INTEGER NOT NULL UNIQUE PRIMARY KEY, UserId INTEGER NOT NULL, DbVersion INTEGER NOT NULL DEFAULT 1, DateLastLogin TIMESTAMP DEFAULT CURRENT_TIMESTAMP, NbrTimesSinceLastUpgradeNag INTEGER NOT NULL DEFAULT 1, DateLastSync TIMESTAMP, DateSyncServiceExpires TIMESTAMP, DateLastReviewRequest TIMESTAMPDEFAULT CURRENT_TIMESTAMP, NbrLogins INTEGER NOT NULL DEFAULT 1, DeviceName TEXT NOT NULL DEFAULT iPhone, DateUpdated TIMESTAMP DEFAULT CURRENT_TIMESTAMP, DateAdded TIMESTAMP DEFAULT CURRENT_TIMESTAMP)");
$cordovaSQLite.execute(db,"CREATE TABLE IF NOT EXISTS Events( EventId INTEGER NOT NULL UNIQUE PRIMARY KEY, CloudId INTEGER,UserId INTEGER NOT NULL, EventName TEXT NOT NULL, DateEvent TIMESTAMP, Category TEXT, ReminderIntervalNbr TEXT, ReminderIntervalUnits TEXT, RepeatInterval TEXT 'No Repeat' NOT NULL, PhotoId INTEGER, DateUpdated TIMESTAMP DEFAULT CURRENT_TIMESTAMP, DateAdded TIMESTAMP DEFAULT CURRENT_TIMESTAMP, DateDeleted TIMESTAMP)");
});
})
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.
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.
Am building a phonegap app and i need to save all contacts that are read with phonegap in device.
So How can I save all contacts that are read with phonegap in device on database app ?
I've tried this:
function onSuccess(contacts) {
if(contacts[i].phoneNumbers != null){
for (var j=0; j<contacts[i].phoneNumbers.length; j++){
var phonenumber = contacts[i].phoneNumbers[j].value;
phonenumber = phonenumber.replace(/[()-]/g,"");
var phonenumberFinal = phonenumber.replace(/ /g,'');
var contactosTodos = [nomeContacto, phonenumberFinal];
//for (var a=0; a<contactosTodos.length; a++){
dbase.transaction(function (tx){
tx.executeSql('DROP TABLE IF EXISTS CONTACTOS');
tx.executeSql("CREATE TABLE IF NOT EXISTS CONTACTOS (nome, numero);");
tx.executeSql("INSERT INTO CONTACTOS (name, number) VALUES (?, ?);", [ contactosTodos[j] , contactosTodos[j=+1] ]);
});
//}
}
}
}
}