从数据库中获取微调器列表

时间:2013-05-09 08:15:32

标签: android database sqlite

我是android初学者。我想从sqlite数据库中获取微调器列表。我看了很多次,但很难找出哪一个是不正确的。 这是代码......

public class SalesEntry extends Activity {

Spinner spin;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sales_entry);

    spin = (Spinner) findViewById(R.id.spinner1);

    loadDiscountData(); // load from database


    spin.setOnItemSelectedListener(new OnItemSelectedListener()
    { // when a spinner item is selected
        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

}

private void loadDiscountData() {
    try {
        // TODO Auto-generated method stub

        Data db = new Data(getApplicationContext());
        ArrayList<String>  lables= new ArrayList<String>();
        lables = db.getAllLabels();

        // Creating adapter for spinner
         ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
         R.layout.spinner_row, lables);

        spin.setAdapter(dataAdapter);

    } catch (Exception e) {
        String error = e.toString();// make a error message
        Dialog d = new Dialog(this);
        d.setTitle("Error!");
        TextView tv = new TextView(this);
        tv.setText(error);// writing a error message not using a text
        d.setContentView(tv);
        d.show();
    }
}
}

public class Data {

// Contacts Table Columns names
// public static final String KEY_ROWID = "_id";
public static final String KEY_DISCOUNT = "_discount";

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "DiscountData";
private static final String DATABASE_TABLE = "discountTable";

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

private static class DbHelper extends SQLiteOpenHelper {

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) { // create db table
        // TODO Auto-generated method stub
        String CREATE_DISCOUNT_TABLE = "CREATE TABLE " + DATABASE_TABLE
                + " ("
                // + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + KEY_DISCOUNT + " INTEGER" + ")";
        db.execSQL(CREATE_DISCOUNT_TABLE);
        // CREATE TABLE discountTable (_id INTEGER PRIMARY KEY _discount
        // TEXT);


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db); // upgrade database.

    }
}

public Data(Context c) {
    ourContext = c;
}

public Data open() {
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}

public void close() {
    ourHelper.close();
}

public long createEntry(String data) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_DISCOUNT, data);// open message which create date
    return ourDatabase.insert(DATABASE_TABLE, null, cv);
}

public void deletEntry(String mDelet) {
    // TODO Auto-generated method stub
    ourDatabase.delete(DATABASE_TABLE, KEY_DISCOUNT + "=" + mDelet, null);
}

public String getDiscountList() {
    // TODO Auto-generated method stub
    String[] columns = new String[] { KEY_DISCOUNT };
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
            null, null);
    String result = "";

    int iRow = c.getColumnIndex(KEY_DISCOUNT);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { // as long as
                                                                // last. it
                                                                // is move
                                                                // to next
        result = result + c.getString(iRow) + "% \n";
    }

    return result;
}

public ArrayList<String> getAllLabels() {
    // TODO Auto-generated method stub

    ArrayList < String > labels = new ArrayList < String > ();
    Cursor cursor = ourDatabase.rawQuery("SELECT * FROM " + KEY_DISCOUNT, null);
    System.out.println("COUNT : " + cursor.getCount());

    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
    { // as long as last.it is move to next
        labels.add(cursor.getString(0));
    }
    return labels;
}

}

log cat输出:

0

5-09 16:12:00.207: E/AndroidRuntime(17815): FATAL EXCEPTION: main
05-09 16:12:00.207: E/AndroidRuntime(17815): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.project/com.example.project.SalesEntry}: java.lang.NullPointerException
05-09 16:12:00.207: E/AndroidRuntime(17815):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1961)
05-09 16:12:00.207: E/AndroidRuntime(17815):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1986)
05-09 16:12:00.207: E/AndroidRuntime(17815):    at android.app.ActivityThread.access$600(ActivityThread.java:128)
05-09 16:12:00.207: E/AndroidRuntime(17815):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1152)
05-09 16:12:00.207: E/AndroidRuntime(17815):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-09 16:12:00.207: E/AndroidRuntime(17815):    at android.os.Looper.loop(Looper.java:137)
05-09 16:12:00.207: E/AndroidRuntime(17815):    at android.app.ActivityThread.main(ActivityThread.java:4449)
05-09 16:12:00.207: E/AndroidRuntime(17815):    at java.lang.reflect.Method.invokeNative(Native Method)
05-09 16:12:00.207: E/AndroidRuntime(17815):    at java.lang.reflect.Method.invoke(Method.java:511)
05-09 16:12:00.207: E/AndroidRuntime(17815):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
05-09 16:12:00.207: E/AndroidRuntime(17815):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
05-09 16:12:00.207: E/AndroidRuntime(17815):    at dalvik.system.NativeStart.main(Native Method)
05-09 16:12:00.207: E/AndroidRuntime(17815): Caused by: java.lang.NullPointerException
05-09 16:12:00.207: E/AndroidRuntime(17815):    at com.example.project.Data.getAllLabels(Data.java:105)
05-09 16:12:00.207: E/AndroidRuntime(17815):    at com.example.project.SalesEntry.loadDiscountData(SalesEntry.java:54)
05-09 16:12:00.207: E/AndroidRuntime(17815):    at com.example.project.SalesEntry.onCreate(SalesEntry.java:27)
05-09 16:12:00.207: E/AndroidRuntime(17815):    at android.app.Activity.performCreate(Activity.java:4465)
05-09 16:12:00.207: E/AndroidRuntime(17815):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-09 16:12:00.207: E/AndroidRuntime(17815):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1925)
05-09 16:12:00.207: E/AndroidRuntime(17815):    ... 11 more

1 个答案:

答案 0 :(得分:0)

我相信您的SQL语句不正确。您似乎是从列名中选择,而不是从表名中选择:

       SELECT * FROM " + KEY_DISCOUNT


      String CREATE_DISCOUNT_TABLE = "CREATE TABLE " + DATABASE_TABLE
            + " ("
            // + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + KEY_DISCOUNT + " INTEGER" + ")";


       private static final String DATABASE_TABLE = "discountTable";