tags:

views:

18

answers:

1

Hello, please, is this the correct syntax for copying a particular row from one table to another?

db.execSQL("insert into Route select * from " + DATABASE_TABLE + " WHERE _id = rowId");

because for some reason, when i call the method that executes this statement, all the items from the first table are copied into the second table and not the specific row i want. thanks.

this is the method call for copy:

public void copyData(long rowId){
db.execSQL("insert into Route select * from " + DATABASE_TABLE + " WHERE _id = rowId"); 
}

if not, could you please show me how?..

A: 

I see a couple of problems:

First, you're not passing the rowId argument to your copyData() method in as part of the query, you're using the string literal rowID.

Second, ROWID has special meaning in SQLite. It's a pseudo-column that uniquely identifies each row in a table and is usually the same value that gets assigned to an INTEGER PRIMARY KEY column, which I'm guessing is how you have _id declared. The result is that when WHERE _id = ROWID is evaluated, the two values always match and therefore all rows are selected.

Blrfl
hmmm.. yeah, i see what you mean. i had thought of this earlier too but since my method was taking in a parameter. i thought it would be unique to this. so please what should i use as the syntax.. because i have read if am not mistaken, that the row id for a table should always be "_id". does that mean i assign the second table a different primary key Id. Also, i don't really understand the first point you made. thank you
sparrow
sorry for the first point you made, is this what you meant? db.execSQL("insert into Route select * from " + DATABASE_TABLE + " WHERE _id =" + rowId);
sparrow
Yes, that would be the way to do it. The second thing is really just a side effect of the first.
Blrfl
ok thanks, it works now
sparrow