Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class gestionedb {
- static final String KEY_RIGAID = "_id";
- static final String KEY_EMAIL = "email";
- static final String KEY_PASSWORD = "password";
- static final String KEY_NICKNAME = "nickname";
- static final String TAG = "gestionedb";
- static final String DATABASE_NOME = "superchat5db";
- static final String DATABASE_TABELLA = "users";
- static final int DATABASE_VERSIONE = 1;
- static final String DATABASE_CREAZIONE = "create table clienti (_id integer primary key autoincrement, " + "email text not null, password text not null, nickname text not null);";
- final Context context;
- DatabaseHelper DBHelper;
- SQLiteDatabase db;
- public gestionedb(Context ctx){
- this.context = ctx;
- DBHelper = new DatabaseHelper(context);
- }
- private static class DatabaseHelper extends SQLiteOpenHelper {
- DatabaseHelper(Context context) {
- super(context, DATABASE_NOME, null, DATABASE_VERSIONE);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- try {
- db.execSQL(DATABASE_CREAZIONE);
- }
- catch (SQLException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- Log.d("Test", "in onUpgrade. Old is: " + oldVersion + " New is: " + newVersion);
- }
- }
- public gestionedb open() throws SQLException {
- db = DBHelper.getWritableDatabase();
- return this;
- }
- public void close() {
- DBHelper.close();
- }
- public Cursor obtainallusers() {
- return db.query(DATABASE_TABELLA, new String[] {KEY_RIGAID, KEY_EMAIL, KEY_NICKNAME}, null, null, null, null, null);
- }
- public Cursor obtainuser(long rigaId) throws SQLException {
- Cursor mCursore = db.query(true, DATABASE_TABELLA, new String[] {KEY_RIGAID, KEY_EMAIL, KEY_NICKNAME}, KEY_RIGAID + "=" + rigaId, null, null, null, null, null);
- if (mCursore != null) {
- mCursore.moveToFirst();
- }
- return mCursore;
- }
- public long adduser(String email, String password, String nickname) {
- ContentValues initialValues = new ContentValues();
- initialValues.put(KEY_EMAIL, email);
- initialValues.put(KEY_PASSWORD, password);
- initialValues.put(KEY_NICKNAME, nickname);
- return db.insert(DATABASE_TABELLA, null, initialValues);
- }
- public boolean deleteuser(long rigaId) {
- return db.delete(DATABASE_TABELLA, KEY_RIGAID + "=" + rigaId, null) > 0;
- }
- public boolean updatenickname(long rigaId, String nickname) {
- ContentValues args = new ContentValues();
- args.put(KEY_NICKNAME, nickname);
- return db.update(DATABASE_TABELLA, args, KEY_RIGAID + "=" + rigaId, null) > 0;
- }
- public boolean updatepsw(long rigaId, String password){
- ContentValues args = new ContentValues();
- args.put(KEY_PASSWORD, password);
- return db.update(DATABASE_TABELLA, args, KEY_RIGAID + "=" + rigaId, null) > 0;
- }
- gestionedb db = new gestionedb(this);
- db.open();
- long id = db.adduser("prova@gmail.com", "prova123", "prova1");
- id = db.adduser("prova2@gmail.com", "prova123", "prova2");
- db.close();
- db.open();
- Cursor c = db.obtainallusers();
- if (c.moveToFirst()){
- do{
- Toast.makeText(this,
- "id: " + c.getString(0) + "n" +
- "Email: " + c.getString(1) + "n" +
- "Nickname: " + c.getString(2),
- Toast.LENGTH_LONG).show();
- } while (c.moveToNext());
- }
- db.close();
- }
- 2018-12-10 19:11:05.175 17701-17701/com.example.lorenzo.superchat5 E/AndroidRuntime: FATAL EXCEPTION: main
- Process: com.example.lorenzo.superchat5, PID: 17701
- java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lorenzo.superchat5/com.example.lorenzo.superchat5.ProvaDB}: android.database.sqlite.SQLiteException: no such table: users (code 1): , while compiling: SELECT _id, email, nickname FROM users
- #################################################################
- Error Code : 1 (SQLITE_ERROR)
- Caused By : SQL(query) error or missing database.
- (no such table: users (code 1): , while compiling: SELECT _id, email, nickname FROM users)
Add Comment
Please, Sign In to add comment