1

PRAGMAの使用を含め、提供されているすべてのソリューションを試しましたが、機能しないようです!!! ユーザーがテーブル名を作成し、希望する列名を手動で入力できるプログラムがあります。選択したテーブルから列名を取得する方法が必要です。これを行うには、「Private void loadDB」関数が必要です。ご覧のとおり、私はそれを機能させるために多くの方法を使用しましたが、名前を抽出しません。

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]);
    }

}
4

2 に答える 2

3

String com = "PRAGMA table_info( "+tableName+" );";

これは、実行する正しいコマンドです。PRAGMAの SQLite ドキュメントによると:

PRAGMA table_info(テーブル名);

このプラグマは、指定されたテーブルの列ごとに 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'、タイプ 'INTEGER'、null にすることができる '0' (false だと思います)、デフォルト値 'null'
  • 1: 名前 'stuff'、タイプ 'VARCHAR'、null にすることができる '0' (false だと思います)、デフォルト値 'null'
  • 2: 名前 'val'、タイプ 'INTEGER'、null にすることができる '0' (false だと思います)、デフォルト値 'null'

最後の列が何であるかはわかりませんidが、自動生成されたフィールドであるため、そうである可能性があります。

于 2012-04-05T12:32:34.300 に答える
1
PRAGMA table_info(table_name);

トリックをしますか?

SQL Lite PRAGMA

于 2012-04-05T10:24:29.173 に答える