SQLite too many terms in compound SELECT - android

In my Android SQLite databese query I have an INSERT INTO statement followed by about 600 ('data1'),('data2')... tags, generated by code.
After db.exec(sql) I got this error: "too many terms in compound SELECT (code1); while compiling INSERT INTO.. "
Is there any way to increase this limitation?

The limit SQLITE_MAX_COMPOUND_SELECT cannot be raised at runtime,
So you need to split your inserts into batches of 500 rows each. This will be more efficient than inserting one row per query. For e.g.
BEGIN TRANSACTION
INSERT INTO tablename (data1,data2) VALUES ("data1","data2")
INSERT INTO tablename (data1,data2) VALUES ("data1","data2")
INSERT INTO tablename (data1,data2) VALUES ("data1","data2")
...
END TRANSACTION
Also see Insert Multiple Rows in SQLite

Related

Sqlite order by rows order

I am using this query
"select * from SomeTable group by SomeColumn"
It is returns list with accenting order, but i need to same order like in database.
For example the order in database is:
p
a
s
But result is:
a
i
p
Sample
The result need to be like distinct by CityEN but with all columns and order like 1.Paris 2.Amsterdam 3.Istanbul
In Sqlite, each row of a table has a unique rowid, which you can use for sorting.
select * from SomeTable group by SomeColumn order by rowid;
In your statement, add this line to sort the results:
order by min(rowid)
Your query does not enforce any order with ORDER BY clause so no assumption about row order should be made. If you want specific order add i.e. ORDER BY SomeColumn. See docs about all available order options: https://www.sqlite.org/lang_select.html#orderby
By the rules of SQL, you can't count on getting records back in any specific order without specifying an ORDER BY clause in your SQL query.
In practice servers sometimes return values in the order in which they're inserted, in the order of the first index created, or in the order of the primary key--but you can't count on this behavior, and in fact I've seen the behavior change between database maintenance windows or after the database version is upgraded. You definitely wouldn't want to count on a DB engine to give you back records in any particular order if you write a SELECT statement without an ORDER BY clause.
The only real way to get your records back in the order you inserted them is to create a timestamp column and then sort on it during the SELECT. If you don't want to worry about populating that column on INSERT, have that column auto-populate itself with a timestamp (depending on your DB engine).

Android SQLite: How to update one column of all records with different values

I found on Stack similar questions like this How to update an entire column in sqlite? But they don't explain me my how to solve my task.
Say, I have a db with 5 columns and 5 records in it. What i need is to update the last column "date" with the values of unix time that differs by 1 sec. So i need to put values 1406820974139, 1406820974140, 1406820974141, 1406820974142, 1406820974143, 1406820974144.
How to do it using ContentValues? As i got i have to loop five times to create new ContentValues object and update one record at a time (maybe using db.startTransaction() syntax).
My question is is there a way to put all values at a time into one ContentValues object and write in them into DB? Or maybe the better way is to use rawQuery using native SQL syntax as explained in How to update an entire column in sqlite?
In theory, it would be possible to put all the values into a single SQL statement:
UPDATE MyTable
SET date = CASE _id
WHEN 5 THEN 1406820974139
WHEN 17 THEN 1406820974140
WHEN 23 THEN 1406820974141
WHEN 69 THEN 1406820974142
WHEN 666 THEN 1406820974143
END;
However, just creating one ContentValues object for each row is easier than constructing this command.
so that we know which date should go to which row? what is the cririteria to differentiate the rows?
a relational db table is different from say an excel table. there is no implicit row order (if you always see the rows in the same order,you can consider it a kind of coincidence,you can not rely on this like you do in excel), in a db table you need to have a column(or a group of them) with unique values which you use in your queries to identify each of your records.
so you need to be more clear in your question. what date should go to which record (identified by what?). there is no implicit row number, if you want it, add an autoincrement PK column.
then you could for instance use something along the lines of
UPDATE table SET column5= 1406820974140+PKcolumn
where 1406820974140 is the start date you have to choose, depending on what you are up to

Android SQLite Insert-Select

There are two different methods for inserting a row into table:
execSql, using it I can execute sql statements with SELECT - INSERT. But it doesn't return id of new row.
different inserts - I can get insert-id here, but I can't create insert queries with substitution values from another table.
I can split my query into two, but isn't it slower, than one query?
Do your insert however you like, then execSql "select last_insert_rowid()".

SQLite Insertion Order vs Query Order?

Will the order of rows returned by a query will be the same as the order in which the rows were inserted into the table, of SQLite database?
If Yes, Is this behaviour consistent?
If No, Can this be enforced?
I have a requirement of storing approx 500 rows of data, and which requires sorting/ordering from time to time. The data is in proper order, before the insertion.
Given the small number of rows in your table, this is probably what you need:
SELECT * FROM yourtable ORDER BY ROWID
For more information on ROWID, see these two links:
SQLite Autoincrement and ROWIDs and the INTEGER PRIMARY KEY
Even if the order may be consistent in one scenario, there is afaik no guarantee.
That is why SQL has the ORDER BY operator:
SELECT foo,bar FROM Table FOO WHERE frobnitz LIKE 'foo%' ORDER BY baz ASC;
Will the order of rows returned by a
query will be the same as the order in
which the rows were inserted into the
table, of SQLite database?
No, you can't count on that. All query optimizers have a lot of freedom when it comes to speeding up queries. One thing they're free to do is to return rows in whatever order is the fastest. That's true even if a particular dbms supports clustered indexes. (A clustered index imposes a physical ordering on the rows.)
There's only one way to guarantee the order of returned rows in a SQL database: use an ORDER BY clause.

is there any max limit for no:of rows while using sqlite?

I have a CSV file which contains around 1200 rows. I was trying to insert it into sqlite db. Around 300 rows only got inserted. Rest didnt...Is there any max limit on no:of rows in a table while using sqlite?
I tried more than 10000 inserts it worked, also check in android market there is SQL performance check tool which makes more inserts.
Rgds
Balaji
Ya.. we can have more than 10000 inserts.. Please refer this link
There IS a max limit for creating rows in SQLite.
If the table is initially empty, then a ROWID of 1 is used. If the
largest ROWID is equal to the largest possible integer
(9223372036854775807) then the database engine starts picking positive
candidate ROWIDs at random until it finds one that is not previously
used. If no unused ROWID can be found after a reasonable number of
attempts, the insert operation fails with an SQLITE_FULL error.
check the official SQLite documentation about Auto Increment.

Categories

Resources