Android database question, need advice and suggestions - android

I currently facing problem where user have the choice to enter several working experience, where every working experience include several details (company name, duration, roles etc)
I don't know how should I store it since users have the freedom to add as many working experience as they have.
I have a table as below
private static final String DATABASE_CREATE_WORKEXP =
"create table workexp (work_id integer primary key, _id integer not null," +
"workcompany text, workduration text, workrole text, workskills text" +
"workproject text, worksalary integer, workreason text," +
"foreign key(_id) references user(_id));";
I have a problem while trying to insert/update and delete entries as company name can be duplicated.
Any advice or suggestion?
I'm glad to provide more info for you to help me.
THANKS!

If you want to avoid duplicate company names, they should be managed in their own table. Then you'll have to add some UI to pick a company name in the table of existing companies or create a new one.

every db table should have an id field, which has the primary_key attribute. the id is unique and can not be dublicated. never use a name as primary key unless it is not unique.
Before implementing databases in android think about your idea.
First you can store the data on the device. But this solution has a big disatvantage. If your app is deleted from the device all data is lost.
On the other hand you can store the data online, on a webserver and access it through a REST API which I think is a more comfortable solution. It is up to you.

Related

Database tool for Android other than SQLite?

I am new to Android programming and I have made a few simple apps which use SQLite to store user's data. Now I am working on a little more complex app in which I need to implement many-to-many relationship among the tables.
So basically, I have three layers (3 Tables) that would be connected to each other and I can't find a good tutorial or any documentation on how to do it. I've spent weeks on researching this. I also looked into realm-database but it's complicated for many-to-many table setup.
So is there any easier solution to this for a beginner? Or is there another tool that I can use to accomplish my task. Any suggestions would be helpful. Thank you :)
Your example isn't a many to many relationship. It's a one to many, each country can only exist in one continent and each state in only one country.
You can get the structure you want by adding a reference to the parent type's ID.
CREATE TABLE continent (
_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
)
CREATE TABLE country (
_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
continentId INTEGER NOT NULL,
name TEXT NOT NULL,
FOREIGN KEY continentId REFERENCES continent(_id)
)
CREATE TABLE state (
_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
countryId INTEGER NOT NULL,
name TEXT NOT NULL,
FOREIGN KEY countryId REFERENCES country(_id)
)
To select all the countries on a continent, just ask SQL using the correct ID.
SELECT *
FROM country
WHERE continentId = ?
Or you can join them together.
SELECT *
FROM continent
JOIN country ON continent._id = country.continent
JOIN state ON country._id = state.countryId
You can do many-to-may relationships with SQLite. For the example shown you just need some XREF tables. For example (pseudocode):
Table CONTINENT(
ContinentID
,ContinentName
)
Table COUNTRY(
CountryID
,CountryName
)
Table CONTINENT_COUNTRY_XREF (
Continent_Country_XrefID
,ContinentID
,CountryID
)
Hope this helps.
Yes you can use Ultralite database from SAP. It supports joins as well.
More details here
http://scn.sap.com/community/developer-center/mobility-platform/blog/2012/08/23/how-to-open-an-ultralite-db-udb
To connect two tables in a many to many relationship, create a third table with three columns. The first column is just a standard is for the primary key. The other two columns are secondary keys into the two original tables. Googling " many to many relationship" will provide more details.

One foreign key referring Multiple column (Notification Concept)

