使用查询时出错

时间:2015-06-15 17:58:04

标签: android sqlite

我有一个改变DB值字段的按钮。我希望在ListView显示所有具有一定价值的字段(顶部,在我的例子中)。我知道,我做错了什么,但不知道什么。 MainActivity:

public class MainActivity extends ActionBarActivity implements View.OnClickListener {
    Button btnadd;
    LinearLayout llMain;
Time today = new Time(Time.getCurrentTimezone());
DBAdapter myDb;
ImageButton imgStar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {



        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
        btnadd = (Button) findViewById(R.id.button);
        //llMain = (LinearLayout) findViewById(R.id.llMain);
        btnadd.setOnClickListener(this);
        openDB();
        populateListView();
        imgStar = (ImageButton)findViewById(R.id.imageButton2);
        imgStar.setOnClickListener(this);
    }


        public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.button:
                Intent intent = new Intent(this, result.class);
                startActivityForResult(intent, 1);
                break;
            case R.id.imageButton2:
                ifTop();
                break;
            }
        }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {


        String name = data.getStringExtra("task");
        String grup = data.getStringExtra("grup");
today.setToNow();
    String timestamp  = today.format("%Y-%m-%d %H:%M:%S");
myDb.insertRow(name,timestamp,grup);
populateListView();





            }

private void openDB(){
    myDb = new DBAdapter(this);
myDb.open();
}
private void populateListView()    {
        Cursor cursor = myDb.getAllRows();
        String[] fromFieldNames = new String[]{DBAdapter.KEY_ROWID,DBAdapter.KEY_TASK};
        int[] toViewIDs = new int[]{R.id.textView3,R.id.textView6};
        SimpleCursorAdapter myCursorAdapter;
        myCursorAdapter = new SimpleCursorAdapter(getBaseContext(),R.layout.item_layout,cursor,fromFieldNames,toViewIDs,0);
        ListView myList = (ListView) findViewById(R.id.listView);

        myList.setAdapter(myCursorAdapter);
    }
private void ifTop(){
    Cursor c = myDb.ifTopViews();
    String[] fromFieldNames = new String[]{DBAdapter.KEY_ROWID,DBAdapter.KEY_TASK};
    int[] toViewIDs = new int[]{R.id.textView3,R.id.textView6};
    SimpleCursorAdapter myCursorAdapter;
    myCursorAdapter = new SimpleCursorAdapter(getBaseContext(),R.layout.item_layout,c,fromFieldNames,toViewIDs,0);
    ListView myList = (ListView) findViewById(R.id.listView);
    myList.setAdapter(myCursorAdapter);
}
    }

结果:

public class result extends ActionBarActivity implements View.OnClickListener {
    EditText etName;
    Button btnOk;
    EditText etData;
    boolean bIcon = true;
ImageButton star;
    String grup;
    Intent intent = new Intent();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
        etName = (EditText) findViewById(R.id.editText);
        btnOk = (Button) findViewById(R.id.button2);
star = (ImageButton) findViewById(R.id.imageButton);
        btnOk.setOnClickListener(this);
        star.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_result, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button2:

            intent.putExtra("task", etName.getText().toString());

            setResult(RESULT_OK, intent);
            finish();
                break;
            case R.id.imageButton:
                if (bIcon) {
                    star.setImageResource(R.drawable.star1);
                    grup = "top";

                }
                else

                {
                    star.setImageResource(R.drawable.star);
                    grup = null;
                }
                intent.putExtra("grup",grup);
                bIcon = !bIcon;
                break;
        }
    }
}

DBAdapter(并非所有,只是不起作用的东西):

public Cursor ifTopViews() {
        String where = KEY_GRUPS+"="+ "top";
        Cursor c =  db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null, null, null, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }

错误:

