2012-04-05 141 views
1

PRAGMAを使用するなど、提供されているすべてのソリューションを試しましたが、動作させることができません。私は、ユーザーがテーブル名を作成し、自分が望む列名を手動で入力できるようにするプログラムを持っています。選択したテーブルから列名を取得する方法がいくつか必要です。これを行うには、私は "private void loadDB"関数が必要です。あなたが見ることができるように、私はそれを動作させるように努力する方法をたくさん使いましたが、名前を抽出することはありません。SQLiteでテーブルの列名を取得する

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Font; 
import java.awt.Insets; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JTable; 
import javax.swing.SwingConstants; 
import javax.swing.border.EmptyBorder; 
import javax.swing.border.LineBorder; 


public class ViewTable extends JFrame { 

    private static final long serialVersionUID = 1L; 
    private static final String PREFERRED_LOOK_AND_FEEL = null; 
    private final String tableName; 
    private String[] columnNames; 
    private String[][] data; 
    private JTable tablePane; 

    public ViewTable(String tableName){ 
     this.tableName=tableName; 

     initComponents(); 
     loadDB(); 
     displayTable(); 

     setDefaultCloseOperation(CreateTemplate.EXIT_ON_CLOSE); 
     setSize(700,700); 
     setTitle("View Table"); 
     setVisible(true); 


    } 

    private void initComponents() { 
     JPanel mainPanel = new JPanel(new BorderLayout()); 
     this.add(mainPanel); 
     mainPanel.setBackground(Color.yellow); 

     JPanel topPanel = new JPanel(new BorderLayout()); 
     topPanel.setBackground(Color.blue); 
     mainPanel.add(topPanel,BorderLayout.NORTH); 

     JLabel titleLabel = new JLabel(this.tableName); 
     titleLabel.setBorder(new LineBorder(Color.black)); 
     titleLabel.setFont(new Font("Helvetica", Font.BOLD, 24)); 
     titleLabel.setForeground(Color.orange); 
     titleLabel.setHorizontalAlignment(SwingConstants.CENTER); 
     topPanel.add(titleLabel,BorderLayout.CENTER); 

     JButton exitButton = new JButton("Finish"); 
     exitButton.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent event){ 
       close_window(); 
      } 
     }); 
     exitButton.setBorder(new EmptyBorder(new Insets(20,20,20,20))); 
     topPanel.add(exitButton,BorderLayout.EAST); 

    } 

    private void close_window(){ this.dispose(); } 

    private void loadDB(){ 
     //String com = "SELECT sql from sqlite_master WHERE tbl_name = '"+tableName+"' AND type = 'table'"; 
     //String com = "PRAGMA table_info("+tableName+");"; 
     //String com = "INSERT null"; 
     //SQLCommands.SQLCommand(com); 
     //String com = "SELECT * FROM sqlite_master"; 
     //String[] output = SQLCommands.returnSQLCommand(com); 
     //String com = "SELECT * FROM (PRAGMA table_info("+tableName+");"; 
     //String[] output = SQLCommands.returnSQLCommand(com); 

     //for (String S : output){ System.out.println(S); JOptionPane.showInputDialog("Enter template name"); } 


     //get column names 
     //columnNames = new String[3]; 
     //columnNames[0] = "Name"; columnNames[1] = "Surname"; columnNames[2] = "Sex"; 

     //use column names to populate data 
     //e.g. data[4][0] => "SELECT Name FROM tableName"[4] 

    } 

    private void displayTable(){ 
     //use JTable 

     //new JScrollPane which uses the table 

     //put JScrollpane in CENTER box 

    } 

    public static void main(String[] args){ 
     String [] tableNames = SQLCommands.returnSQLCommand("SELECT name FROM sqlite_master"); 
     new ViewTable(tableNames[9]); 
    } 

} 
+0

PCでSQLite CLI(コマンドラインインターフェイス)を使用して遊ぶことを強くお勧めします。 –

答えて

3

列COM = "プラグマtable_info( "+ tableNameの+");";

これは正しいコマンドです。 SQLite docs on PRAGMA

PRAGMA table_info(table-name);

このプラグマは、名前付きテーブルの各列に対して1つの行を返します。結果セットの列には、列名、データ型、列をNULLにするかどうか、および列の既定値が含まれます。

私はJDBC経由でのSQLiteでそれをしようとすると、正常に動作するようです:

JdbcDatabaseConnection compiled statement: PRAGMA table_info(foo) 
[0, id, INTEGER, 0, null, 1] 
[1, stuff, VARCHAR, 0, null, 0] 
[2, val, INTEGER, 0, null, 0] 

これはfoo表は3列があることを示しています。

  • 0:名 'ID' と、 'null'、デフォルト値 'null'
  • 1:名前 'stuff'、タイプ 'VARCHAR'、可能null '0'(false)私は推測する)、デフォルト値 'null'
  • 2:名 'valを'、タイプ 'INTEGER'、-することができ、ヌル '0'(私が推測する偽)、デフォルト値 'NULL'

私は、最後の列が何であるかわからないんだけどidは自動的に生成されるフィールドなので、それが可能です。

+0

私は、最後の列がプライマリキーのステータスに対応していると思います –

関連する問題