Returning column names from table in SQLite using Java GUI

拟墨画扇 提交于 2019-11-28 11:54:22

问题


I need to get column names from table, only difference is that I made a function that makes new column depending on users entry in txtbox. So for example if a user enters "3", column would be named "Sezonalni_utjecaj_3". Now when you saw that example I need to make a query that returns column names so I can put them inside my combobox, so everytime I enter a new column, the name of the column would be placed inside of the combobox (column names have something in common, and thats "Sezonalni_utjecaj_", but I have other columns too, not only ones that have "Sezonalni_utjecaj_" inside their name but I dont need their names ).

My Interface:

When i press "Ok" this is code behind it:

btnOk_2 = new JButton("Ok");
    btnOk_2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            try {
                String kolumna=(String)textField.getText();
                String upit="alter table Linearni_trend_s_sezonalnim_utjecajem add column 'Sezonalni_utjecaj_"+kolumna+"' float;";
                PreparedStatement pst1 = konekcija.prepareStatement(upit);
                pst1.execute();
                pst1.close();
            } catch (SQLException e) {

                e.printStackTrace();
            }

回答1:


Actually Pragma can help you to get all columns name with type etc etc.PRAGMA_SQL

  PRAGMA table_info(table_name);

Alternatively you may use another way like ;

SELECT sql FROM sqlite_master
WHERE tbl_name = 'table_name' AND type = 'table'


来源:https://stackoverflow.com/questions/29329740/returning-column-names-from-table-in-sqlite-using-java-gui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!