SQLite column index order creates a table

This is the query that I use to create the table

create table site_table( _id integer primary key autoincrement, name_site text, url text, login text, pass text ); 

I called Cursor.getColumnNames() and noticed that the column order is id, login, pass, name, url .

So, if I need a value, I have to get it at the Cursor.getString(index) . While I wasn’t debugging, I messed up calling the wrong index, but now I wonder why SQLite saves this way? Why does this not follow the way I created id, name_site, url, login and pass ?

Thanks

+4
source share
4 answers

So, if I need a value, I have to get it at the Cursor.getString index (index)

So, for example, for this reason you should always use

 c.getString(c.getColumnIndex("ColName")); // or better getColumnIndex(CONSTANT) 

This method saves us all and ensures that you never get the wrong results. As a rule, this method is recommended, as well as saving COLUMN_NAMES as CONSTANTS in a separate class is a very, very useful and effective practice.

Note: The order depends on the projection, i.e. select name, lastname from table

+4
source

This data is ordered by the order that you requested in the query, and not in the order in which you created the table. That way, you probably reordered the query that generated the specified cursor.

+1
source

The order of the columns in your course depends on the projection. To use the correct column index, use c.getString(c.getColumnIndexOrThrow("COLUMN_NAME")) , where c is your cursor.

+1
source

I just made a first-hand experience:

Indexes of cursor columns as a result

SELECT * FROM mytable WHERE ...

the query sometimes (not always) differs from what the SQLITE Database Browser shows as the order of the columns on the Database Structure tab. Therefore, column references using getColumnIndex seem to be the only safe way.

0
source

All Articles