Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the type of a column in SQLite

Tags:

android

sqlite

My mate made the database for my Android app. For example, one of the tables is created like this:

CREATE TABLE table1(
    id_fields_starring INTEGER PRIMARY KEY AUTOINCREMENT
    , fields_descriptor_id INTEGER NOT NULL
    , starring_id INTEGER NOT NULL
    , form_mandatory INTEGER NOT NULL DEFAULT 1
    , form_visible INTEGER NOT NULL DEFAULT 1
    , FOREIGN KEY(fields_descriptor_id) REFERENCES pr_fields_descriptor(id_fields_descriptor) ON DELETE CASCADE ON UPDATE CASCADE
    , FOREIGN KEY(starring_id) REFERENCES starring(id_starring) ON DELETE CASCADE ON UPDATE CASCADE
)

From the fields of a table I need to know which of them is INTEGER NOT NULL and which is INTEGER NOT NULL DEFAULT 1, because for the first case I must create dinamically an EditText and for the second case I must create a CheckBox. Any idea?

like image 873
Gabrielle Avatar asked Aug 01 '12 06:08

Gabrielle


People also ask

How do I SELECT a column type in SQL?

You can get the MySQL table columns data type with the help of “information_schema. columns”. SELECT DATA_TYPE from INFORMATION_SCHEMA. COLUMNS where table_schema = 'yourDatabaseName' and table_name = 'yourTableName'.

How do I get the column name in sqlite3?

PRAGMA table_info(table_name); will get you a list of all the column names.

How can I see the structure of a table in SQLite?

If you are running the sqlite3 command-line access program you can type ". tables" to get a list of all tables. Or you can type ". schema" to see the complete database schema including all tables and indices.

What is SQLite datatype?

SQLite only has four primitive data types: INTEGER, REAL, TEXT, and BLOB. APIs that return database values as an object will only ever return one of these four types.


1 Answers

Calling:

PRAGMA table_info(table1);

will dump the table information, e.g.

cid|name                 |type    |notnull |dflt_value |pk
0  |id_fields_starring   |INTEGER |0       |           |1
1  |fields_descriptor_id |INTEGER |1       |           |0
2  |starring_id          |INTEGER |1       |           |0
3  |form_mandatory       |INTEGER |1       |1          |0
4  |form_visible         |INTEGER |1       |1          |0

and simply find the row in the cursor with notnull=1 and dflt_value=1


Edit:

To list all columns defined as INTEGER NOT NULL DEFAULT 1 this would work (helper is your instance of SQLiteOpenHelper):

SQLiteDatabase db = helper.getWritableDatabase();
Cursor cursor = db.rawQuery("PRAGMA table_info(table1)", null);
try {
    int nameIdx = cursor.getColumnIndexOrThrow("name");
    int typeIdx = cursor.getColumnIndexOrThrow("type");
    int notNullIdx = cursor.getColumnIndexOrThrow("notnull");
    int dfltValueIdx = cursor.getColumnIndexOrThrow("dflt_value");
    ArrayList<String> integerDefault1NotNull = new ArrayList<String>();
    while (cursor.moveToNext()) {
        String type = cursor.getString(typeIdx);
        if ("INTEGER".equals(type)) {
            // Integer column
            if (cursor.getInt(notNullIdx) == 1) {
                // NOT NULL
                String defaultValue = cursor.getString(dfltValueIdx);
                if ("1".equals(defaultValue)) {
                    integerDefault1NotNull.add(cursor.getString(nameIdx));
                }
            }
        }
    }
    System.out.println("integerDefault1NotNull now contains a list of all columns " +
            " defined as INTEGER NOT NULL DEFAULT 1, " + integerDefault1NotNull);
} finally {
    cursor.close();
}
like image 70
Jens Avatar answered Oct 01 '22 19:10

Jens