2012-04-05 110 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(命令行界面)來玩耍。 – 2012-04-05 10:09:32

回答

3

字符串COM = 「PRAGMA table_info( 」+表名+「);」;

這是執行的正確命令。根據SQLite docs on PRAGMA

PRAGMA table_info(table-name);

此編譯指示爲命名錶中的每列返回一行。結果集中的列包括列名稱,數據類型,列是否可以爲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',類型'INTEGER',可以爲空'0'(假設我猜),默認值'null'
  • 1:name'stuff',type'VARCHAR',can-null-null'0'(false我猜),默認值'null'
  • 2:名稱 'VAL', 'integer' 類型,可以-是空的 '0'(假我猜),默認值爲 '空'

我不知道最後一列是什麼但id是一個自動生成的字段,所以它可能是。

+0

我認爲,最後一列對應主鍵狀態 – 2015-04-05 08:15:35