· 8 years ago · Aug 20, 2018, 04:52 PM
1_ID does not exist
2public class DataBaseHelper extends SQLiteOpenHelper{
3
4 private static String DB_PATH = "/data/data/com.rbrlnx.lugares/databases/";
5 private static final String DATABASE_NAME="db.db";
6 SQLiteDatabase db;
7
8 String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS lugares (" +
9 " _id INTEGER PRIMARY KEY AUTOINCREMENT, " +
10 "nombre text," +
11 "descripcion text,"+
12 "latitud real," +
13 "longitud real," +
14 "foto String);";
15
16 /*Primero se crea constructor, funcion onCreate, onUpgrade,Abrir y Cerrar*/
17
18 public DataBaseHelper(Context context){
19 super(context,DATABASE_NAME,null,1);
20
21 }
22
23 public void onCreate(SQLiteDatabase db){
24 try {
25 openDataBase();
26 db.execSQL(CREATE_TABLE);
27
28 } catch (Exception e) {
29 // handle exception
30 }
31 }
32
33public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
34
35}
36 public void openDataBase() throws SQLException{
37
38 //Open the database
39 String myPath = DB_PATH + DATABASE_NAME;
40 db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
41
42 }
43
44 public void close(){
45 db.close();
46 }
47
48
49/*Despues metodos para añadir y obtener datos*/
50
51 public long addNombre(String nombre){
52
53 ContentValues cv = new ContentValues();
54 cv.put("nombre", nombre);
55return db.insert("lugares", null, cv);
56}
57
58 public long addDescripcion(String descripcion){
59
60 ContentValues cv = new ContentValues();
61 cv.put("descripcion", descripcion);
62 return db.insert("lugares", null, cv);
63
64 }
65 public long addLatitud(double latitud){
66
67 ContentValues cv = new ContentValues();
68 cv.put("latitud", latitud);
69 return db.insert("lugares", null, cv);
70
71 }
72 public long addLongitud(double longitud){
73
74 ContentValues cv = new ContentValues();
75 cv.put("longitud", longitud);
76 return db.insert("lugares", null, cv);
77
78 }
79
80 public long addFoto(String foto) {
81
82 ContentValues cv = new ContentValues();
83 cv.put("foto", foto);
84 return db.insert("lugares", null, cv);
85
86 }
87
88
89 public Cursor getNombres(){
90 SQLiteDatabase db = this.getWritableDatabase();
91
92 Cursor respuesta = db.rawQuery("select nombre from lugares", null);
93 return respuesta;
94
95 }
96
97
98
99
100
101
102
103 }
104
105public class listatab extends ListActivity{
106
107 Context context;
108 ListView listanombres;
109 DataBaseHelper ayudabbdd;
110
111
112 public void onCreate(Bundle savedInstanceState) {
113 super.onCreate(savedInstanceState);
114 DataBaseHelper ayudabbdd = new DataBaseHelper(this);
115 Cursor nombresC;
116 nombresC = (Cursor) ayudabbdd.getNombres();
117 startManagingCursor(nombresC);
118 if(nombresC!=null){
119 ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.listatab, nombresC, new String[] { "nombre" }, new int[] { R.id.lista });
120 this.setListAdapter(adapter);
121 this.getListView().setTextFilterEnabled(true);
122
123 }
124 }
125 @Override
126protected void onDestroy() {
127 super.onDestroy();
128 if (ayudabbdd != null) {
129 ayudabbdd.close();
130 }
131 }
132 }
133
13410-11 00:55:49.930: ERROR/AndroidRuntime(32392): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rbrlnx.lugares/com.rbrlnx.lugares.listatab}: java.lang.IllegalArgumentException: column '_id' does not exist
13510-11 00:55:49.930: ERROR/AndroidRuntime(32392): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
13610-11 00:55:49.930: ERROR/AndroidRuntime(32392): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1487)
13710-11 00:55:49.930: ERROR/AndroidRuntime(32392): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
13810-11 00:55:49.930: ERROR/AndroidRuntime(32392): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
13910-11 00:55:49.930: ERROR/AndroidRuntime(32392): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
14010-11 00:55:49.930: ERROR/AndroidRuntime(32392): at android.widget.TabHost.setCurrentTab(TabHost.java:326)
14110-11 00:55:49.930: ERROR/AndroidRuntime(32392): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:132)
14210-11 00:55:49.930: ERROR/AndroidRuntime(32392): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:456)
14310-11 00:55:49.930: ERROR/AndroidRuntime(32392): at android.view.View.performClick(View.java:2485)
14410-11 00:55:49.930: ERROR/AndroidRuntime(32392): at android.view.View$PerformClick.run(View.java:9080)
14510-11 00:55:49.930: ERROR/AndroidRuntime(32392): at android.os.Handler.handleCallback(Handler.java:587)
14610-11 00:55:49.930: ERROR/AndroidRuntime(32392): at android.os.Handler.dispatchMessage(Handler.java:92)
14710-11 00:55:49.930: ERROR/AndroidRuntime(32392): at android.os.Looper.loop(Looper.java:130)
14810-11 00:55:49.930: ERROR/AndroidRuntime(32392): at android.app.ActivityThread.main(ActivityThread.java:3683)
14910-11 00:55:49.930: ERROR/AndroidRuntime(32392): at java.lang.reflect.Method.invokeNative(Native Method)
15010-11 00:55:49.930: ERROR/AndroidRuntime(32392): at java.lang.reflect.Method.invoke(Method.java:507)
15110-11 00:55:49.930: ERROR/AndroidRuntime(32392): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
15210-11 00:55:49.930: ERROR/AndroidRuntime(32392): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
15310-11 00:55:49.930: ERROR/AndroidRuntime(32392): at dalvik.system.NativeStart.main(Native Method)
15410-11 00:55:49.930: ERROR/AndroidRuntime(32392): Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
155
156Context context;
157private static final String DATABASE_NAME="lugaresbbdd";
158private SQLiteDatabase db; // Referencia al manager.
159private final int DB_VERSION = 1; // version
160// Nombres para las tablas y campos
161private final String TABLE_NAME = "lugares";
162private final String TABLE_ROW_ID = "_id";
163private final String CNOMBRE = "nombre";
164private final String CDESC = "descripcion";
165private final String CLAT = "latitud";
166private final String CLONG="longitud";
167private final String CFOTO="foto";
168
169public DataBaseHelper(Context context)
170{
171 this.context = context;
172
173 //Crea o abre la BBDDD
174 CustomSQLiteOpenHelper helper = new CustomSQLiteOpenHelper(context);
175 this.db = helper.getWritableDatabase();
176}
177
178
179
180 /*Metodos para añadir datos a la BBDD*/
181/**********************************************************************
182 * Metodos para añadir nombres a la BBDDD
183 *
184 * @param nombre valor para nombre
185 */
186 public long addNombre(String nombre){
187
188 ContentValues cv = new ContentValues();
189 cv.put("nombre", nombre);
190 return db.insert("lugares", null, cv);
191 }
192
193 /**********************************************************************
194 * Metodos para añadir descrpcipciones a la BBDDD
195 *
196 * @param descripcion valor para descripcion
197 */
198 public long addDescripcion(String descripcion){
199
200 ContentValues cv = new ContentValues();
201 cv.put("descripcion", descripcion);
202 return db.insert("lugares", null, cv);
203
204 }
205 /**********************************************************************
206 * Metodos para añadir latitudes a la BBDDD
207 *
208 * @param latitud valor para campo latitud
209 */
210 public long addLatitud(double latitud){
211
212 ContentValues cv = new ContentValues();
213 cv.put("latitud", latitud);
214 return db.insert("lugares", null, cv);
215
216 }
217 /**********************************************************************
218 * Metodos para añadir longitud a la BBDDD
219 *
220 * @param longitud valor para campo longitud
221 */
222 public long addLongitud(double longitud){
223
224 ContentValues cv = new ContentValues();
225 cv.put("longitud", longitud);
226 return db.insert("lugares", null, cv);
227
228 }
229 /**********************************************************************
230 * Metodos para añadir foto a la BBDDD
231 *
232 * @param foto valor para patch de la foto
233 */
234 public long addFoto(String foto) {
235
236 ContentValues cv = new ContentValues();
237 cv.put("foto", foto);
238 return db.insert("lugares", null, cv);
239
240 }
241
242
243/**********************************************************************
244 * * Obten todos los nombres
245 *
246 */
247
248public Cursor getNombres(){
249
250 Cursor respuesta = db.rawQuery("select "+TABLE_ROW_ID+","+CNOMBRE+" from "+TABLE_NAME, null);
251 return respuesta;
252}
253
254
255/**********************************************************************
256 * Borra una fila de la BBDD
257 *
258 * @param rowID ID de la BBDD que quiero borrar
259 */
260
261public void deleteRow(long rowID)
262{
263 try {db.delete(TABLE_NAME, TABLE_ROW_ID + "=" + rowID, null);}
264 catch (Exception e)
265 {
266 Log.e("DB ERROR", e.toString());
267 e.printStackTrace();
268 }
269}
270
271/**********************************************************************
272 * ACTUALIZANDO UN LUGAR DE LA BBDD
273 * @param rowID EL ID DEL LUGAR QUE QUIERO MODIFICAR
274 * @param rowNombre nombre nuevo
275 * @param rowDesc descrpcion nuega
276 * @param rowLat latitud nueva
277 * @param rowLong longitud nueva
278 * @param rowFoto foto nueva
279 */
280public void updateRow(long rowID, String rowNombre, String rowDesc, Long rowLat, Long rowLong, String rowFoto)
281{
282 // this is a key value pair holder used by android's SQLite functions
283 ContentValues values = new ContentValues();
284 values.put(CNOMBRE, rowNombre);
285 values.put(CDESC, rowDesc);
286 values.put(CLAT, rowLat);
287 values.put(CLONG,rowLong);
288 values.put(CFOTO,rowFoto);
289
290 // ask the database object to update the database row of given rowID
291 try {db.update(TABLE_NAME, values, TABLE_ROW_ID + "=" + rowID, null);}
292 catch (Exception e)
293 {
294 Log.e("DB Error", e.toString());
295 e.printStackTrace();
296 }
297}
298/**
299Clase que comprueba si la tabla existe,
300Si no existe, se crea
301Si existe, se actualiza
302Metodo que la cierra al finalizar su uso
303 */
304
305private class CustomSQLiteOpenHelper extends SQLiteOpenHelper
306{
307 public CustomSQLiteOpenHelper(Context context)
308 {
309 super(context, DATABASE_NAME, null, DB_VERSION);
310 }
311
312 public void onCreate(SQLiteDatabase db)
313 {
314
315 String CREA_TABLA =
316 "CREATE TABLE " +
317 TABLE_NAME +
318 "("+
319 TABLE_ROW_ID + " integer primary key autoincrement not null," +
320 CNOMBRE + " TEXT," +
321 CDESC + " TEXT," +
322 CLAT + " REAL," +
323 CLONG + " REAL," +
324 CFOTO + " STRING" +
325 ");";
326
327 db.execSQL(CREA_TABLA);
328 }
329
330
331
332 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
333 {
334 //NO HAGAS NADA
335 }
336
337
338 public void close(){
339 db.close();
340 }
341
342
343
344}
345
346public class listatab extends ListActivity{
347
348 Context context;
349 ListView listanombres;
350 DataBaseHelper ayudabbdd;
351
352
353 public void onCreate(Bundle savedInstanceState) {
354 super.onCreate(savedInstanceState);
355 ayudabbdd = new DataBaseHelper(this);
356 Cursor nombresC;
357 nombresC = (Cursor) ayudabbdd.getNombres();
358 startManagingCursor(nombresC);
359
360 if(nombresC!=null){
361 ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.listatab, nombresC, new String[] { "nombre" }, new int[] { R.id.lista });
362 this.setListAdapter(adapter);
363 this.getListView().setTextFilterEnabled(true);
364
365 }
366 }