I'm struggling to achieve what seems to me should be a simple goal.
I have a long list of vaule I want imported in to a database in one column with many rows.
I have no problems getting the data in in one row and many colums, but I can't figure out how to do it the other way round.
Is that possible??? if not is it possible to alter the table once the data in imported to swap to a manyx1 from a 1xmany table.
Thanks in advance.
As with any other database, if you want 10 rows of data, you have to perform 10 insert operations. So you would just loop over your list of values and for each one insert a new row into the database.
Related
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
I am building an android application with a database that contains more than 20,000 entries.
When i retrieve data from the database, especially when searching for data, it seems to be working slow.
Especially, when i search data based on an editText. Every time editText TextChange(), i query :
Select * from mytable where data='mydata'
And it runs slowly.
I really don't know how to make it work faster.
Hope anyone can help me!
You can create an index on just that column to make selection work faster. If your column is the second one in an existing index, this index cannot be used most efficiently.
CREATE INDEX idx_mytable_word ON mytable(word)
May be you initialize db connection every time?
Try to add pagination. it's make your app faster.
I kept my sqlite database in assets folder. I need to get the entire table data and should display it in listview. i.e each row of the table should be each item of the listview. How can I do this? Please help me in this regard. I worked on getting data from remote database server via php and json. Can we use the same json for getting data from local database. If so how can I do that?
Any suggestion will be thankful....
How many columns that you have in that table, if you more number of columns then you might need to think to show about 2 or 3 columns instead of all in the list view.
The below tutorial help you to retrieve the records from the table, please go through
http://www.vogella.com/articles/AndroidSQLite/article.html
I want to copy all the rows and columns from one table to another. I know the way using Cursor, The cursor gets all the data and i then taking it into the ArrayList and after doing the same thing reverse to insert it into another table.
But is there any simple and fast way to copy one table to another. If Yes,then please let me know..
You can copy the contents of one table and use it to populate another table as long the structure of the tables are same. INSERT INTO Destination SELECT * FROM Source;
My question is quite straightforward. I have a table, with, lets say x rows, with each an id, but I don't know how many rows I have. Now I want to delete the last row of my table for some reason... Is there an easy way to do that in android? I have been trying to use the last_insert_rowid in my where clause...but no luck so far...
Any idea to do that with the sqlite database tools of android?
I assume that last_insert_rowid means that you are talking about db.
DELETE FROM test WHERE id = (SELECT MAX(id) FROM test);