在SQLite中检索表列名称

时间:2012-04-05 10:07:11

标签: java sql sqlite sqlcommand

我已经尝试了所有提供的解决方案,包括使用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]);
    }

}

2 个答案:

答案 0 :(得分:3)

  

String com =“PRAGMA table_info(”+ tableName +“);”;

这是执行的正确命令。根据{{​​3}}:

  

PRAGMA table_info(table-name);

     

此pragma为指定表中的每列返回一行。结果集中的列包括列名,数据类型,列是否为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:名称'stuff',输入'VARCHAR',可以为空'0'(我猜是假的),默认值'null'
  • 2:名称'val',输入'INTEGER',可以为空'0'(我猜是假的),默认值'null'

我不确定最后一列是什么,但id是一个自动生成的字段,所以可能就是这样。

答案 1 :(得分:0)

PRAGMA table_info(table_name);

会做到这一点吗?

SQL Lite PRAGMA