Is it possible to insert a variable name into sqlite table as a column name? Questions is an array of objects. The goal is to grab the title property of each object and insert that as a column name.
db.serialize(() => {
db.run("CREATE TABLE assessmentTable (test TEXT)");
for(const q of questions){
db.run("ALTER TABLE assessmentTable ADD VALUES(?)", q.title);
}
});
I don't see a problem with that. Try replacing the quotes with back ticks so you can use the alter table statement as a template, then have it evaluate ${q.title} for column name. You may need to pass a definition for the table as well, not sure if that's required or not on sqlite3.
...
for(const q of questions){
db.run(`ALTER TABLE assessmentTable ADD ${q.title}`);
}
...
Related
I am trying to create a procedure that choose columns dynamically from two table. I have already hardcoded query which works fine.
#Existing query
create table mydb.result_table as
select
t1.id,
t1.name,
t1.place,
t1.product
t1.prize
t2.id,
t2.name,
t2.place,
t2.product,
t2.prize
from source_db.source_table t1
full outer join target_db_target_table t2 on
t1.id=t2.id and t1.name=t2.name
where t1.place<>t2.place
Now i need to create a procedure which basically select column dynamically from given table name and join both with given keys
so something like below
CREATE OR REPLACE PROCEDURE result(source_db varchar, source_table VARCHAR, targt_db varchar, target_table VARCHAR, key_join varchar<not sure can pass list>, filter_col varchar )
returns string not null
language javascript
as
$$
var query= `create table mydb.result_table as
select
t1.<all columns from source_db and source_table>
t2.<all columns from targt_db and target_table >
from <source_db and source_table> as t1
full outer join <target_db_target_table> as t2
on <key_join> where <t1.filter_col <> t2.filter_col>
`
return 'success';
$$;
i don't see example of selecting columns dynamically for this kind of use case.
Any solution to this?
You need to query the information schema for the list of columns in each table and use the result to dynamically build your SQL statement.
You also need to be aware of the same column name existing in multiple tables as you obviously can’t have the same column appearing multiple times in a table - so you’ll need to dynamically rename columns as appropriate
Just query the INFORMATION_SCHEMA.COLUMNS view to access the table's columns and then you can build your query dynamically:
https://docs.snowflake.com/en/sql-reference/info-schema/columns.html
I'm currently learning MySQL and have learned the INSERTO INTO statement and the INSERT INTO SELECT statement.
The INSERT INTO statement works like this:
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
While the INSERT INTO SELECT works like this:
INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;
What I'm trying to do is add new values while also adding values that I already have stored in another table.
con.query("INSERT INTO vehicles (vehicleType, vehicleModel, vehicleOwner, vehicleSpawnX, vehicleSpawnY, vehicleSpawnZ)")
From the query above, I already have the vehicleOwner value stored in another table, while the other ones I've just gotten.
How can I do this? Add a VALUES statement before SELECT?
I'm using SQL Workbench 8.0 and JavaScript. Also, all the values are NOT NULL, so I can't make two different queries, unless on the first one I add temporary values that I'll update on the second one.
What I want to replace:
vehicleType -> "players"
vehicleModel -> vehicleCreate.model
vehicleOwner -> playerID FROM players table
vehicleSpawnX -> pos.x
vehicleSpawnY -> pos.y
vehicleSpawnZ -> pos.z
Thanks!
It's not possible... But you can select data and store that on variables, then you will store it in another table
You would construct a query. Your data model is a bit hard to follow since you give no examples of the data or of what you really want to do.
So let me give a simpler example. Say, you have a table with two columns for two players and you want to put the ids into a table -- but using their names. The query would look like:
insert into pairs (playerId1, playerId2)
select p1.playerId, p2.playerId
from players p1 join
players p2
on p1.name = ? and p2.name = ?;
The ? are for parameter placeholders. I assume you know to aways use parameters when passing values into a query.
I use datatables 1.10 for a while now, and I have come to a point in my development where I have a table in my db that stores a json object. Furthermore that table also contains an attribute called groupName. As the name suggests, the entries of that table are "grouped" by that attribute, and in that group, the json object has the same attributes.
Here an example of how my database table looks like:
+---------------------------------------------------------------------------+
|id|groupname |json |
+---------------------------------------------------------------------------+
|01|group1 |{"key1":"value1","key2":"value2"} |
|02|group1 |{"key1":"foo","key2":"bar"} |
|03|group2 |{"kex1":"foo1","keyab":"bar2","key1":"foo"}|
+---------------------------------------------------------------------------+
My page that contains the datatabase, and also has a dropdown menu populated with the group names which filters the the datatable for the particular group. The jsonObject of that filtered group is being displayed as a raw json string.
What I'm trying to do now is to parse my jsonObject, get the keys and populate them in the datatable as columns. The values of the jsonObject are displayed in the columns accordingly.
Where or how do I reinitialize the datatable, providing it with the proper column names?
Can you please share some example of your JSON object.
Thanks!
Any ways what you can do is to to take the keys of the json object and put them into array then using loop you can make a string of columns. Then you can provide an id to of the table and append the string into the table.
let keys = Object.keys(YOUR_JSON_OBJECT);
let text = "";
for(let i =0;i<keys.length;i++){
text+= ["<tr>","<td>",YOUR_JSON_OBJECT.its_key,"</td>","</tr>"].join("");
}
$('table thead').html(text);
See if this works!
I've a table of unknown data (some columns and rows ) ,I'm at the point where i require to use the function table.updateData() which requires column id to be existing within the data construction which i can't guarantee as the data are pulled from an unknown source .
is there's a way around this or is there's any alternative way of updating the data later ?
p.s. i only use vanilla javascript not jquery
There needs to be a unique index set on each row of the data in order for Tabulator to know which row you want to refer to.
You can set this to any column field in the row data using the index option in the table constructor
var table = new Tabulator("#example-table", {
index:"age", //set the index field to the "age" field.
});
By default this is set to the id filed
If you want to set this locally you can use a mutator to create a value in this field for you:
//define variable to count index's to ensure they are unique
var index = 0;
//define custom mutator
var idMutator = function(value, data, type, params, component){
//value - original value of the cell
//data - the data for the row
//type - the type of mutation occurring (data|edit)
//params - the mutatorParams object from the column definition
//component - when the "type" argument is "edit", this contains the cell component for the edited cell, otherwise it is the column component for the column
return index++; //return the index for the row.
}
//column definition
{field:"id", mutator:idMutator, visible:false}
However if you are updating the data from a remote source then there is no wat to tie that back to the data in your table.
It is standard practice to include an index or unique identifier in the data of this sort to allow syncing between client and server side
I want to filter data on the basis of number of rows and columns provided by user.
I am getting the data on controller after reading this excel file:
this is the column header values which I am getting after user has passed 5 as input columns:
This is the row data which I am getting after user has given 4 rows as input to be rendered, here there is data of 4 rows:
Inside each row, there is key value pair containing column name and the cell value:
I want to filter this row data on the basis of the header names which are coming through column so that the row has only the values till the column is defined. Currently I am getting entire row values.
I have written a logic like this but this is not working.
var rowData=[];
for(var i=0;i<headerNames.length;i++){//headerNames contains column names
for(var j=0;j<data.length;j++){//data contains the rows data
for(var keyName in data[j])
if(angular.equals(keyName,headerNames[i])){
rowData.push({'keyName':data[j][keyName]})//I want only the key,values which matches the column names. I want to set the keyName value as array key but I am not getting its value instead it is coming like keyName:18
}
}
}
}
Adding the plunker for the code.
http://plnkr.co/edit/28Z44xDBug7nFCQnKZJL?p=preview
I am able to filter the data on UI and get only the row and column data as per user input.
But i need the same data on controller, so that i can save it in MongoDb. I want to filter the row data as per the column input. So, that i only get the row data till the column is defined.
Please suggest how can i filter the data and can splice the row values so that in row i can have value upto the column numbers defined. For eg, if I user have entered 4 rows and 5 columns then in my row Data i can have only values upto 5th column and all other values i can remove from array. in my code currently i am not able to get the key value to be set as array key.
Please help me to resolve this problem.
Looks like you're most of the way there. You just need to assign rowData to be $scope.rowData so that angular can get to it, and have the Angular document show {{rowdata}} (shows $scope.rowData).
You'll probably need to arrange it into columns though so you might do a ng-repeat on rowData, with variable=row, and in TR TD tags show row.1
eg something like ..
<tr ng-repeat="row in rowData">
<td>{{row.1}}</td>
<td>{{row.2}}</td>
</tr>