Last active
May 17, 2017 20:25
-
-
Save SarahBourgeois/ddd8578486dd1f2fdaf3a512cc10c20b to your computer and use it in GitHub Desktop.
Create a database and table with Sql Lite for your android project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package model; | |
import android.content.Context; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteDatabase.CursorFactory; | |
import android.database.sqlite.SQLiteOpenHelper; | |
/** | |
* Create by sarahb | |
* Create SQL table : | |
*/ | |
public class MySQLLiteBDD extends SQLiteOpenHelper { | |
//=================================== | |
// Definition column tables | |
// ================================== | |
// definition of the table USER (for example) | |
private static final String TABLE_USER = "table_user"; | |
private static final String COL_LOGIN = "Login"; | |
private static final String COL_PWD = "Password"; | |
private static final String COL_NAME = "Name"; | |
private static final String COL_SURNAME = "Surname"; | |
private static final String COL_MAIL = "Mail"; | |
// ================================== | |
// create table_user | |
// =================================== | |
private static final String CREATE_BDD = "CREATE TABLE " + | |
TABLE_USER + " (" + | |
COL_LOGIN + " TEXT PRIMARY KEY, " + | |
COL_PWD + " TEXT NOT NULL, " + | |
COL_NAME + " TEXT NOT NULL, " + | |
COL_SURNAME + " TEXT NOT NULL, " + | |
COL_MAIL + " TEXT NOT NULL);"; | |
// constructor | |
public MySQLLiteBDD(Context context, String name, CursorFactory factory, int version) { | |
super(context, name, factory, version); | |
} | |
// ======================================== | |
// Create general database | |
// ======================================== | |
@Override | |
public void onCreate(SQLiteDatabase db) { | |
db.execSQL(CREATE_BDD); | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | |
db.execSQL("DROP TABLE " + TABLE_USER + ";"); | |
onCreate(db); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package model; | |
/** | |
* Created by sarahb | |
*/ | |
public class User { | |
private String login; | |
private String password; | |
private String name; | |
private String surname; | |
private String mail; | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getSurname() { | |
return surname; | |
} | |
public void setSurname(String surname) { | |
this.surname = surname; | |
} | |
public String getMail() { | |
return mail; | |
} | |
public void setMail(String mail) { | |
this.mail = mail; | |
} | |
public String getLogin() { | |
return login; | |
} | |
public void setLogin(String login) { | |
this.login = login; | |
} | |
public String getPassword() { | |
return password; | |
} | |
public void setPassword(String password) { | |
this.password = password; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package model; | |
import android.content.ContentValues; | |
import android.content.Context; | |
import android.database.Cursor; | |
import android.database.sqlite.SQLiteDatabase; | |
/** | |
* Created by sarahb | |
*/ | |
public class UserBDD { | |
// variable we'll use | |
private static final int VERSION_BDD = 1; | |
private static final String NOM_BDD = "managersBDD.db"; | |
private static final String TABLE_USER = "table_user"; | |
private static final String COL_LOGIN = "Login"; | |
private static final int NUM_COL_LOGIN = 0; | |
private static final String COL_PWD = "Password"; | |
private static final int NUM_COL_PWD = 1; | |
private static final String COL_NAME ="Name"; | |
private static final int NUM_COL_NAME = 2; | |
private static final String COL_SURNAME ="Surname"; | |
private static final int NUM_COL_SURNAME= 3; | |
private static final String COL_MAIL = "Mail"; | |
private static final int NUM_COL_MAIL = 4; | |
private SQLiteDatabase bdd; | |
private MySQLLiteBDD maBaseSQLite; | |
// Cerate table and BDD | |
public UserBDD(Context context) { | |
maBaseSQLite = new MySQLLiteBDD(context, NOM_BDD, null, VERSION_BDD); | |
} | |
// open BDD | |
public void open() { | |
bdd = maBaseSQLite.getWritableDatabase(); | |
} | |
// close BDD | |
public void close() { | |
bdd.close(); | |
} | |
public SQLiteDatabase getBDD() { | |
return bdd; | |
} | |
//================================= | |
// Code sample for user's accounts | |
//================================== | |
// Create new Account | |
public long insertNewUser(User user) { | |
ContentValues values = new ContentValues(); | |
values.put(COL_LOGIN, user.getLogin()); | |
values.put(COL_PWD, user.getPassword()); | |
values.put(COL_MAIL, user.getMail()); | |
values.put(COL_NAME, user.getName()); | |
values.put(COL_SURNAME, user.getSurname()); | |
return bdd.insert(TABLE_USER, null, values); | |
} | |
// Update password | |
public int updatePassword(String login, User user) { | |
ContentValues values = new ContentValues(); | |
values.put(COL_PWD, user.getPassword()); | |
return bdd.update(TABLE_USER, values, COL_LOGIN + " = " + login, null); | |
} | |
// Verify if user exist to connect him | |
public User getUserWithLogin(String login) { | |
// thanks to his login | |
Cursor c = bdd.query(TABLE_USER, new String[]{COL_PWD, COL_LOGIN}, COL_LOGIN + " LIKE \"" + login + "\"", null, null, null, null); | |
return cursorToLivre(c); | |
} | |
// Convert Cursor to a User object | |
private User cursorToLivre(Cursor c) { | |
if (c.getCount() == 0) { | |
return null; | |
} else { | |
c.moveToFirst(); | |
User user = new User(); | |
user.setLogin(c.getString(NUM_COL_LOGIN)); | |
user.setPassword(c.getString(NUM_COL_PWD)); | |
c.close(); | |
return user; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment