如何从sqlite中检索数据以在textview上显示

时间:2014-02-23 18:13:41

标签: android sql textview

我搜索了很多我发现与我的问题有关的问题,但我已经修好了,它仍然是错误我不知道为什么请帮我解决。

我想从getNotname方法中检索数据以在textview中显示,但是它出错我不知道为什么,我只是在DatabaseHandler类中复制getAllContacts方法,并在getNotename中调整了一点查询。

在getAllContacts中字符串selectQuery =“SELECT * FROM”+ TABLE_SONG_DETAIL;

在getNotename中字符串selectQuery =“SELECT”+ KEY_N_NAME +“FROM”+ TABLE_SONG_DETAIL +“WHERE KEY_ID ='2';”;

但是当我调用getNotename时,在mainactivity中,它出错,它没有显示任何内容。另一方面,当我调用getAllContacts而不是getNotename时,可以在textview上显示数据。我只是想知道为什么当我在getNotename中调整一点点查询时它会出错。

               **List<Contact> contacts = db.getNotename();**  // When I call getNotename method, It errors but when I change to call to getAllContacts, It can show data on textview.

这是我的代码

DatabaseHandler类

public class DatabaseHandler extends SQLiteOpenHelper {



    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "PianoNote";

    // Contacts table name
    private static final String TABLE_SONG_DETAIL = "songDetail";

    // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_S_NAME = "songname";
    private static final String KEY_N_NAME = "notename";
    private static final String KEY_TIME = "time";


    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_SONG_DETAIL + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_S_NAME + " TEXT," + KEY_N_NAME + " TEXT,"
                + KEY_TIME + " TEXT" + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_SONG_DETAIL);

        // Create tables again
        onCreate(db);
    }



    // Adding new data
    void addContact(Contact contact) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues(); //import android.content.ContentValues
        values.put(KEY_S_NAME, contact.getsongName()); // Song Name
        values.put(KEY_N_NAME, contact.getNotename()); //  Note name
        values.put(KEY_TIME, contact.getTime()); // Time


        // Inserting Row
        db.insert(TABLE_SONG_DETAIL, null, values);
        db.close(); // Closing database connection
    }



    // Getting All Data in Table
    **public List<Contact> getAllContacts() {**
        List<Contact> contactList = new ArrayList<Contact>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_SONG_DETAIL;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Contact contact = new Contact();
                contact.setID(Integer.parseInt(cursor.getString(0)));
                contact.setsongName(cursor.getString(1));
                contact.setNotename(cursor.getString(2));
                contact.setTime(cursor.getString(3));
                // Adding contact to list
                contactList.add(contact);
            } while (cursor.moveToNext());
        }

        // return contact list
        return contactList;
    }



    // Getting  Notename
    **public List<Contact> getNotename() {**
        List<Contact> contactList = new ArrayList<Contact>();  
        // Select  Query

        String selectQuery = "SELECT  "+ KEY_N_NAME +" FROM " + TABLE_SONG_DETAIL+ "  WHERE KEY_ID = '2';";


        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Contact contact = new Contact();

                contact.setID(Integer.parseInt(cursor.getString(0)));
                contact.setsongName(cursor.getString(1));
                contact.setNotename(cursor.getString(2));
                contact.setTime(cursor.getString(3));

                // Adding contact to list
                contactList.add(contact);
            } while (cursor.moveToNext());
        }

        // return contact list
        return contactList;
    }
    }

MainActivity类

public class MainActivity extends Activity {

    SQLiteDatabase mDb;
    Cursor mCursor;


    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            TextView note = (TextView)findViewById(R.id.textView1);
            DatabaseHandler db = new DatabaseHandler(this);


            // Inserting Data
            Log.d("Insert: ", "Inserting .."); 


            db.addContact(new Contact("Twinkle", "Do", "0.1"));   
            db.addContact(new Contact("Twinkle", "Do", "0.2"));    
            db.addContact(new Contact("Twinkle", "Sol", "0.3"));    
            db.addContact(new Contact("Twinkle", "Sol", "0.4"));    
            db.addContact(new Contact("Twinkle", "La", "0.5")); 



            // Reading all contacts
            Log.d("Reading: ", "Reading all contacts.."); 


            **List<Contact> contacts = db.getNotename();** 

                    StringBuilder builder = new StringBuilder();
                for (Contact cn : contacts) {
                    builder.append(cn.getNotename());


                    // Writing Contacts to log
                    String log = "Id: "+cn.getID()+" ,SongName: " + cn.getsongName() + " ,NoteName: " + cn.getNotename()+ " ,Time: " + cn.getTime();
                    Log.d("Name: ", log);
                }

                // print show on textview
                note.setText(builder.toString());


        }


        }

联系班级

package com.example.databasehandler.java;

public class Contact {



        //private variables
        int _id;
        String _songname;
        String _note_name;
        String _time;

        // Empty constructor
        public Contact(){

        }
        // constructor
        public Contact(int id, String songname, String notename, String time){
            this._id = id;
            this._songname = songname;
            this._note_name = notename;
            this._time = time;

        }

        // constructor
        public Contact(String songname, String notename, String time){
            this._songname = songname;
            this._note_name = notename;
            this._time = time;

        }
        // getting ID
        public int getID(){
            return this._id;
        }

        // setting id
        public void setID(int id){
            this._id = id;
        }

        // getting name
        public String getsongName(){
            return this._songname;
        }

        // setting name
        public void setsongName(String songname){
            this._songname = songname;
        }

        // getting phone number
        public String getNotename(){
            return this._note_name;
        }

        // setting phone number
        public void setNotename(String notename){
            this._note_name = notename;
        }

     // getting phone number
        public String getTime(){
            return this._time;
        }

        // setting phone number
        public void setTime(String time){
            this._time = time;
        }
    }

1 个答案:

答案 0 :(得分:0)

我认为您的问题是您在SQL select语句的末尾添加了一个半冒号。

如上所述: http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#rawQuery(java.lang.String,java.lang.String [],android.os.CancellationSignal)

SQL字符串不能是“;”终止。