从sqlite表Windows 8应用程序读取列名 [英] read column names from sqlite table windows 8 app

查看:21
本文介绍了从sqlite表Windows 8应用程序读取列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 SQLite 用于我正在开发的数据输入 Windows 8 应用程序.我可以创建数据库、插入数据、检索列数和读取数据,但无法获取列名.

I am using SQLite for a data entry windows 8 app I am working on. I can create the db, insert data, retrieve a column count, and read data, but cannot get the column names.

底层框架来自这个帖子.

我了解了 PRAGMA table_info(table_name); 命令,但我似乎无法正确发送和读回此查询.我已经谷歌搜索了 3 天!

I read about the PRAGMA table_info(table_name); command but I cannot seem to send and read back this query properly. I have been googling for 3 days!

MainPage.xaml.cs:

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)
        {
        }
    }
}

qvdb.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SQLite;

namespace SqlLiteTest.Model
{
    public class qvdb
    {
        [PrimaryKey, AutoIncrement]
        public int qvdbRecord { get; set; }

        [MaxLength(3000)]
        public string qvdbNotes { get; set; }

        [MaxLength(1000)]
        public string qvdb001 { get; set; }

        [MaxLength(1000)]
        public string qvdb002 { get; set; }

    }
}

感谢 CL 提供的信息.我添加了类,但仍然不知道如何访问它们.还有一些代码...

Thanks CL for the info. I added the class but still do not know how to access them. Some more code...

    // 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";
    }

推荐答案

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

o end the loop on CL's advice, this code successfully reads the column names:

 // 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.");
            }

这篇关于从sqlite表Windows 8应用程序读取列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