I am new in programming, and I want to ask question regarding database schema (I'm using SQLite Database for Android Development)
I have some table, let say :
MsMember
MemberId
Password
MsGroup
GroupId
GroupName
MsAnnouncement
AnnouncementId
AnnouncementName
MsComment
CommentId
CommentContent
MsTodolist
TodolistId
TodolistTitle
And I want everytime a new row has been inserted to (at least one of) all five tables above, it will create a notification to user, as far as I know, with this concept, I should create a table to store every detail of notification then shows it to user..
And my best opinion so far is I create a table let say MsNotification, then to connect all five tables with this MsNotification I should have foreign key referring to each table..
My Question is would it be possible (and effective) to have a column that has more than one references?
Example :
Foreign key (SourceId) Referring MsMember (MemberId),
Foreign key(SourceId) referring MsComment (CommentId),
Foreign key (SourceId) referring MsAnnouncement (AnnouncementId), and so on.
or is there any better way to implement this concept?
Thank you in advance
No ,you can not assign single foreign key to multiple column .
But you can put multiple foreign key in single table

data duplication in sqlite with android app

i am getting information from user in sqlite database.
But when i insert same record which is already in database it is added again.
how i can stop duplication of record in sqlite. I am developing this in android.
I am using mobile number as primary key. still it add that record in database.
Please suggest me appropriate solutions.
Thanks in advanced.
Be aware of the limitations of REPLACE or INSERT OR REPLACE as these will overwrite any custom data your app user has added to these rows in the database - it is not as advanced as UPSERT in other SQL databases.
As mentioned in a previous post you really need to identify what the primary key could be and use this information to either update old data or to remove an old row before inserting the fresh one.
If this is not possible then you could always DELETE FROM my_table or DROP my_table before running the insertions so that there will be no duplicates. This will (for better or worse) also make sure that data that is missing from new imports is not left lying around in your app.
make sure you have set your phone number as Primary Key at the time you created the table.
for example:
String query = "CREATE TABLE IF NOT EXISTS PhoneBook ("+
"TelNum VARCHAR(100) PRIMARY KEY NOT NULL,"+
"Address TEXT);";
db.execSQL(query);
and in case you want to enforce foreign keys defined in your table then call the following method before doing anything in your database
db.execSQL("PRAGMA foreign_keys = ON;"); //enforcing FK
Use REPLACE INTO keyword:
REPLACE INTO my_table (pk_id, col1) VALUES (5, '123');
This automatically identifies the primary key and finds a matching row to update, inserting a new one if none is found.

Android SQLite, some SQL basics

I am following this tutorial: http://www.codeproject.com/KB/android/AndroidSQLite.aspx
I must be overthinking this SQLite stuff (in the past my domain server would automatically initialize databases I requested, and I could do queries when desired. never put one together from scratch)
I have some questions about their onCreate function. I never recall using a
CREATE TRIGGER command in my SQL
I only need to create one table with 2 or 3 columns (if you count the primary key)
I should just be able to do
db.execSQL("CREATE TABLE" + tableName +"("+colID+"INTEGER PRIMARY KEY,"+columnName+"TEXT)");
correct?
Do I need a "Trigger" and a "View" ?
If you just need a place to store some data - then Table is enough. But if your logic is more complicated then you'll need additional stuff. Also note that some Triggers are not supported by SQLite: Info from here
You not need to create TRIGGER. Unless it is required. Here is how I implemented in one of my project. Hope this help.
https://github.com/gopalB/FeedReader/blob/master/src/com/feedReader/provider/FeedDB.java
If you do not need a Trigger or a View, then you do not need to create them. It appears that the tutorial is just explaining some of the things you can do.
if SQLite TRIGGER and VIEW are similar to what they're used for in MySQL then no, they are not necessarily for what you're trying to accomplish.
VIEWs are useful when you have complex queries (like when using JOINs to join data from multiple tables).
TRIGGERSs are conditions that are run when you modify a table. (like using UPDATE, or INSERT)
As written, your create statement won't work because of a lack of whitespace. Try:
db.execSQL("CREATE TABLE " + tableName +" (" + colID + " INTEGER PRIMARY KEY, " + columnName + " TEXT)");

Sqlite different column names, but with same values

Here is my current SQLite table:
CREATE TABLE formula (_id INTEGER PRIMARY KEY AUTOINCREMENT,suggest_text_1 TEXT,formula TEXT,category TEXT);
I want to integrate search suggestions in an Android app, but I need a SUGGEST_COLUMN_INTENT_DATA_ID and I want my _id to represent it. But I also need a _id for the content provider. So is there a way where I could have SUGGEST_COLUMN_INTENT_DATA_ID and store the current and future values of _id in it? Would I use an alias?
Would I use an alias?
This is possible but it depends on your exact requirements. See my answer to this question to see if this applies to you. Android: column '_id' does not exist problem

Categories

Resources