Note that there are some explanatory texts on larger screens.

plurals
  1. POSQlite Query Problems, causing Force Close
    primarykey
    data
    text
    <p>I am writing my first application, and I am also trying to incorporate a database, and I am really struggling with it. What I am trying to do is have multiple spinners, and those spinners get their data from a database. Each spinner will do a different query.</p> <p>So far this is what I have: DBUtility</p> <pre><code>import android.content.Context; </code></pre> <p>import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.widget.SimpleCursorAdapter;</p> <p>public class DbUtility {</p> <pre><code>static final String DB_NAME="food"; static final String BEEF_TABLE="beef"; static final String CHICKEN_TABLE="chicken"; SQLiteDatabase db=null; Context context; public static class DatabaseHelper extends SQLiteOpenHelper { public DatabaseHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE IF NOT EXISTS "+BEEF_TABLE+" (_id INT PRIMARY KEY, cookTime INT PRIMARY KEY ,name VARCHAR);"); db.execSQL("INSERT INTO "+BEEF_TABLE+" values(5000,'Skirt Steak');"); db.execSQL("INSERT INTO "+BEEF_TABLE+" values(10000,'Flank Steak');"); db.execSQL("INSERT INTO "+BEEF_TABLE+" values(15000,'Filet Mignon');"); db.execSQL("CREATE TABLE IF NOT EXISTS "+CHICKEN_TABLE+" (_id INT PRIMARY KEY, cookTime INT PRIMARY KEY ,name VARCHAR);"); db.execSQL("INSERT INTO "+CHICKEN_TABLE+" values(5000,'Breast');"); db.execSQL("INSERT INTO "+CHICKEN_TABLE+" values(10000,'Wings');"); db.execSQL("INSERT INTO "+CHICKEN_TABLE+" values(15000,'Burger');"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } } public DbUtility(Context context) { this.context=context; } public SimpleCursorAdapter getBeefAdapter() { DatabaseHelper dbhelper=new DatabaseHelper(this.context, DB_NAME, null, 1); db=dbhelper.getWritableDatabase(); Cursor beefCursor=db.rawQuery("SELECT * FROM "+BEEF_TABLE, null); while(!beefCursor.isLast()) beefCursor.moveToNext(); //I don't understand why but it's necessary (alternative call c.getCount() ) String[] from=new String[1]; from[0]="name"; int[] to=new int[1]; to[0]=android.R.id.text1; SimpleCursorAdapter beefAdapter=new SimpleCursorAdapter(this.context, android.R.layout.simple_spinner_item, beefCursor, from, to); beefAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); db.close(); return beefAdapter; } public SimpleCursorAdapter getChickenAdapter() { DatabaseHelper dbhelper=new DatabaseHelper(this.context, DB_NAME, null, 1); db=dbhelper.getWritableDatabase(); Cursor chickenCursor=db.rawQuery("SELECT * FROM "+CHICKEN_TABLE, null);//If I change this to BEEF_TABLE it doesn't force close... while(!chickenCursor.isLast()) chickenCursor.moveToNext(); //I don't understand why but it's necessary (alternative call c.getCount() ) String[] from=new String[1]; from[0]="name"; int[] to=new int[1]; to[0]=android.R.id.text1; SimpleCursorAdapter chickenAdapter=new SimpleCursorAdapter(this.context, android.R.layout.simple_spinner_item, chickenCursor, from, to); chickenAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); db.close(); return chickenAdapter; } </code></pre> <p>}</p> <p>and I also have my chickenActivity, this is where my spinners are:</p> <p>package com.tsilo.grillbuddy;</p> <p>import android.app.Activity; import android.os.Bundle; import android.widget.SimpleCursorAdapter; import android.widget.Spinner;</p> <p>public class ChickenActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.spinner);</p> <pre><code> DbUtility db=new DbUtility(this); SimpleCursorAdapter beefAdapter=db.getBeefAdapter(); Spinner beefSpinner=(Spinner)this.findViewById(R.id.spinner); beefSpinner.setAdapter(beefAdapter); SimpleCursorAdapter chickenAdapter=db.getChickenAdapter(); Spinner chickenSpinner=(Spinner)this.findViewById(R.id.spinner2); chickenSpinner.setAdapter(chickenAdapter); } </code></pre> <p>}</p> <p>This is what I have noticed:<br> If I change my getChickenAdapter() so it queries BEEF_TABLE, it works, it is only when I switch it to CHICKEN_TABLE, that it force closes.</p> <hr> <p>My DDMS Error: </p> <p>12-06 16:03:12.473: INFO/Database(11856): sqlite returned: error code = 1, msg = no such table: chicken 12-06 16:03:12.473: DEBUG/AndroidRuntime(11856): Shutting down VM 12-06 16:03:12.473: WARN/dalvikvm(11856): threadid=1: thread exiting with uncaught exception (group=0x4001d7f0) 12-06 16:03:12.483: ERROR/AndroidRuntime(11856): FATAL EXCEPTION: main 12-06 16:03:12.483: ERROR/AndroidRuntime(11856): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tsilo.grillbuddy/com.tsilo.grillbuddy.ChickenActivity}: android.database.sqlite.SQLiteException: no such table: chicken: , while compiling: SELECT * FROM chicken</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload