SQLite Schema Information Metadata

后端 未结 7 1001
终归单人心
终归单人心 2020-11-27 03:46

I need to get column names and their tables in a SQLite database. What I need is a resultset with 2 columns: table_name | column_name.

In MySQL, I\'m a

相关标签:
7条回答
  • 2020-11-27 03:59

    There are ".tables" and ".schema [table_name]" commands which give kind of a separated version to the result you get from "select * from sqlite_master;"

    There is also "pragma table_info([table_name]);" command to get a better result for parsing instead of a construction query:

    
    sqlite> .tables
    students
    sqlite> .schema students
    create table students(id INTEGER, name TEXT);
    sqlite> pragma table_info(students);
    0|id|INTEGER|0||0
    1|name|TEXT|0||0
    

    Hope, it helps to some extent...

    0 讨论(0)
  • 2020-11-27 04:04

    Try this sqlite table schema parser, I implemented the sqlite table parser for parsing the table definitions in PHP.

    It returns the full definitions (unique, primary key, type, precision, not null, references, table constraints... etc)

    https://github.com/maghead/sqlite-parser

    The syntax follows sqlite create table statement syntax: http://www.sqlite.org/lang_createtable.html

    0 讨论(0)
  • 2020-11-27 04:09

    Another useful trick is to first get all the table names from sqlite_master.

    Then for each one, fire off a query "select * from t where 1 = 0". If you analyze the structure of the resulting query - depends on what language/api you're calling it from - you get a rich structure describing the columns.

    In python

    c = ...db.cursor()
    c.execute("select * from t where 1=0");
    c.fetchall();
    print c.description;
    

    Juraj

    PS. I'm in the habit of using 'where 1=0' because the record limiting syntax seems to vary from db to db. Furthermore, a good database will optimize out this always-false clause.

    The same effect, in SQLite, is achieved with 'limit 0'.

    0 讨论(0)
  • 2020-11-27 04:11

    Recent versions of SQLite allow you to select against PRAGMA results now, which makes this easy:

    SELECT 
      m.name as table_name, 
      p.name as column_name
    FROM 
      sqlite_master AS m
    JOIN 
      pragma_table_info(m.name) AS p
    ORDER BY 
      m.name, 
      p.cid
    

    where p.cid holds the column order of the CREATE TABLE statement, zero-indexed.

    David Garoutte answered this here, but this SQL should execute faster, and columns are ordered by the schema, not alphabetically.

    Note that table_info also contains

    • type (the datatype, like integer or text),
    • notnull (1 if the column has a NOT NULL constraint)
    • dflt_value (NULL if no default value)
    • pk (1 if the column is the table's primary key, else 0)

    RTFM: https://www.sqlite.org/pragma.html#pragma_table_info

    0 讨论(0)
  • 2020-11-27 04:14

    This is an old question but because of the number of times it has been viewed we are adding to the question for the simple reason most of the answers tell you how to find the TABLE names in the SQLite Database WHAT DO YOU DO WHEN THE TABLE NAME IS NOT IN THE DATABASE ? This is happening to our app because we are creating TABLES programmatically So the code below will deal with the issue when the TABLE is NOT in or created by the Database Enjoy

        public void toPageTwo(View view){
    
        if(etQuizTable.getText().toString().equals("")){
            Toast.makeText(getApplicationContext(), "Enter Table Name\n\n"
                    +"           OR"+"\n\nMake Table First", Toast.LENGTH_LONG 
       ).show();
            etQuizTable.requestFocus();
            return;
        }
    
        NEW_TABLE = etQuizTable.getText().toString().trim();
        db = dbHelper.getWritableDatabase();
        ArrayList<String> arrTblNames = new ArrayList<>();
        Cursor c = db.rawQuery("SELECT name FROM sqlite_master WHERE 
       type='table'", null);
    
        if (c.moveToFirst()) {
            while ( !c.isAfterLast() ) {
                arrTblNames.add( c.getString( c.getColumnIndex("name")) );
                c.moveToNext();
            }
        }
        c.close();
        db.close();
    
        boolean matchFound = false;
        for(int i=0;i<arrTblNames.size();i++) {
            if(arrTblNames.get(i).equals(NEW_TABLE)) {
                Intent intent = new Intent(ManageTables.this, TableCreate.class 
       );
                startActivity( intent );
                matchFound = true;
            }
        }
        if (!matchFound) {
            Toast.makeText(getApplicationContext(), "No Such Table\n\n"
                    +"           OR"+"\n\nMake Table First", Toast.LENGTH_LONG 
     ).show();
            etQuizTable.requestFocus();
        }
    }
    
    0 讨论(0)
  • 2020-11-27 04:18

    You've basically named the solution in your question.

    To get a list of tables (and views), query sqlite_master as in

    SELECT name, sql FROM sqlite_master
    WHERE type='table'
    ORDER BY name;
    

    (see the SQLite FAQ)

    To get information about the columns in a specific table, use PRAGMA table_info(table-name); as explained in the SQLite PRAGMA documentation.

    I don't know of any way to get tablename|columnname returned as the result of a single query. I don't believe SQLite supports this. Your best bet is probably to use the two methods together to return the information you're looking for - first get the list of tables using sqlite_master, then loop through them to get their columns using PRAGMA table_info().

    0 讨论(0)
提交回复
热议问题