06-15 20:47:26.487      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:47:26.497    7684-7684/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-15 20:47:32.714      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:47:33.094    7726-7726/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-15 20:47:35.477      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:47:35.998      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 9 to 15
06-15 20:47:38.750  19244-19294/? E/ExternalAccountType﹕ Unsupported attribute readOnly
06-15 20:47:39.111    7808-7808/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-15 20:47:39.361  19244-19294/? E/ExternalAccountType﹕ Unsupported attribute readOnly
06-15 20:47:39.631    7824-7824/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-15 20:47:39.772    7838-7838/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-15 20:47:39.882    1231-1231/? E/NetworkScheduler.SchedulerReceiver﹕ Invalid parameter app
06-15 20:47:39.882    1231-1231/? E/NetworkScheduler.SchedulerReceiver﹕ Invalid package name : Perhaps you didn't include a PendingIntent in the extras?
06-15 20:47:40.122    7868-7868/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-15 20:47:40.172    7868-7868/? E/dalvikvm﹕ Could not find class 'android.app.Notification$Action$Builder', referenced from method b.a
06-15 20:47:40.192    7868-7868/? E/dalvikvm﹕ Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method b.a
06-15 20:47:40.282    7868-7868/? E/dalvikvm﹕ Could not find class 'android.app.Notification$Action$Builder', referenced from method b.a
06-15 20:47:40.302    7868-7868/? E/dalvikvm﹕ Could not find class 'android.transition.Transition', referenced from method b.a
06-15 20:47:40.302    7868-7868/? E/dalvikvm﹕ Could not find class 'android.transition.Transition', referenced from method b.a
06-15 20:47:40.312    7868-7868/? E/dalvikvm﹕ Could not find class 'android.transition.Transition', referenced from method b.a
06-15 20:47:40.342    7868-7868/? E/dalvikvm﹕ Could not find class 'android.app.RemoteInput[]', referenced from method b.a
06-15 20:47:40.352    7868-7868/? E/dalvikvm﹕ Could not find class 'android.transition.Transition', referenced from method b.b
06-15 20:47:40.392      631-660/? E/power﹕ Can not write CPU_PERF_LEVEL_LOCK. (Invalid argument)
06-15 20:47:40.542    7887-7887/com.example.user.campus E/Trace﹕ error opening trace file: No such file or directory (2)
06-15 20:47:40.663      631-993/? E/Surface﹕ Invalid SurfaceControl
06-15 20:47:40.903    7907-7907/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-15 20:47:41.634    7937-7937/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-15 20:47:41.704    7951-7951/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-15 20:47:41.794    7966-7966/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-15 20:47:42.004    7983-7983/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-15 20:47:42.184     904-1051/? E/Prism.WidgetManager﹕ The same lists. No need to update. skip it.
06-15 20:47:42.314    8009-8009/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-15 20:47:42.384      631-660/? E/power﹕ request perf lock level(0) is invalid. (Invalid argument)
06-15 20:47:42.494    8032-8032/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-15 20:47:42.575    1231-1231/? E/NetworkScheduler.SchedulerReceiver﹕ Invalid parameter app
06-15 20:47:42.575    1231-1231/? E/NetworkScheduler.SchedulerReceiver﹕ Invalid package name : Perhaps you didn't include a PendingIntent in the extras?
06-15 20:47:42.695    8048-8048/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-15 20:47:42.775    8066-8066/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-15 20:47:43.015    1231-1231/? E/AuthorizationBluetoothService﹕ Proximity feature is not enabled.
06-15 20:47:43.225    7887-7887/com.example.user.campus E/SQLiteLog﹕ (1) no such column: top
06-15 20:47:43.235    7887-7887/com.example.user.campus E/AndroidRuntime﹕ FATAL EXCEPTION: main
    android.database.sqlite.SQLiteException: no such column: top (code 1): , while compiling: SELECT DISTINCT _id, task, date, grup FROM mainToDo WHERE grup=top
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:909)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:520)
            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
            at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
            at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
            at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:65)
            at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1370)
            at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1217)
            at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1088)
            at com.example.user.campus.DBAdapter.ifTopViews(DBAdapter.java:101)
            at com.example.user.campus.MainActivity.ifTop(MainActivity.java:127)
            at com.example.user.campus.MainActivity.onClick(MainActivity.java:91)
            at android.view.View.performClick(View.java:4231)
            at android.view.View$PerformClick.run(View.java:17537)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:158)
            at android.app.ActivityThread.main(ActivityThread.java:5751)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
            at dalvik.system.NativeStart.main(Native Method)
