繁体   English   中英

如何获取java db sql以具有“按姓氏排序”?

[英]How do I get the java db sql to have a 'Order by lastname'?

这是来源-尝试添加+“按姓氏排序”时出现错误,有什么想法吗? (绝对是新手)//在尝试向下大约55行之前,我已经//。 我正在尝试将结果按姓氏排序

package com.tritech.colony;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class SearchPersonByColonyActivity extends Activity implements    OnItemClickListener
{
    private EditText searchPersonByColonyEditText ;
    private SQLiteDatabase db ;
    private Cursor cursor ;
    private ListAdapter adapter ;
    private ListView personByColonyListView ;
    private DatabaseHelper databaseHelper ;
    private String colonyName ;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.searchpersonbycolony);
        this.colonyName =     this.getIntent().getStringExtra(DatabaseHelper.KEY_COLONY_NAME);
        this.databaseHelper = new DatabaseHelper(this);
        this.databaseHelper.createDatabase();
        try {
            this.databaseHelper.openDatabase();
        } catch( SQLException ex ) {
            Log.e( DatabaseHelper.TAG, "Database can not be opened", ex);
        }
        this.databaseHelper.close();
        this.db = this.databaseHelper.getReadableDatabase() ;
        this.searchPersonByColonyEditText =         (EditText)this.findViewById(R.id.searchPersonByColonyEditText);
        this.personByColonyListView = (ListView)this.findViewById(R.id.searchPersonByColonyList);
        this.personByColonyListView.setOnItemClickListener(this);
        this.cursor = this.db.rawQuery("SELECT _id as _id , " + 
                DatabaseHelper.KEY_LAST_NAME + " , " + 
                DatabaseHelper.KEY_FIRST_NAME + " , " +
                "\"" + DatabaseHelper.KEY_SR_JR + "\" " + 
                "FROM " + DatabaseHelper.TABLE_NAME + 
                " WHERE " + DatabaseHelper.KEY_COLONY_NAME + " LIKE ?", 
                new String[] { this.colonyName } );
// tried these for line above and they fails
// new String[] { this.colonyName } + " ORDER BY " + DatabaseHelper.KEY_LAST_NAME );
// new String[] { this.colonyName } + " ORDER BY lastname" );

        this.startManagingCursor(this.cursor);
        this.adapter = new SimpleCursorAdapter(this, 
                R.layout.searchpersonbycolonyrow , 
                this.cursor , 
                new String[] { DatabaseHelper.KEY_LAST_NAME ,     DatabaseHelper.KEY_FIRST_NAME , DatabaseHelper.KEY_SR_JR }, 
                new int[] { R.id.searchPersonByColonyLastnameTextView ,         R.id.searchPersonByColonyFirstnameTextView , R.id.searchPersonByColonySrJrTextView } );
        this.personByColonyListView.setAdapter(this.adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        boolean back_tag = this.getSharedPreferences(MainActivity.TAG, MODE_PRIVATE).getBoolean(MainActivity.BACK_TAG, false);
        if( back_tag ) {
            this.finish();
        }
    }

    public void searchPerson( View v ) {
        String text = this.searchPersonByColonyEditText.getText().toString();
        if( !text.equals("") ) {
            this.cursor = this.db.rawQuery("SELECT _id as _id , " + 
                DatabaseHelper.KEY_LAST_NAME + " , " + 
                    DatabaseHelper.KEY_FIRST_NAME + " , " +
                    "\"" + DatabaseHelper.KEY_SR_JR + "\" " + 
                    "FROM " + DatabaseHelper.TABLE_NAME + 
                    " WHERE " + DatabaseHelper.KEY_COLONY_NAME + " LIKE ?" + 
                    " AND " + DatabaseHelper.KEY_LAST_NAME + " LIKE ?" , 
                    new String[] { this.colonyName , "%"+text+"%" } );
        } else {
            this.cursor = this.db.rawQuery("SELECT _id as _id , " + DatabaseHelper.KEY_LAST_NAME + " , " + DatabaseHelper.KEY_FIRST_NAME + " FROM " +     DatabaseHelper.TABLE_NAME + " WHERE " + DatabaseHelper.KEY_COLONY_NAME + " LIKE ?", new String[] { this.colonyName } );
            this.cursor = this.db.rawQuery("SELECT _id as _id , " + 
                    DatabaseHelper.KEY_LAST_NAME + " , " + 
                    DatabaseHelper.KEY_FIRST_NAME + " , " +
                    "\"" + DatabaseHelper.KEY_SR_JR + "\" " + 
                    "FROM " + DatabaseHelper.TABLE_NAME + 
                    " WHERE " + DatabaseHelper.KEY_COLONY_NAME + " LIKE ?", 
                    new String[] { this.colonyName } );
        }
        this.startManagingCursor(this.cursor);
        this.adapter = new SimpleCursorAdapter(this, 
                R.layout.searchpersonbycolonyrow , 
                this.cursor , 
                new String[] { DatabaseHelper.KEY_LAST_NAME ,     DatabaseHelper.KEY_FIRST_NAME , DatabaseHelper.KEY_SR_JR }, 
                new int[] { R.id.searchPersonByColonyLastnameTextView ,     R.id.searchPersonByColonyFirstnameTextView , R.id.searchPersonByColonySrJrTextView } );
        this.personByColonyListView.setAdapter(this.adapter);
    }

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent( this , PersonDetailsActivity.class );
        Cursor cursor = (Cursor)this.adapter.getItem(position);
        intent.putExtra(DatabaseHelper.KEY_ID, cursor.getInt(     cursor.getColumnIndex(DatabaseHelper.KEY_ID) ) );
        this.startActivity(intent);
    }

    protected void onDestroy() {
        this.cursor.close();
        this.databaseHelper.close();
    super.onDestroy();
    }
}

rawQuery()看起来需要两个参数:sql语句和一个字符串数组,它将替换sql语句中的?。 如果是正确的话,我想您想要的是:

        this.cursor = this.db.rawQuery("SELECT _id as _id , " + 
            DatabaseHelper.KEY_LAST_NAME + " , " + 
            DatabaseHelper.KEY_FIRST_NAME + " , " +
            "\"" + DatabaseHelper.KEY_SR_JR + "\" " + 
            "FROM " + DatabaseHelper.TABLE_NAME + 
            " WHERE " + DatabaseHelper.KEY_COLONY_NAME + " LIKE ? ORDER BY " 
            + DatabaseHelper.KEY_LAST_NAME, 
            new String[] { this.colonyName } );

我认为您在Order By之前缺少WHERE子句。

例如:

         SELECT * FROM items
         WHERE item LIKE 'B%'
         ORDER BY cost;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM