2012-04-05 2 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 = "PRAGMA의 table_info ("+ TABLENAME + ")";

올바른 명령입니다. SQLite docs on PRAGMA에있어서

PRAGMA의 table_info (표 이름);

이 pragma는 명명 된 테이블의 각 열에 대해 하나의 행을 반환합니다. 결과 집합의 열은 열 이름, 데이터 형식, 열이 NULL이 될 수 있는지 여부 및 열의 기본값을 포함합니다. 내가 JDBC를 통해 SQLite는 그것을하려고하면

잘 작동하는 것 같다 :

  • 0 :

    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 열이 있음을 보여줍니다 이름 '아이디'를, 'null'이 될 수 있습니다. 'default' 'null'일 수도 있습니다.

  • 1 : 이름 'stuff', 'VARCHAR', null 일 수 있음 '0'(false 나는 짐작한다), 디폴트 값 'null'
  • 2 : 이름이 '발'입력 'INTEGER는'- null이 될 수있다 '0'(거짓 같아요), 잘 모르겠어요

기본 값 '널 (null)이'마지막 열이 무엇인지 그러나 id은 자동 생성 필드이므로 그렇게 될 수 있습니다.

+0

나는 마지막 칼럼이 기본 키 상태에 해당한다고 생각한다. –

관련 문제