mirror of
https://github.com/pierre42100/ComunicAndroid
synced 2024-11-23 13:59:29 +00:00
Insert a new user in local database
This commit is contained in:
parent
1e0c867217
commit
5584840145
@ -15,7 +15,7 @@ public class UserInfo {
|
||||
private int id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private String imageURL;
|
||||
private String accountImageURL;
|
||||
|
||||
/**
|
||||
* Set the ID of the user
|
||||
@ -85,8 +85,8 @@ public class UserInfo {
|
||||
*
|
||||
* @param imageURL The URL of the image
|
||||
*/
|
||||
public void setImageURL(String imageURL) {
|
||||
this.imageURL = imageURL;
|
||||
public void setAccountImageURL(String imageURL) {
|
||||
this.accountImageURL = imageURL;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -94,7 +94,7 @@ public class UserInfo {
|
||||
*
|
||||
* @return The image URL of the account of the user
|
||||
*/
|
||||
public String getImageURL() {
|
||||
return imageURL;
|
||||
public String getAcountImageURL() {
|
||||
return accountImageURL;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package org.communiquons.android.comunic.client.data.UsersInfo;
|
||||
|
||||
import android.content.ContentValues;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
|
||||
@ -68,6 +69,107 @@ public class UsersInfosDbHelper {
|
||||
//Close cursor
|
||||
c.close();
|
||||
|
||||
//Close the database
|
||||
db.close();
|
||||
|
||||
return number_entries > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a user in the database
|
||||
*
|
||||
* @param user The user to insert in the database
|
||||
* @return -1 in case of failure / the id of the new entry else
|
||||
*/
|
||||
public int insert(UserInfo user){
|
||||
|
||||
//Get a database with write access
|
||||
SQLiteDatabase db = dbHelper.getWritableDatabase();
|
||||
|
||||
//Prepare the insertion
|
||||
ContentValues newValues = new ContentValues();
|
||||
newValues.put(UsersInfoSchema.COLUMN_NAME_USER_ID, user.getId());
|
||||
newValues.put(UsersInfoSchema.COLUMN_NAME_USER_FIRSTNAME, user.getFirstName());
|
||||
newValues.put(UsersInfoSchema.COLUMN_NAME_USER_LASTNAME, user.getLastName());
|
||||
newValues.put(UsersInfoSchema.COLUMN_NAME_USER_ACCOUNT_IMAGE, user.getAcountImageURL());
|
||||
|
||||
//Insert it
|
||||
long newRowId = db.insert(UsersInfoSchema.TABLE_NAME, null, newValues);
|
||||
|
||||
//Close the database
|
||||
db.close();
|
||||
|
||||
return (int) newRowId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a user from the database
|
||||
*
|
||||
* @param userID The ID of the user to retrieve
|
||||
* @return UserInfo about requested user / null in case of filaure
|
||||
*/
|
||||
public UserInfo get(int userID){
|
||||
|
||||
UserInfo result;
|
||||
|
||||
//Open database for access
|
||||
SQLiteDatabase db = dbHelper.getReadableDatabase();
|
||||
|
||||
//Prepare the request on the database
|
||||
String[] requestedFields = {
|
||||
UsersInfoSchema.COLUMN_NAME_USER_ID,
|
||||
UsersInfoSchema.COLUMN_NAME_USER_FIRSTNAME,
|
||||
UsersInfoSchema.COLUMN_NAME_USER_LASTNAME,
|
||||
UsersInfoSchema.COLUMN_NAME_USER_ACCOUNT_IMAGE
|
||||
};
|
||||
|
||||
//Set the conditions of the request
|
||||
String selection = UsersInfoSchema.COLUMN_NAME_USER_ID + " = ?";
|
||||
String[] selectionArgs = { ""+userID };
|
||||
|
||||
//Sort order
|
||||
String sortOrder = UsersInfoSchema._ID;
|
||||
|
||||
//Perform the request
|
||||
Cursor c = db.query(
|
||||
UsersInfoSchema.TABLE_NAME,
|
||||
requestedFields,
|
||||
selection,
|
||||
selectionArgs,
|
||||
null,
|
||||
null,
|
||||
sortOrder
|
||||
);
|
||||
|
||||
//Check for errors
|
||||
if(c.getCount() < 1)
|
||||
result = null;
|
||||
else {
|
||||
|
||||
//Initialize User object
|
||||
result = new UserInfo();
|
||||
|
||||
c.moveToFirst();
|
||||
|
||||
//Extract the information and record them
|
||||
result.setId(
|
||||
c.getInt(
|
||||
c.getColumnIndexOrThrow(
|
||||
UsersInfoSchema.COLUMN_NAME_USER_ID
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
//TODO : finish extraction
|
||||
}
|
||||
|
||||
//Close the cursor
|
||||
c.close();
|
||||
|
||||
//Close the database
|
||||
db.close();
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user