-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get results in wide format #84
Comments
There is no 'conventional format'... I agree that it looks more logical, and easier to manipulate. But it also has inconvenients. Loading all the results in one array is rarely necessary... Why not just iterating over the prepared statement? Putting the rows of results in objects makes it impossible to access the fields in the order specified by the query, hides the columns with duplicate names, and encourages you to create a new string every time you want to access a field... But I understand that performance is not always important (especially when using Creating a |
Hello, I started using SQLite recently and met this tool ( Anyway I'll let the code here where helpful.
function select(query){
query = _db.exec(query);
query = query[0];
var queryObjects = [];
var keys = query.columns;
var values = query.values;
for(var i = 0; i < values.length; i++){
var valueObject = {};
for(var j = 0; j < keys.length; j++){
valueObject[keys[j]] = values[i][j];
}
queryObjects.push(valueObject);
}
return queryObjects;
} |
@williamd1k0 thank you very much for sharing this function. it's helpful! |
share my approach incase somebody need it too, db.exec('SELECT * FROM items; SELECT * FROM items WHERE id = "version";').map(
({ columns, values }) => {
return values.map(
(item) => {
return Object.assign(
{}, ...item.map(
(el, index) => {
return {
[columns[index]]: el
};
}
)
);
}
);
}
) OR db.exec('SELECT * FROM items; SELECT * FROM items WHERE id = "version";').map(
({ columns, values }) => values.map((item) =>
Object.assign(
{}, ...item.map(
(el, index) => {
return { [columns[index]]: el }
}
)
)
)
) OR db.exec('SELECT * FROM items; SELECT * FROM items WHERE id = "version";').map(({ columns, values }) => values.map((x) => Object.assign({}, ...x.map((v, i) => ({ [columns[i]]: v }))))) |
Is there an easy way to call
db.exec
and get the results in the conventional format:I am currently using:
But
push()
can be inefficient. It would be nice to have a shorthand for this.The text was updated successfully, but these errors were encountered: