· 8 years ago · Jun 04, 2018, 06:24 PM
1package com.example.marco.biblioteca;
2
3 import android.content.ContentValues;
4 import android.content.Context;
5 import android.database.Cursor;
6 import android.database.SQLException;
7 import android.database.sqlite.SQLiteDatabase;
8 import android.database.sqlite.SQLiteOpenHelper;
9 import android.util.Log;
10 import android.widget.Toast;
11
12public class GestioneDb {
13
14 static final String KEY_RIGAID = "id";
15 static final String KEY_CODICE = "codice";
16 static final String KEY_AUTORE = "autore";
17 static final String KEY_COPIEDISPONIBILI = "copiedisponibili";
18 static final String KEY_COPIETOTALI = "copietotali";
19 static final String KEY_TITOLO = "titolo";
20 static final String KEY_CASAEDITRICE = "casaeditrice";
21 static final String TAG = "GestioneDB";
22 static final String DATABASE_NOME = "DB";
23 static final String DATABASE_TABELLA = "Libri";
24 static final int DATABASE_VERSIONE = 1;
25 static final String TABELLA_CREAZIONE =
26 "CREATE TABLE clienti (id integer primary key autoincrement, " + "codice text not null, autore text not null, copiedispinibili text not null, copietotali text not null, titolo text not null, casaeditrice text not null,);";
27
28
29 final Context context;
30 DatabaseHelper DBHelper;
31 SQLiteDatabase db;
32
33 public GestioneDb(Context ctx) {
34 this.context = ctx;
35 DBHelper = new DatabaseHelper(context);
36 }
37
38 private static class DatabaseHelper extends SQLiteOpenHelper {
39 DatabaseHelper(Context context) {
40 super(context, DATABASE_NOME, null, DATABASE_VERSIONE);
41 }
42
43
44 @Override
45 public void onCreate(SQLiteDatabase db) {
46 try {
47 db.execSQL(TABELLA_CREAZIONE);
48 } catch (SQLException e) {
49 e.printStackTrace();
50 }
51 }
52
53 @Override
54 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
55 Log.w(DatabaseHelper.class.getName(), "Aggiornamento database dalla versione " + oldVersion + " alla "
56 + newVersion + ". I dati esistenti verranno eliminati.");
57 db.execSQL("DROP TABLE IF EXISTS clienti");
58 onCreate(db);
59 }
60 }
61 public GestioneDb open() throws SQLException
62 {
63 db = DBHelper.getWritableDatabase();
64 return this;
65 }
66
67 public void close()
68 {
69 DBHelper.close();
70 }
71
72 public boolean inserisciLibro(int codice,String titolo,String casaEditrice,String autore,int copieDisponibili,int copieTotali)
73 {
74 ContentValues initialValues = new ContentValues();
75 if(copieDisponibili<=copieTotali) {
76 initialValues.put(KEY_CODICE, codice);
77 initialValues.put(KEY_TITOLO, titolo);
78 initialValues.put(KEY_CASAEDITRICE, casaEditrice);
79 initialValues.put(KEY_AUTORE, autore);
80 initialValues.put(KEY_COPIEDISPONIBILI, copieDisponibili);
81 initialValues.put(KEY_COPIETOTALI, copieTotali);
82 return true;
83 }
84 else{
85 return false;
86 }
87 }
88
89
90
91 public void cancellaLibro(long codice) {
92 Cursor mCursore = db.query(true, DATABASE_TABELLA, new String[] {KEY_RIGAID,KEY_COPIEDISPONIBILI,
93 KEY_COPIETOTALI}, null, null, null, null, null, null);
94 if (mCursore != null) {
95 mCursore.moveToFirst();
96 if (mCursore.getString(0)==mCursore.getString(1)){
97 db.delete(DATABASE_TABELLA, KEY_CODICE + "=" + codice, null);
98 }
99 }
100 }
101 public boolean prestaLibro(long codice) {
102 ContentValues args= new ContentValues();
103 int nuovoVal=0;
104 Cursor mCursore = db.query(true, DATABASE_TABELLA, new String[] {KEY_RIGAID,KEY_COPIEDISPONIBILI,}, KEY_CODICE + "=" + codice, null, null, null, null, null);
105 if (mCursore != null) {
106 mCursore.moveToFirst();
107 }
108 if (Integer.parseInt(mCursore.getString(0))!=0){
109 nuovoVal =Integer.parseInt(mCursore.getString(0));
110 nuovoVal=nuovoVal-1;
111 args.put(KEY_COPIEDISPONIBILI,nuovoVal);
112 db.update(DATABASE_TABELLA, args, null, null);
113 return true;
114 }
115 else {
116 return false;
117 }
118 }
119
120 public Cursor ottieniTuttiILibri() {
121 return db.query(DATABASE_TABELLA, new String[]{KEY_RIGAID,KEY_CODICE, KEY_TITOLO, KEY_CASAEDITRICE, KEY_AUTORE,KEY_COPIEDISPONIBILI,KEY_COPIETOTALI}, null, null, null, null, null);
122 }
123
124 public Cursor ottieniLibro(long codice) throws SQLException {
125 Cursor mCursore = db.query(true, DATABASE_TABELLA, new String[]{KEY_RIGAID,KEY_CODICE, KEY_TITOLO, KEY_CASAEDITRICE, KEY_AUTORE,KEY_COPIEDISPONIBILI,KEY_COPIETOTALI}, KEY_CODICE + "=" + codice, null, null, null, null, null);
126 if (mCursore != null) {
127 mCursore.moveToFirst();
128 }
129 return mCursore;
130 }
131 public boolean rendiLibro(long codice) throws SQLException {
132 ContentValues args = new ContentValues();
133 int nuovoVal = 0;
134 Cursor mCursore = db.query(true, DATABASE_TABELLA, new String[]{KEY_RIGAID, KEY_COPIEDISPONIBILI, KEY_COPIETOTALI}, KEY_CODICE + "=" + codice, null, null, null, null, null);
135
136 if (mCursore != null) {
137 mCursore.moveToFirst();
138 }
139 if (Integer.parseInt(mCursore.getString(0)) < Integer.parseInt(mCursore.getString(1))) {
140 nuovoVal = Integer.parseInt(mCursore.getString(0));
141 nuovoVal = nuovoVal + 1;
142 args.put(KEY_COPIEDISPONIBILI, nuovoVal);
143 db.update(DATABASE_TABELLA, args, KEY_CODICE + "=" + codice, null);
144 return true;
145 }
146 else{
147 return false;
148 }
149 }
150
151
152 public boolean aggiornaLibro(long codice, String titolo,String casaEditrice,String autore,int copieDisponibili,int copieTotali) {
153 ContentValues args = new ContentValues();
154 args.put(KEY_TITOLO, titolo);
155 args.put(KEY_CASAEDITRICE, casaEditrice);
156 args.put(KEY_AUTORE, autore);
157 args.put(KEY_COPIEDISPONIBILI, copieDisponibili);
158 args.put(KEY_COPIETOTALI, copieTotali);
159 return db.update(DATABASE_TABELLA, args, KEY_CODICE + "=" + codice, null)>0;
160
161 }
162
163
164}