2016-10-03 10 views
0

I次のコード例を使用し、何らかの方法でsqlite3のデータベースのカラム名を取得したい:SQLiteのPRAGMAのtable_info(テーブル)列名を返す(C#でData.SQLiteを使用して)いない

using System; 
using System.Data.SQLite; 
namespace Program 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     Program stuff = new Program(); 
     stuff.DoStuff(); 
     Console.Read(); 
    } 
    private void DoStuff() 
    { 
     SQLiteConnection.CreateFile("Database.sqlite"); 
     SQLiteConnection con = new SQLiteConnection("Data Source=Database.sqlite;Version=3;"); 
     con.Open(); 
     string sql = "create table 'member' ('account_id' text not null unique, 'account_name' text not null);"; 
     SQLiteCommand command = new SQLiteCommand(sql, con); 
     command.ExecuteNonQuery(); 
     sql = "insert into member ('account_id', 'account_name') values ('0', '1');"; 
     command = new SQLiteCommand(sql, con); 
     sql = "PRAGMA table_info('member');"; 
     command = new SQLiteCommand(sql, con); 
     SQLiteDataReader reader = command.ExecuteReader(); 
     while (reader.Read()) 
     { 
      Console.WriteLine(reader.GetName(0)); 
     } 
     con.Close(); 
    } 

} 
} 

I私はだから、列名が必要です ..私はしかし、実際のカラム名を取得いけない 「CID名前タイプにnotnull dflt_value PK」 :また、私が得る唯一の結果は次の出力である

for(int i=0;i<reader.FieldCount;i++) 
{ 
    Console.WriteLine(reader.GetName(i)); 
} 

var columns = Enumerable.Range(0, reader.FieldCount).Select(reader.GetName).ToList(); 

を試してみました新しい列をtに書き込む私はアクセスしていない別のサーバーからのAPIの結果に応じて、彼はデータベースを取得します。データを印刷するときには、正しい列名が表示されていることを確認する必要があります。

+0

私はSystem.Data.SQLite 1.0.103を使用しています(ちょうどあなたが知りたい場合にのみ) – Ben

答えて

1

PRAGMA table_infoステートメントは、クエリのようなデータを返します。つまり、固定数の列と、各列に値を持つ行がいくつかあります。 GetName()を返すカラム名を呼び出す

 
sqlite> pragma table_info(member); 
cid  name   type  notnull dflt_value pk  
------- ------------ ------- ------- ---------- ------- 
0  account_id text  1     0  
1  account_name text  1     0  

:各行のデータを使用すると、約求めているテーブルの1列に対応しています。 - その代わりに、私は今

SELECT * FROM member WHERE 1 = 2; 

このコースの使用

PRAGMA table_info('member'); 

beeingてクエリ

while (reader.Read()) { 
    Console.WriteLine("field name: " + reader.GetString(1)); 
} 
+0

ちょうど確かめてください:スクリーンショットに示されているように、結果は 'account_id'と 'account_name'で何かになりたいです - 返された値は得られません.- http://puu.sh/rwfiZ/4166090e28.png – Ben

+0

より良いスクリーンショットを追加する:http://puu.sh/rwfth/8a86ba9bd9.png – Ben

0

私は本当にSQLiteのが私を嫌っていると思う:行の値を返すことなどGetString()を呼び出しどんな内容もなくテーブル自体を私に返すだけです。 BUT - reader.GetName(i)は実際には実際の列名を返しています! PRAGMA table_info( 'table_name') 'を作ろうと思っています。

関連する問題