Android Spinner How to Show Current Value From SQL - android

I have an android app using sql lite with two tables. Table_1 can be referenced by Table_2 multiple times. Table_2 references table1 by the primary key from table_1. I have a form to edit the data in the second table. I have a spinner in this form that loads the name column from table_1. Everything works good on the database side, however I am unable to load a "default" value for the spinner box. The spinner box will always show the first name in table_1. I would like to use setSelection() method to set the position to the currently selected name. Since the data in table_1 can be modified, deleted or updated the primary key does not match the row position in the database, I can not simply set the setSelection to the primary key.
I have tried the getPosition() method but I get an error that a cursor can not return an integer value.

Now working. Ended up creating an arraylist for the primary key.
position = arraylist.indexOf(String.valueOf(primarykeyID));
The key to working was converting the integer value to a string value

Related

Is the sequence on how we type a Sqlite query to Create a column with attributes really matters

I'm new to coding android apps with Sqlite
I have three questions
I created this Sqlite table with columns with attributes like
TEXT
NOT NULL
UNIQUE
DEFAULT regular
Q1) I'm skeptical to know whether is there any order on how to delare attributes for a row
Q2) If I declare any row to have a default value like will the text be still inserted even though the user inserts something in that row, if yes, then how to insert a default value if the user dosen't inset any value in a specific Row
Q3)Is my code below correct ? What I desire is the row KEY_TAGNAME to be unique, not null and to have a value if the row doesn't get any data while a insert statement occurs for that table.
private static final String CREATE_TAGTABLE_SQL=
"CREATE TABLE " + DATABASE_TABLE_TAG
+ " ("
+ KEY_TAGROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_TAGNAME + " TEXT NOT NULL UNIQUE DEFAULT regular"
+ ");";
A1: I couldn't find anything in the documentation that clearly says anything about the order of the attributes but I tried to create a couple of tables in a test database I created to check this and it seems that if you do not follow the proper order you will get a syntax error.
(tried with create table test (key1 integer primary key autoincrement) which works correctly but create table test1 (key1 primary key integer autoincrement) gives a near "integer": syntax error:
A2: You can have a default value inserted that will be put if the user does not input anything there. The keyword is default and you will find more info here on how to use it (TL;DR upon table's creation you will create the row as usual and in the end put a DEFAULT and next to it the value. Please check the link on this)
A3 Your query is correct and will do the things you mention.

Sql query not giving desire result for android application?

I have two tables naming :
Custom_fields -->(id,name,sequence)
Custom_field_value -->(id,user_id,custom_field_id,custom_field_value)
The primary key of Custom_fields table is present in Custom_field_value table.Every user will have data associated with custom fields but not for all the fields.I need to make a query to find out the values of custom fields for a particular user id.For every user I need all custom field and their values(even if value is not present).I created the following query:
"SELECT alumni_custom_fields. _id,
alumni_custom_fields.field_label as key ,
alumni_custom_field_data.custom_field_value as value
FROM alumni_custom_fields LEFT JOIN alumni_custom_field_data
ON alumni_custom_fields._id=alumni_custom_field_data.custom_field_id " +
"WHERE alumni_id ='"+alumniId+"' order by sequence";
But its only giving the custom fields which has value and not giving me the custom fields whose value is none or empty.
Please help me fix this query.
Please try this.....
SELECT alumni_custom_fields. _id,alumni_custom_fields.field_label as key ,alumni_custom_field_data.custom_field_value as value FROM alumni_custom_fields LEFT JOIN alumni_custom_field_data ON alumni_custom_fields._id=alumni_custom_field_data.custom_field_id and alumni_id ='"+alumniId+"' order by sequence
This will surely solve your problem...

Not able to add unique rows using sqlite in android

I am working on Android 2.3 Simulator.
I have some 5 unique categories...
I need to add these categories in my table....
I added it using primary key like this
Create table if not exists test (category integer primary key, value text) ;
But if i execute this query twice
insert into test(category, value) values(1,"0");
Then it is adding this twice
Even if i use unique like this
Create table if not exists test (category integer unique, value text) ;
Then also it is adding the row twice...
How to make a row unique?
dont add record manually ie for primary key, you can assign primary key as auto increment, let system will add record.

Sqlite on Android handling of NaN value

I'm trying to establish if I have found a bug in sqlite or if it is just with my understanding (as I haven't had a lot of experience with it).
(And, yes I have checked the sqlite bug tracker.)
I have an Android where I want to store doubles in the database. I also want store a record where the value may not be set yet (ie invalid). To track this state I want to store Double.NaN.
So when I create the database table as such
create table Example (myRecordId integer primary key autoincrement, someString text not null, problemDouble REAL);
Insert a record where the problemDouble is Double.NaN and then access via Cursor.getDouble(...) I will get back a Java double with the value 0.0.
Now if I create the table this way (using text instead of real)
create table Example (myRecordId integer primary key autoincrement, someString text not null, problemDouble text);
Insert the record where the Java double is converted to a string and access it the same way I will get back a double with the value NaN.
I thought that that sqlite is supposed to be typeless but it appears to be messing around with the value when the column is a REAL. Or is it that sql REAL don't support a NaN value?
My assumption would be that sqlite is just converting the REAL values to strings in the background.
So would this inconsistency be considered a bug ?

Generating automatic ID

I want to generate automatic id of type ame100, ame101 and so on, so that as soon as I start my Android application, the next value of the series ame*** which is currently not in the database, should come automatically in edittext. So that on submitting the form I should have that value in database.
Make your primary key autoincrement, so that db will generate the next value.
Each time you need new id, just ask your databse for MAX(ID) from your table.
Increment it and set to edittext!
Note: this solution works only if you have one place of inserting the values.
I would suggest not showing the number before inserting, but create it after, when you already have generated id of the object.

Categories

Resources