In my program, I fetch some values from the server with ID's. Some of the ID's returned are 0. I am executing an insert statement where I am inserting these values. When I make a Select Query, I am not able to get the values with ID = 0. I am getting every other ID's. I cannot look at the database tables because it is encrypted. I am not sure if it is not inserting it into the database or the query is not fetching it. The SELECT query does return values with ID's != 0. It is just the values with ID = 0 which is not being returned. Any clue of what I might be doing wrong, or how can I track whats going wrong in my program?
Don't use the server IDs as an index in SQLite.
Have another column for your primary key and store the server IDs in a plain NUMERIC column.
Related
Mistakenly I have put a double value divided by zero in an SQLite database column.
In "DB Browser", it looks like string "Inf" or "inf". However, "WHERE MY_COLUMN = 'inf'" gives no result.
How can I retrieve all records containing this value via an SQL query?
Also, how can I identify this value in the cursor loop?
I have tried these:
cursor.getString(cursor.getColumnIndex(MY_COLUMN)).equalsIgnoreCase("inf")
cursor.getDouble(cursor.getColumnIndex(MY_COLUMN)) == Double.POSITIVE_INFINITY
No result as well.
I was curious if androids SQLiteDatabase insert method automatically handles type conversion.
Here is my example:
I have a csv file with a column name of age. Its type will be an INTEGER.
Lets say I have already created the database and table.
Now I am parsing the csv file with CSVReader, which parses each line and inserts each value into an index of a String[].
In order to insert each line of data into the database, I have to use a ContentValue object, which allows me to store values in it.
//Parse each line and store in line...
ContentValue values = new ContentValue();
values.put(KEY_AGE, line[1]); // Assume line[1] is the age
database.insert(table, null, values);
If I store the age value as a string (as seen above), and then insert it into the table, does Android handle the conversion to INTEGER before inserting it into the database?
I am asking this because I am trying to insert a bunch of tables into a database, and it looks much cleaner when I can just iterate through an array then explicitly state each put call, i.e:
Also if anyone has any design suggestions feel free to tell me.
CLEAN
int i = 0;
for(String s : TransitContract.Routes.COLUMN_ARRAY) {
values.put(s, line[i]);
i++;
}
UGLY
values.put(TransitContract.Routes.KEY_ROUTE_ID, line[0]);
values.put(TransitContract.Routes.KEY_AGENCY_ID, line[1]);
values.put(TransitContract.Routes.KEY_SHORT_NAME, line[2]);
values.put(TransitContract.Routes.KEY_LONG_NAME, line[3]);
values.put(TransitContract.Routes.KEY_DESCRIPTION, line[4]);
values.put(TransitContract.Routes.KEY_ROUTE_TYPE, Integer.parseInt(line[5]));
values.put(TransitContract.Routes.KEY_URL, line[6]);
values.put(TransitContract.Routes.KEY_COLOR, line[7]);
values.put(TransitContract.Routes.KEY_TEXT_COLOR, line[8]);
return mDatabase.insert(TransitContract.Routes.TABLE_NAME, null, values);
When you declare a column as INTEGER, SQLite will automatically convert strings to numbers, if possible.
See the documentation for Type Affinity.
If your ContentProvider doesn't restrict it (i.e. pass it directly to the SQLiteDatabase.insert() method), it should work. SQLite is not that picky about the types used in queries/inserts and the actual column type.
However, it would be best practice to parse and check the values before inserting. Otherwise you might actually insert a string which can't be parsed as integer and therefore retrieving the value might fail.
References:
Boolean datatype accepting string value and integer value
SQLite table with integer column stores string
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.
I have a database wherein i get my questions , correct answers/options from... I want my application to automatically generate random rowIds so that the questions could be shuffled.. Of course, the question that's already shown should not display again. I want to get 10 questions then finish();..
Using a random rowId is the wrong approach. What if the database is modified and the ID becomes invalid? You'd have to check every ID and regenerate when an invalid ID comes up.
Instead, you should use a LIMIT clause in your SELECT statement with a random number less than the number of rows in the table.
No need to generate random ids first. Just insert your rows, making sure you have the questionId column.
When you want read your database. Do something like quizid = rand() ....
After that you select the row with quizid in your database
SELECT * FROM quiztable WHERE questionId = quizid
Something like that will give you a random row from your database.
I think you get the point.
Here is my table schema:
downloadstbl
_id int not null auto
url text not null
filename text not null
date text not null)
I would like to insert a row only if there isnt a row in the table already with the same url.
E.g, if the following row exists:
_id=1
url="http://google.com/img.jp"
filename="img.jp"
date="11/03/2012"
then if I try to insert another row that has url="http://google.com/img.jp" the sql statement will not insert to avoid duplicate rows for the same remote file.
I know I could probably do this by first doing a SELECT and checking for whether a row already exists, however I would like to check if this is possible at the point of insertion to make things more robust/clean.
You can (and should, for data integrity reasons) add a unique constraint to the url column by adding UNIQUE(url) to the SQL create statement for your table or just adding unique after not null. This will cause an insertion to fail when inserting a row where the URL is already in the table. Then you can use insertWithOnConflict to check for the result of the insert.
First of all, I would add the UNIQUE keyword to the url column.
Then, when inserting, use this INSERT OR IGNORE instead of INSERT.