2

We are creating a WinStore application using C# that stores some data locally within a SQLite database. We are currently using the SQLite-net library to access the database. There are a number of tables within the database that have a dynamic schema based on the client’s data. The schema for these tables is only known at runtime.

We have not been able to query these dynamic tables with the SQLite-net library. Is it possible to query these tables with the SQLite-net library? Is there a different library that would provide similar functionality?

4

1 回答 1

0

The sqlite_master table has metadata about the objects in the database such as tables. You can query that table just like any other table. Be aware that it is a system table that you cannot manipulate directly and it's created/updated as you create database objects.

Once you query the table, you would have to dynamically build queries based on that metadata to query your dynamic schema.

Here's another SO post covering querying for tables: How to list the tables in an SQLite database file that was opened with ATTACH?

It is also discussed in the sqlite docs: http://www.sqlite.org/sqlite.html

See "Aside: Querying sqlite_master table"

EDIT:

Because you'll be creating dynamic sql based off of metadata, you'll need a method that can execute a text based t-sql query that you dynamically construct. Something like this method in sqlite-net:

public List<T> Query<T> (string query, params object[] args)

*note: I haven't used sqlite-net directly to construct dynamic sql but I have used other drivers (including the c one) to do this.

What you won't be able to do since it's dynamic sql (unless you want to do c# code generation and reflection of that generated code) is use the simple generics style query methods:

var query = conn.Table<Stock>().Where(v => v.Symbol.StartsWith("A"));

That pattern is most useful when you know your objects and table structure at compile time.

Note that there's is also a table called sqlite_temp_master which lists temporary tables. For example, as the docs above explain, the .tables cmdline shortcut actually does this query:

SELECT name FROM sqlite_master 
WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%'
UNION ALL 
SELECT name FROM sqlite_temp_master 
WHERE type IN ('table','view') 
ORDER BY 1

Note that it's also filter out sqlite_% since that prefix is reserved for system tables.

于 2013-04-11T00:02:56.143 回答