Comments (13)
Should be pretty simple to leverage the ExpandoObject
from sqlite-net.
Why did you close this issue @tofutim ?
@praeclarum Can you consider this? Maybe using dynamics?
from sqlite-net.
I added the following to my version of SQLite. If anyone has any comments on improvements or gotchas, they are much appreciated. To SQLiteCommand:
public List<Dictionary<string, object>> ExecuteQuery()
{
return ExecuteDeferredQuery().ToList();
}
public IEnumerable<Dictionary<string, object>> ExecuteDeferredQuery()
{
if (_conn.Trace)
{
Debug.WriteLine("Executing Query: " + this);
}
var stmt = Prepare();
try
{
var cols = new string[SQLite3.ColumnCount (stmt)];
for (int i = 0; i < cols.Length; i++) {
var name = SQLite3.ColumnName16 (stmt, i);
cols [i] = name;
}
while (SQLite3.Step(stmt) == SQLite3.Result.Row)
{
var obj = new Dictionary<string, object>();
for (int i = 0; i < cols.Length; i++)
{
var colType = SQLite3.ColumnType(stmt, i);
Type targetType;
switch (colType)
{
case SQLite3.ColType.Text:
targetType = typeof(string);
break;
case SQLite3.ColType.Integer:
targetType = typeof(int);
break;
case SQLite3.ColType.Float:
targetType = typeof(double);
break;
default:
targetType = typeof(object);
break;
}
var val = ReadCol(stmt, i, colType, targetType);
obj.Add(cols[i], val);
}
OnInstanceCreated(obj);
yield return obj;
}
}
finally
{
SQLite3.Finalize(stmt);
}
}
from sqlite-net.
Sorry if this seems ignorant but I'm new to c#... how would you call this function, currently I am using:
var results = db.Query<Dictionary<string, object>>(query, query_params);
And it only calls this function not yours:
public List<T> ExecuteQuery<T> () where T : new()
{
return ExecuteDeferredQuery<T>(_conn.GetMapping(typeof(T))).ToList();
}
from sqlite-net.
Just an update, to work around this I made this function to call your functions:
public List<Dictionary<string, object>> Query2(string query, params object[] args)
{
var cmd = CreateCommand(query, args);
return cmd.ExecuteQuery();
}
and call it with
var results = db.Query2(query, query_params);
maybe there is a better way to do this?
from sqlite-net.
This was exactly what I was looking for! Created a new issue #255 before I came across this issue. If you have been using this code without issue for while maybe you could use your post as a pull request to get it into master?
from sqlite-net.
Hi @tofutim. Thanks you for the great code
from sqlite-net.
Is there anyone that can get this into a new NuGet release?
from sqlite-net.
Yes this does seem like a reasonable feature. I think I would prefer dynamic over dictionary though.
from sqlite-net.
@tofutim, I love you, man!
Putting the code in a partial SQLiteCommand implementation safely extends the class without the risk of loosing the changes after the sqlite.net module update/reinstall.
from sqlite-net.
how about this? if no problem, can you add to master branch?
public partial class SQLiteConnection
{
public List<ExpandoObject> Query(string query, params object[] args)
{
var cmd = CreateCommand (query, args);
return cmd.ExecuteQuery();
}
}
public partial class SQLiteCommand
{
public List<ExpandoObject> ExecuteQuery() {
return ExecuteDeferredQuery().ToList();
}
private IEnumerable<ExpandoObject> ExecuteDeferredQuery () {
if (_conn.Trace) {
_conn.Tracer?.Invoke ("Executing Query: " + this);
}
var stmt = Prepare ();
try {
var cols = new string[SQLite3.ColumnCount (stmt)];
for (var i = 0; i < cols.Length; i++) {
var name = SQLite3.ColumnName16 (stmt, i);
cols[i] = name;
}
while (SQLite3.Step (stmt) == SQLite3.Result.Row) {
var obj = new ExpandoObject() as IDictionary<string, object>;
for (var i = 0; i < cols.Length; i++) {
var colType = SQLite3.ColumnType (stmt, i);
var val = ReadCol (stmt, i, colType, MapToCSharpType(colType));
obj[cols[i]] = val;
}
OnInstanceCreated (obj);
yield return (ExpandoObject) obj;
}
}
finally {
SQLite3.Finalize (stmt);
}
}
private Type MapToCSharpType (SQLite3.ColType colType)
{
Type targetType;
switch (colType) {
case SQLite3.ColType.Text:
targetType = typeof (string);
break;
case SQLite3.ColType.Integer:
targetType = typeof (int);
break;
case SQLite3.ColType.Float:
targetType = typeof (double);
break;
case SQLite3.ColType.Blob:
targetType = typeof (byte[]);
break;
default:
targetType = typeof (object);
break;
}
return targetType;
}
}
from sqlite-net.
with this method if I do complex query is very slow and device crash
from sqlite-net.
I have a select query that I need to use a dynamic type with. I am trying
var result= _database.Query<Dictionary<string,object>>(query);
This returns the correct number in the list, but the keys and values are blank. Is there a different way I should be running this to get the dynamic results?
from sqlite-net.
Related Issues (20)
- Getting RTL/Arabic Text from sqliteDB using this library flips character
- System.DLLNotFoundException:e_sqlcipher assembly:<unknown assembly> type:<unknown type> member:(null) HOT 1
- Insert large data (over 2GB) occurs OverflowException, how to solve it?
- SQLite version and how to upgrade it HOT 1
- Query with Where() returns no results, fails only with UWP release build
- Support for ORDER BY CASE WHEN? HOT 1
- SQLiteAsyncConnection Support for REGEXP
- LINQ Where() Does Not Work Well With Enums Decorated With the StoreAsTextAttribute
- Problem with .NET MAUI Blazor SQL app
- Debug local iphone result in file is not a database HOT 1
- Android 10 release version results in file is not a database error HOT 1
- Cannot get SQL for: Subtract
- e_sqlite3.dll not included in single file output with Native AOT compilation HOT 2
- Exception: Library e_sqlite3 not found, is seen when application is run in x64 or AnyCPU.
- "Where" clause doesn't work with bool when record is migrated until the record is updated
- e_sqlite.dll is created but not showing an error when using it.
- Using sqlite-net-base with SQLitePCLRaw.provider.winsqlite3 HOT 2
- The UniqueAttribute is not used in the DDL
- Using versions that support standard .net in an old .NET 4.X project doesn't work
- ExecuteAsync and RunInTransactionAsync are not safe together. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sqlite-net.