C# 从sqlite表windows 8应用程序读取列名

C# 从sqlite表windows 8应用程序读取列名,c#,windows,sqlite,C#,Windows,Sqlite,我正在使用SQLite作为我正在使用的数据输入windows 8应用程序。我可以创建数据库、插入数据、检索列计数和读取数据,但无法获取列名 底层框架就是从这个角度出发的 我读过关于PRAGMA table_info(table_name)的文章命令,但我似乎无法正确发送和读取此查询。我已经在谷歌上搜索了3天了 MainPage.xaml.cs: using SQLite; using SqlLiteTest.Model; using System; using System.Collection

我正在使用SQLite作为我正在使用的数据输入windows 8应用程序。我可以创建数据库、插入数据、检索列计数和读取数据,但无法获取列名

底层框架就是从这个角度出发的

我读过关于
PRAGMA table_info(table_name)的文章命令,但我似乎无法正确发送和读取此查询。我已经在谷歌上搜索了3天了

MainPage.xaml.cs:

using SQLite;
using SqlLiteTest.Model;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Windows.Storage;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace SqlLiteTest
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            txtPath.Text = ApplicationData.Current.LocalFolder.Path;
        }

        private async void createDB(object sender, RoutedEventArgs e)
        {
            // access local folder
            var qvLocalFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

            try
            {
                //Create a blank carrier file
                StorageFile qvLocalFileCarrier = await qvLocalFolder.CreateFileAsync("qvdbLocal.db", CreationCollisionOption.FailIfExists);

                //Write the blank carrier file
                await FileIO.WriteTextAsync(qvLocalFileCarrier, "");
            }
            catch { }

            // connect
            var path = Windows.Storage.ApplicationData.Current.LocalFolder.Path + @"\qvdbLocal.db";
            var db = new SQLiteAsyncConnection(path);

            // create table
            await db.CreateTableAsync<qvdb>();

            // insert data
            var insertRecords = new List<qvdb>()
            {
                new qvdb
                {
                            qvdbRecord = 1,
                            qvdbNotes = "Notes1",
                            qvdb001 = "Variable 1.1",
                            qvdb002  = "Variable 2.1"
                    },
                    new qvdb
                    {
                            qvdbRecord = 1,
                            qvdbNotes = "Notes1",
                            qvdb001 = "Variable 1.1",
                            qvdb002  = "Variable 2.1"
                    },
                    new qvdb
                    {
                            qvdbRecord = 1,
                            qvdbNotes = "Notes1",
                            qvdb001 = "Variable 1.1",
                            qvdb002  = "Variable 2.1"
                    },
            };

            await db.InsertAllAsync(insertRecords);

            // read count
            var allUsers = await db.QueryAsync<qvdb>("SELECT * FROM qvdb");
            var count = allUsers.Any() ? allUsers.Count : 0;

            Debug.WriteLine(count);
        }

        private async void updateDB(object sender, RoutedEventArgs e)
        {
            var path = Windows.Storage.ApplicationData.Current.LocalFolder.Path + @"\qvdbLocal.db";
            var db = new SQLiteAsyncConnection(path);

            var tempCell = db.QueryAsync<qvdb>("UPDATE qvdb SET qvdbNotes ='!@#$%$%^^&*()+)(*&^%$#@!{:L<>?' WHERE qvdbRecord = 10");
            await db.UpdateAsync(tempCell);
        }

        private async void readDB(object sender, RoutedEventArgs e)
        {
            var path = Windows.Storage.ApplicationData.Current.LocalFolder.Path + @"\qvdbLocal.db";
            var db = new SQLiteAsyncConnection(path);

            var query = db.Table<qvdb>();
            var result = await query.ToListAsync();
            foreach (var item in result)
            {
                MessageDialog dialog = new MessageDialog(string.Format("{0} {1} {2}", item.qvdbRecord, item.qvdbNotes, item.qvdb001));
                await dialog.ShowAsync();
            }
        }

        private void readColNames(object sender, RoutedEventArgs e)
        {
        }
    }
}
谢谢你的信息。我添加了这个类,但仍然不知道如何访问它们。更多的代码

    // this works
    // read record count
    var allRecords = await db.QueryAsync<qvdb>("SELECT * FROM qvdb");
    var countRecords = allRecords.Any() ? allRecords.Count : 0;
    this.textboxLog.Text = this.textboxLog.Text + Environment.NewLine + "There are " + countRecords + " records.";

    // ??
    // read column names
    var allColumns = await db.QueryAsync<qvdb>("PRAGMA table_info(qvdb)");
    foreach (var item in allColumns) {
       //read name
        this.textboxLog.Text = this.textboxLog.Text + Environment.NewLine + "columbn names";
    }
