how to generate an automatic id(incremental) in android? - android

i have one orderid field in android form .. now i want to add a number along with the content of orderid such that each time the number should be in incremental manner..
for example
if user input 123 in orderid then i want to store 123100 in database
similarly on next run i want to store 123101 in database ..and so on...how will i do this
plz help

Create a field with INTEGER PRIMARY KEY as a type with the name _id. This field will increase by one each time a value is added into the database.

Related

SQLite in Android - How to generate User IDs with date and sequential number per day

I currently have an app where I store user data in a SQLite database, and one of my fields is a User ID. I would like to add an option to auto-generate User IDs in an mmddyyXXX format, where XXX is a sequential number per user that resets every day.
Does anyone know how I would approach this? I looked at some of the other similar questions, but they don't seem to be helpful.
This is not complicated at all. If your'e similar with SQLite in android just take the date and the userId using a SELECT and generate that string yourself.
If the XXX is not the userId just save another table containing 'tokens' for users. every userId would have a 'token'.
Every new day just change the contents of this table.
I believe you could use a TRIGGER that will generate the userid when a row is inserted.
The following may suit :-
CREATE TRIGGER IF NOT EXISTS newuserid AFTER INSERT ON users
BEGIN
UPDATE users SET userid = strftime('%m%d',date('now'))||substr(strftime('%Y',date('now')),3)||
(
SELECT CAST(substr('000',1,3-length(count()+1)) AS TEXT)||CAST((count()+1) AS TEXT)
FROM USERS
WHERE substr(userid,1,6) = strftime('%m%d',date('now'))||substr(strftime('%Y',date('now')),3)
)
WHERE userid IS NULL;
END;
The trigger is named newuserid
userid is the column for the auto-generated id. The above relies upon it being NULL so it cannot be a PRIMARY INDEX.
There is no reliance upon other columns.
Testing
Starting with an empty table :-
Inserting 4 rows using INSERT INTO users VALUES(null,'This is a new user'); results in :-
To check for another date the rows are adjusted from 041018??? to 040918??? as per :-
4 more rows are inserted using INSERT INTO users VALUES(null,'This is a new user');, resulting in :-
Note this answer isn't intended to be fail-safe but rather the basis of the concept for the answer.

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...

How to Use CONTAINS keyword in Sqlite Database

what i am trying to use is that i want to select cities from my LocationByCity table in sqlite database each time As user inters search word in a search box,
for example if user enters F i want all the CityNames Starting From F ,so how can i do that?
Thanx in advance.
I'd have to check up, but I believe you want to use the LIKE keyword.
Syntax somthing along the lines of:
Select Fieldname From Tablename where fieldname LIKE ('%$myvar%');
where the % signs are wildcard characters.
Not sure if sqlite has fulltext indexing so you should be aware that can get very slow depending on how many rows you have in the table.

Help with MySQL Friends List

I am making an Android app that will use a friends list. As far as I know, you can't store an array in a MySQL table, so it seems that I will have to make a separate table from my "Users" table and call it, say, "Friends". This table will just have Friend1 and Friend2.
My question is the preferred way to store this table. I can use the UserName field (string) from my "Users" table to store them, or I could use the UserID field (integer) from my "Users" table. Using the ID would make the table smaller because the small integers take up more space than the string, but at the same time, I access this data mainly using the UserName field (so I have to query the Users table to get the UserID from the Users table).
Which method is preferred for a MySQL table? Using the users name directly so I do not have to find the UserID from the Users table, or saving the table as two integers, and querying to find the ID from the UserName? Thanks.
Store two userID keys from the users table.
Let's say that you change a name of a contact from "Guy from bar" to "Mr. McMoneypants". If this contact was a friend, it will still show up as "Guy from bar" even after the change.
Try to keep data from living in multiple places.
The preferred method is to use the UserID from the Users table in the Friends table as a way to reference that user. That way, as Phillip says, the User can change their name and you only have to change it in one place. Plus, as you say, your Friends table will take up less space with a numeric column as compared to a string column.
And in regards to "(so I have to query the Users table to get the UserID from the Users table)", the following query is not too cumbersome:
SELECT FriendName
FROM Users Natural Left Join Friends
WHERE UserName = 'Ralph';
As far as your query is concerned, you never had to deal with the UserID column.
That's not that much harder than your method:
SELECT FriendName
FROM Friends
WHERE UserName = 'Ralph';

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