06-15 20:47:43.245      631-659/? E/EmbeddedLogger﹕ App crashed! Process: com.example.user.campus
06-15 20:47:43.245      631-659/? E/EmbeddedLogger﹕ App crashed! Package: com.example.user.campus v1 (1.0)
06-15 20:47:43.245      631-659/? E/EmbeddedLogger﹕ Application Label: Campus
06-15 20:47:43.786     631-1441/? E/Surface﹕ Invalid SurfaceControl
06-15 20:47:43.916    8093-8093/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-15 20:47:44.006      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 15 to 3
06-15 20:47:44.537      631-702/? E/InputDispatcher﹕ channel '42eafd38 com.example.user.campus/com.example.user.campus.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
06-15 20:47:45.017      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 3 to 9
06-15 20:47:53.867      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:47:54.027      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 9 to 15
06-15 20:47:57.130      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:48:00.113      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:48:02.966      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:48:04.047      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 15 to 3
06-15 20:48:04.938      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:48:05.049      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 3 to 102
06-15 20:48:49.336      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:48:52.830      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:48:52.850    8256-8256/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-15 20:48:56.393      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:48:56.523      235-235/? E/AudioPolicyManagerBase﹕ getOutput() IOProfile is null.
06-15 20:48:56.523      235-235/? E/AudioPolicyManagerBase﹕ getOutput() IOProfile is null.
06-15 20:48:56.523      235-235/? E/AudioPolicyManagerBase﹕ getOutput() IOProfile is null.
06-15 20:48:56.523      235-235/? E/AudioPolicyManagerBase﹕ getOutput() IOProfile is null.
06-15 20:48:57.585      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:48:58.886     235-8300/? E/MP3Extractor﹕ Unable to resync. Signalling end of stream.
06-15 20:49:00.117      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:49:35.605      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:49:37.577      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:49:38.218      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 102 to 9
06-15 20:49:38.228      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:49:42.833      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:49:43.233      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 9 to 3
06-15 20:49:44.935      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:49:45.235      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 3 to 15
06-15 20:49:45.686      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:49:46.237      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:49:51.402      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:49:52.253      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 15 to 9
06-15 20:49:58.329      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:49:59.260      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 9 to 15
06-15 20:50:05.918      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:50:06.268      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 15 to 9
06-15 20:50:06.368      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:50:37.101    8611-8611/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-15 20:50:37.111      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:50:37.371    8640-8640/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-15 20:50:39.193      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:50:39.333      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 9 to 15
06-15 20:50:39.543      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:50:43.428      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:50:51.756      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:50:52.357      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 15 to 3
06-15 20:50:53.709      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:50:54.359      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 3 to 102
06-15 20:51:47.676      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:51:52.701      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:52:00.109      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:52:43.095      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:52:43.576      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 102 to 3
06-15 20:52:44.577      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 3 to 9
06-15 20:53:54.812      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:53:55.723      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 9 to 3
06-15 20:53:56.343      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:53:56.714      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 3 to 15
06-15 20:54:03.021      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:54:03.731      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 15 to 9
06-15 20:55:37.702      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 20:56:00.105      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000

1 个答案:

答案 0 :(得分:2)

在你的查询&#34; SELECT DISTINCT _id,task,date,grup FROM mainToDo WHERE grup = top&#34;在引用文本值(顶部)时,您没有使用引号。它应该在引号之间,否则SQL将不会将其识别为字符串值。

正确查询:SELECT DISTINCT _id,task,date,grup FROM mainToDo WHERE grup =&#39; top&#39;;

在结果类中使用:

if (bIcon) {
   star.setImageResource(R.drawable.star1);
   grup = "'top'"; 
}