Guest User

Untitled

a guest
Dec 10th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. public class gestionedb {
  2. static final String KEY_RIGAID = "_id";
  3. static final String KEY_EMAIL = "email";
  4. static final String KEY_PASSWORD = "password";
  5. static final String KEY_NICKNAME = "nickname";
  6. static final String TAG = "gestionedb";
  7. static final String DATABASE_NOME = "superchat5db";
  8. static final String DATABASE_TABELLA = "users";
  9. static final int DATABASE_VERSIONE = 1;
  10.  
  11. 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);";
  12.  
  13. final Context context;
  14. DatabaseHelper DBHelper;
  15. SQLiteDatabase db;
  16.  
  17. public gestionedb(Context ctx){
  18. this.context = ctx;
  19. DBHelper = new DatabaseHelper(context);
  20. }
  21.  
  22. private static class DatabaseHelper extends SQLiteOpenHelper {
  23. DatabaseHelper(Context context) {
  24. super(context, DATABASE_NOME, null, DATABASE_VERSIONE);
  25. }
  26. @Override
  27. public void onCreate(SQLiteDatabase db) {
  28. try {
  29. db.execSQL(DATABASE_CREAZIONE);
  30. }
  31. catch (SQLException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35.  
  36. @Override
  37. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  38. Log.d("Test", "in onUpgrade. Old is: " + oldVersion + " New is: " + newVersion);
  39. }
  40. }
  41.  
  42. public gestionedb open() throws SQLException {
  43. db = DBHelper.getWritableDatabase();
  44. return this;
  45. }
  46.  
  47. public void close() {
  48. DBHelper.close();
  49. }
  50.  
  51. public Cursor obtainallusers() {
  52. return db.query(DATABASE_TABELLA, new String[] {KEY_RIGAID, KEY_EMAIL, KEY_NICKNAME}, null, null, null, null, null);
  53. }
  54.  
  55. public Cursor obtainuser(long rigaId) throws SQLException {
  56. Cursor mCursore = db.query(true, DATABASE_TABELLA, new String[] {KEY_RIGAID, KEY_EMAIL, KEY_NICKNAME}, KEY_RIGAID + "=" + rigaId, null, null, null, null, null);
  57. if (mCursore != null) {
  58. mCursore.moveToFirst();
  59. }
  60. return mCursore;
  61. }
  62.  
  63. public long adduser(String email, String password, String nickname) {
  64. ContentValues initialValues = new ContentValues();
  65. initialValues.put(KEY_EMAIL, email);
  66. initialValues.put(KEY_PASSWORD, password);
  67. initialValues.put(KEY_NICKNAME, nickname);
  68. return db.insert(DATABASE_TABELLA, null, initialValues);
  69. }
  70.  
  71. public boolean deleteuser(long rigaId) {
  72. return db.delete(DATABASE_TABELLA, KEY_RIGAID + "=" + rigaId, null) > 0;
  73. }
  74.  
  75. public boolean updatenickname(long rigaId, String nickname) {
  76. ContentValues args = new ContentValues();
  77. args.put(KEY_NICKNAME, nickname);
  78. return db.update(DATABASE_TABELLA, args, KEY_RIGAID + "=" + rigaId, null) > 0;
  79. }
  80.  
  81. public boolean updatepsw(long rigaId, String password){
  82. ContentValues args = new ContentValues();
  83. args.put(KEY_PASSWORD, password);
  84. return db.update(DATABASE_TABELLA, args, KEY_RIGAID + "=" + rigaId, null) > 0;
  85. }
  86.  
  87. gestionedb db = new gestionedb(this);
  88.  
  89. db.open();
  90. long id = db.adduser("prova@gmail.com", "prova123", "prova1");
  91. id = db.adduser("prova2@gmail.com", "prova123", "prova2");
  92. db.close();
  93.  
  94. db.open();
  95. Cursor c = db.obtainallusers();
  96. if (c.moveToFirst()){
  97. do{
  98. Toast.makeText(this,
  99. "id: " + c.getString(0) + "n" +
  100. "Email: " + c.getString(1) + "n" +
  101. "Nickname: " + c.getString(2),
  102. Toast.LENGTH_LONG).show();
  103. } while (c.moveToNext());
  104. }
  105. db.close();
  106.  
  107. }
  108.  
  109. 2018-12-10 19:11:05.175 17701-17701/com.example.lorenzo.superchat5 E/AndroidRuntime: FATAL EXCEPTION: main
  110. Process: com.example.lorenzo.superchat5, PID: 17701
  111. 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
  112. #################################################################
  113. Error Code : 1 (SQLITE_ERROR)
  114. Caused By : SQL(query) error or missing database.
  115. (no such table: users (code 1): , while compiling: SELECT _id, email, nickname FROM users)
Add Comment
Please, Sign In to add comment