//这很管用
//读取记录计数
var allRecords=await db.QueryAsync(“从qvdb中选择*);
var countRecords=allRecords.Any()?所有记录。计数:0;
this.textboxLog.Text=this.textboxLog.Text+Environment.NewLine+“有”+countRecords+“记录。”;
// ??
//读取列名
var allColumns=await db.QueryAsync(“PRAGMA table_info(qvdb)”;
foreach(所有列中的变量项){
//读名字
this.textboxLog.Text=this.textboxLog.Text+Environment.NewLine+“columbn name”;
}
返回的记录如下所示:

public class table_info_record
{
    public int    cid        { get; set; }
    public string name       { get; set; }
    public string type       { get; set; }
    public int    notnull    { get; set; }
    public string dflt_value { get; set; }
    public int    pk         { get; set; }
}
db.QueryAsync<table_info_record>("PRAGMA table_info(...)");
像这样使用它:

public class table_info_record
{
    public int    cid        { get; set; }
    public string name       { get; set; }
    public string type       { get; set; }
    public int    notnull    { get; set; }
    public string dflt_value { get; set; }
    public int    pk         { get; set; }
}
db.QueryAsync<table_info_record>("PRAGMA table_info(...)");
db.querySync(“PRAGMA table_info(…)”;
返回的记录如下所示:

public class table_info_record
{
    public int    cid        { get; set; }
    public string name       { get; set; }
    public string type       { get; set; }
    public int    notnull    { get; set; }
    public string dflt_value { get; set; }
    public int    pk         { get; set; }
}
db.QueryAsync<table_info_record>("PRAGMA table_info(...)");
像这样使用它:

public class table_info_record
{
    public int    cid        { get; set; }
    public string name       { get; set; }
    public string type       { get; set; }
    public int    notnull    { get; set; }
    public string dflt_value { get; set; }
    public int    pk         { get; set; }
}
db.QueryAsync<table_info_record>("PRAGMA table_info(...)");
db.querySync(“PRAGMA table_info(…)”;

o根据CL的建议结束循环,此代码成功读取列名:

 // read column names
           var query = await db.QueryAsync<table_info_record>("PRAGMA table_info(MY_TABLE_NAME_HERE)");

            foreach (var item in query)
            {
                Debug.WriteLine(string.Format("{0}", item.name) + " is a column.");
            }
//读取列名
var query=await db.querySync(“PRAGMA table_info(MY_table_NAME_HERE)”;
foreach(查询中的var项)
{
WriteLine(string.Format(“{0}”,item.name)+“是一列”);
}

o根据CL的建议结束循环,此代码成功读取列名:

 // read column names
           var query = await db.QueryAsync<table_info_record>("PRAGMA table_info(MY_TABLE_NAME_HERE)");

            foreach (var item in query)
            {
                Debug.WriteLine(string.Format("{0}", item.name) + " is a column.");
            }
//读取列名
var query=await db.querySync(“PRAGMA table_info(MY_table_NAME_HERE)”;
foreach(查询中的var项)
{
WriteLine(string.Format(“{0}”,item.name)+“是一列”);
}