· 5 years ago · Jun 17, 2020, 12:32 PM
1// CHECK CONSTRAINTS: https://www.tutorialspoint.com/sqlite/sqlite_constraints.htm
2
3public class facturasSQLiteHelper extends SQLiteOpenHelper {
4
5 private Context context;
6 private Cursor cursor;
7 private SQLiteDatabase db;
8 private String[] campos = new String[] {"IDFACTURA", "FECHA", "DESCRIPCION", "IDCLIENTE", "IMPORTE", "TIPO", "ANULADA"};
9
10 // Sentencia SQL para crear la tabla de Facturas
11 private static final String sqlCreate = "CREATE TABLE factura (IDFACTURA integer primary key autoincrement, " +
12 "FECHA integer not null, DESCRIPCION VARCHAR(127), " +
13 "IDCLIENTE integer not null, IMPORTE real not null, " +
14 "TIPO text not null, ANULADA integer not null)";
15
16 // Nombre de la BBDD
17 private static final String nombreBD = "Facturacion";
18
19 public facturasSQLiteHelper(Context contexto, SQLiteDatabase.CursorFactory factory, int version) {
20 super(contexto, nombreBD, factory, version);
21 this.context = contexto;
22 }
23
24 @Override
25 public void onCreate(SQLiteDatabase db) {
26 //Se ejecuta la sentencia SQL de creación de la tabla
27 db.execSQL(sqlCreate);
28 }
29
30 @Override
31 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
32 //Se elimina la versión anterior de la tabla
33 db.execSQL("DROP TABLE IF EXISTS factura");
34
35 //Se ejecuta la sentencia SQL de creación de la tabla
36 db.execSQL(sqlCreate);
37 }
38
39 public void aplicarDescuentoFactura(int id, double descuento){
40 db = getWritableDatabase(); // Es necesario que ponga esto?
41 db.execSQL("UPDATE factura SET IMPORTE=IMPORTE-" + descuento/100 + "*IMPORTE WHERE IDFACTURA=" + id);
42 }
43
44 public void borrarFactura(int id, boolean anular){
45 int anular_num = (anular)?1:0;
46 db = getWritableDatabase(); // Es necesario que ponga esto?
47
48 if(anular){
49 // Marcar como anulada
50 db.execSQL("UPDATE factura SET ANULADA=" + anular_num + "WHERE IDFACTURA=" + id);
51 }
52 else{
53 // Borrar
54 db.delete("factura", "IDFACTURA=" + id, null);
55 }
56 }
57
58 public List<String> consultarFacturas(int fecha_ini, int fecha_fin){
59
60 List<String> lista_ids = new ArrayList<String>();
61 db = getWritableDatabase();
62
63 String[] args = new String[] {Integer.toString(fecha_ini), Integer.toString(fecha_fin)};
64 cursor = db.rawQuery("SELECT * from factura where FECHA BETWEEN ? AND ? ORDER BY IDFACTURA DESC", args);
65
66 if (cursor.moveToFirst()) {
67 do {
68 lista_ids.add(Integer.toString(cursor.getInt(0)));
69 } while(cursor.moveToNext());
70 }
71 return lista_ids;
72 }
73
74 public String obtenerInfoFactura(){
75 db = getWritableDatabase();
76 cursor = db.query("factura", campos, "", null, null, null, null);
77 String aa = "";
78 if (cursor.moveToFirst()) {
79 do {
80 aa += cursor.getString(2);
81 aa += " ";
82 } while(cursor.moveToNext());
83 }
84 return aa;
85 }
86
87 public void insertarFactura(int fecha, String desc, int idcliente, double importe, String tipo, int anulada){
88
89 db = getWritableDatabase();
90 String sql = "INSERT INTO factura (FECHA,DESCRIPCION,IDCLIENTE,IMPORTE,TIPO,ANULADA) " +
91 "VALUES (" + fecha+", '" + desc + "'," + idcliente + ", " + importe + ", '" + tipo + "'," + anulada+ ")";
92
93 db.execSQL(sql);
94 }
95}
96
97
98//---------------------------------------------------------------------------------------------------------------------------------------
99// MAIN
100
101public class MainActivity extends AppCompatActivity {
102
103 @Override
104 protected void onCreate(Bundle savedInstanceState) {
105 super.onCreate(savedInstanceState);
106 setContentView(R.layout.activity_main);
107
108 facturasSQLiteHelper usdbh = new facturasSQLiteHelper(this, null, 1);
109 SQLiteDatabase bd = usdbh.getWritableDatabase();
110
111 usdbh.insertarFactura( 20040915, "lola", 2, 3.33, "cargo", 0);
112
113 usdbh.insertarFactura( 20050505, "lolo", 2, 3.33, "cargo", 0);
114
115 usdbh.borrarFactura(1, false);
116
117 String info = usdbh.obtenerInfoFactura();
118
119 Toast.makeText(this, "BBDD: " + info, Toast.LENGTH_LONG).show();
120
121 usdbh.aplicarDescuentoFactura(1, 50);
122
123 List<String> aa = usdbh.consultarFacturas(20000101, 20300303);
124
125 for(int i = 0; i < aa.size(); i++){
126 Toast.makeText(this, "lol: " + aa.get(i), Toast.LENGTH_LONG).show();
127 }
128 }
129}
130
131// License CC