Obtain "next to be used" id for a table - android

I have a table with 2 int type columns: one auto increment and primary, while the other is just not null.
now, whenever a record is inserted into the tale, the first auto-increment column gets a unique and new id. What I want is that this new ID value to be also inserted to the other column, in the same insert statement.
How can a achieve this? Plz help...
thnx in advance :)

I do not understand why you need in this? However, you can create the second column also with autoincrement (but you should check if the values in these two columns are the same). Moreover, you can create a trigger that will update the value of the second column after the insert (this is the best way, I guess).

Related

Is 'ORDER BY _id' enough to get rows in insertion order?

I'm using SQLite DB in my Android app.
Now I need to implement data extraction in the same order in which it was inserted.
Is it enough to do SELECT ... ORDER BY _id ... if the _id column is INTEGER PRIMARY KEY AUTOINCREMENT?
Or should I add a column to store the date and time a row has been created?
You can use ORDER BY _id. Make sure you are using a List and not a Set....
No need, an id generated by AUTOINCREMENT is enough.
Yes, if you just need the order, you may use such statement or just simple "SELECT ... FROM sometable". Results will be equals.

Column index order SQLite creates table

This is the query that I use to create a 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 columns order are id, login, pass, name, url.
So, if I want a value I have to get it by the index Cursor.getString(index). Until I debugged I was messing up calling the wrong index, but now I wonder, why SQLite saves that way? Why it does not follow that way I created id, name_site, url, login and pass?
Thanks
So, if I want a value I have to get it by the index
Cursor.getString(index)
So for example for this reason you should always use
c.getString(c.getColumnIndex("ColName")); // or better getColumnIndex(CONSTANT)
This method saves all of us and ensure that you never get wrong results. Generally this method is recommended and also storing COLUMN_NAMES as CONSTANTS in separated class is very, very useful and efficient practise.
Note: Order depends on projection i.e. select name, lastname from table
That data is ordered by the order your requested it in your query, not the order you created the table with. So you probably changed the order in your query that generated said cursor.
Columns order in your cursor depends on projection. To be sure you use correct column index use c.getString(c.getColumnIndexOrThrow("COLUMN_NAME")) where c is your cursor.
I just made the experience first hand:
The indices of the columns of the cursor as a result of a
SELECT * FROM mytable WHERE ...
query have sometimes (not always) a different order that what SQLITE Database Browser shows as column order in the Database Structure tab. So referencing the columns via getColumnIndex seems to be the only safe way.

Autoupdate of the primary key in android

I created the table using SQLiteOpenHelper the image of the table i created is given below
After this I delete the first row from the table and now my table looks like this
You can see that the id is now starting with 2.
So my problem is when I delete the first row the id first column goes to 2
Please help me to solve this issue . i want a table looks like this.
You can see that the id is now start with 2. So my problem is when I
delete the first row the id first column goes to 2 Please help me to
solve this issue
There is no problem with id value. If you are using autoincrement or not this is normal behaviour. In the case of autoincrement has internal iterator and it hasn't "memory".
So imagine this scenario: You have two rows with id 1, 2.
What will happen when you will delete first row?
In db will remain one row with id 2 but if you will insert next row, this row will be automatic have value id with 3 not deleted 1 also if you will delete first row, second row won't change value to 1.
In the other case if you are not using autoincrement this is possible(only make it programatically) but i think this approach is not effective. If you will have for example milion records, every row's id should be changed? This is sick.
So here is really not problem but normal behaviour.
Your issue is probably one of the following:
Your ID field is simply called id, whereas Android uses the conventional name _id (as a Java constant, BaseColumns._ID).
The ID field should be of type INTEGER PRIMARY KEY

SQLite insert record with _id value for two or more fields

I have a case that I would like to insert record in SQLite with database.insert(table, null, values).
TABLE t2 (_id, field1, field2)
..
val.setVal1(null);
val.setVal2(val2);
..
if(val.getVal1==null){
values.put(field1, _id);
}else{
values.put(field1, var.val1);
}
values.put(field2, var.val2);
database.insert("t2", null, values);
Is possible to do sth like this "values.put(field1, _id);"?
_id is generated at database.insert().
Note: I am looking for solution for one insert call. Insert and update row with (field1=_id) is easy.
i think i see now. you're asking if you can enter a value into a specific SQLite row _id field if it's available in your val object. Else, you want the database to automatically create a unique id for that column while inserting, like normally done. Is this correct?
To that end, i would seriously reconsider this purpose. You should never be specifying values for the _id column because it needs to be unique or else you'll get exceptions thrown. Moreover, it's only purpose is to be a unique identifier for the system, so you personally knowing this value should be of no use to you.
If you still need this functionality, i'd suggest making another field in your table (much like the _id column but not it), which you can fill with randomly generated numbers or val.getVal1 values.

Find id of last inserted row

I have to simple tables in SQLite in my app and I need to insert into my tables values, but when I insert into the first I need to find value id ( primary key first column ) and that to be foreign key in second table. I know to find this with select last but is there better way to find this, id of last inserted row ?
If you're using android.database.sqlite.SQLiteDatabase then you can use insert() which returns the id of the inserted row: see here
use
SELECT last_insert_rowid()

Categories

Resources