How to add a value after being logged in? - android

I am working in an android studio project and I have a login activity,I created database using SharedPreferences and SQLite;I have created a table which has columns id,email,password,check_in,check_out.
I made it possible to login after I register a user;but I do not know how to insert a data in check_in cell of logged user.
So my problem is that how to make possible to add the time of check_in in table in database after logged in?
And how I can the see the table created using SQLite?

SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("check_in", value);
contentValues.put("check_out", value);
// this will insert if record is new, update otherwise
db.insertWithOnConflict(TABLE, null, contentValues, SQLiteDatabase.CONFLICT_REPLACE);

I made it possible to login after I register a user;but I do not know
how to insert a data in check_in cell of logged user.
You would use the SQLiteDatabase's update method something along the lines of :-
ContentValues cv = new ContentValues();
cv.put("check_in",System.currentTimeMillis());
SQLiteDatabase db = this.getWritableDatabase();
db.update("your_table_name",cv,"email=? AND user=?",new String[]{email,user});
And how I can the see the table created using SQLite?
You can
install a product that provides this ability,
copy the database via adb or tools that utilise adb, so that a product that can look at SQLite databases can access the copied file,
extract a cursor from the table and then use this as the source for displaying the data e.g you could use something like
:-
SQLiteDatabae db = this.getWriteableDatabase();
Cursor csr = db.query("your_table_name",null,null,null,null,null,null);
Databaseutils.dumpCursor(csr);
csr.close();

Related

Data insertion failed after wiped out app data emulator

Inserting data in SQlite from EditText
Code was working well before i wiped out data from emulator now its giving me Null Pointer Exception, Kindly figure where actual problem lies. Thanks
Data is being inserted from here ]
public long insert(String name, String password, String role, String unique) {
sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_USERNAME, name);
contentValues.put(KEY_PASSWORD, password);
contentValues.put(KEY_ROLE, role);
contentValues.put(KEY_UNIQUE, unique);
long result = sqLiteDatabase.insert(DOCTOR_TABLE,
null,
contentValues);
sqLiteDatabase.close();
return result;
}
SQLite Database class
Well. It's an obvious.
As you mentioned you have wiped the data, so the table which is located in the android app's own data folder will be deleted too. So you must check whether it exists or not.
Call to Getting your table returns null.
So you inserting your value to nothing.
So You can just alter the code to see if the table exists and if not create it.
Basically you just call,
CREATE TABLE IF NOT EXISTS method(SQLite 3.3 and above support IF NOT EXISTS). When you first call it, if not existed, it will create that table but later it will return the existing table with same name.
In one line- check or get the correct table before you do any operation.

Insert data into two table at once using foreign key

Database Design
Above pic shows my database design. I want to insert data into these two tables. Some part into one table and some into second table using foreign key.
Also how can i delete data from two tables at once using foreign key.
public boolean insertToBlockList(String originatingAddress,String messageBody){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(ORIGINATING_ADDRESS,originatingAddress);
contentValues.put(MESSAGE_BODY, messageBody);
db.insert(TABLE_BLOCK_LIST,null,contentValues);
return true;
i wrote this code but doesn't work for me. kindly help.
how to select and show data from these two tables using foreign key?
SELECT *
FROM table1
JOIN table2
ON table1.column_name=table2.column_name;
Read more: http://www.w3schools.com/sql/sql_join_inner.asp
You have to execute two insert queries as told by #Prerak. However you have to use sql transactions for better result.

Empty sqlite database when app send on another device

I am developing Android app and I have SQLite database in it. I have inserted records in database. Now when I am sending my app into another device there is empty database!. Please help me
public void createPrincipal(CollegePrincipal principal){
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(PRINCI_NAME, principal.getName());
values.put(PRINCI_QUALI, principal.getQalification());
values.put(PRINCI_INTRO, principal.getIntro());
// insert row
long princi_id = db.insert(TABLE_PRINCI, null, values);
Log.e("princi id", String.valueOf(princi_id));
}
when ever you create data base it will in the local store of your device , And when you transfer app it wont care you local data you need store those data variables in you code to get in the another device.

How to insert data into two tables at once using SQLiteDatabase in Android?

So lets say I have this code:
SQLiteDatabase sqlDB = database.getWritableDatabase();
long iD = 0;
iD = sqlDB.insert(Table1, null, TestV);
Can I somehow rewrite this to insert into two tables instead of one? My problem is that 'values' returns 3 values from one table, and 1 value from another. Therefore, the 1 value that isn't in 'Table1' sets off an error that the column cant be found in 'Table1.' Is there a way to do this? If not then should I do two separate inserts? I am confused as how to insert data into two tables in one database (Tables are Relational: one has a foreign key).
From the documentation for INSERT in SQLite:
Note that the flowchart doesn't give you an opportunity to post into more than one table at once. SQLiteDatabase.insert() is a convenience method for using the INSERT statement.
You can simulate this using database transaction:
SQLiteDatabase db = getWritableDatabase();
db.beginTransaction();
try {
db.execSQL("insert into table1...");
db.execSQL("insert into table2...");
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
You may need to choose the order of the inserts if there are dependencies between the values inserted, for instance if there's a foreign key relationship: ( insert to table1 and then table2, or first insert to table2...)

Is it possible to apply key's (Primary key, candidate key etc) in android while using database

I am new to android and i have just learned how to use the database and i have a couple of questions:
How can we apply keys on the data in the database in android just like we give in Oracle database? Is it possible? If no please tell me why.
When i am deleting the first row in a database (Whose id is '1') the below rows id's are not coming in serial number i.e., id for the second row (Now first row) is '2' why not '1'.
Thank you
ok, from first,
"How can we apply keys on the data in the database in android just like we give in Oracle database? Is it possible? If no please tell me why."
Ans - We can apply the all rule which one applies to a any other database, like oracle, mysql etc... So you can have both concept of primary key and foreign key in Android's SQLite database.
2.When i am deleting the first row in a database (Whose id is '1') the below rows id's are not coming in serial number i.e., id for the second row (Now first row) is '2' why not '1'?
Ans : as per the database rules whenever you delete any records from row then its key is remains same, its not changed and whatever data after that records are also has a same key or ID so whenever you want to access that data the ID or key remain same.
EDIT: and If you want to modify that key or ID you can use UPDATE query for that.
EDIT: update(String table, ContentValues values, String whereClause, String[] whereArgs)
Updating Values
To execute an update statement, we have two ways:
1. To execute db.execSQL
2. To execute db.update method:
public int UpdateEmp(Employee emp)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(colName, emp.getName());
cv.put(colAge, emp.getAge());
cv.put(colDept, emp.getDept());
return db.update(employeeTable, cv, colID+"=?",
new String []{String.valueOf(emp.getID())});
}
The update method has the following parameters:
1. String Table: The table to update a value in
2. ContentValues cv: The content values object that has the new values
3. String where clause: The WHERE clause to specify which record to update
4. String[] args: The arguments of the WHERE clause
Convenience method for updating rows in the database.
Hope you will understand it.
Thanks,

Categories

Resources