简体   繁体   中英

Returning column names from table in SQLite using Java GUI

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

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM