无法绑定索引处的参数,因为索引超出范围

时间:2014-06-23 13:16:06

标签: android sqlite android-activity android-sqlite sqliteopenhelper

我的sqlitedatabase仍有问题。你知道我想在sqlite数据库中存储和检索数据。我有一个名为tour的表,有五列。我的数据库已经创建,我可以通过filemanager看到它,但问题是插入和读取方法都不起作用。我通过这个命令得到行的计数找到它 我有一个管理数据库的类:SQLopenHelper,有两个添加和读取方法

并在MainActivity中调用该方法

cursor.getCount(); 

它给了我0

我的SQLopenHelper类:

package com.google.site.sqlHelper;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class Sql_openHelper extends SQLiteOpenHelper {

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

    private static final String DATABASE_NAME="tours.db";
    private static final int DATABASE_VERSION=1;

    private static final String TABLE_TOURS="tours";
    private static final String COLUMN_ID="tourId";
    private static final String COLUMN_TITLE="title";
    private static final String COLUMN_DESC="description";
    private static final String COLUMN_PRICE="price";
    private static final String COLUMN_IMAGE="image";

    private static final String TABLE_CREATE="CREATE TABLE "+TABLE_TOURS + " ("+
            COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
            COLUMN_TITLE + " TEXT, " +
            COLUMN_DESC + " TEXT, "+
            COLUMN_IMAGE+" TEXT, " +
            COLUMN_PRICE + " TEXT " +
            " )";
    private static final String SQL_DELETE_ENTRIES=
            "DROP TABLE IF EXISTS " + TABLE_TOURS;
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(TABLE_CREATE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(SQL_DELETE_ENTRIES);
        db.close();

    }
    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        onUpgrade(db,oldVersion,newVersion);
    }
    public void InsertDb(tour tour)
    {
        ContentValues  values=new ContentValues();

        values.put(COLUMN_TITLE, tour.getTitle());
        values.put(COLUMN_DESC, tour.getDescription());
        values.put(COLUMN_PRICE, tour.getPrice());
        values.put(COLUMN_IMAGE, tour.getImage());

        SQLiteDatabase db=this.getWritableDatabase();
        db.insert(TABLE_TOURS, null, values);

        Log.d("InsertDb method", "InsertDb method operation has been done");



    }
    public tour Readdb(tour tour)
    {
        long count=0;
        SQLiteDatabase db=this.getReadableDatabase();
        String[] tableColumns = new String[] {
                COLUMN_ID,COLUMN_TITLE,COLUMN_DESC,COLUMN_PRICE,COLUMN_IMAGE
            };





        try{
        Cursor cursor =
                db.query(TABLE_TOURS, // a. table
                tableColumns, // b. column names
                COLUMN_TITLE+ " like '"+tour.getTitle()+"'", // c. selections
                new String[] { COLUMN_TITLE }, // d. selections args
                null, // e. group by
                null, // f. having
                null, // g. order by
                null); // h. limit
        cursor.moveToFirst();
        count=cursor.getCount();
        tour.setTitle(cursor.getString(cursor.getColumnIndex(COLUMN_TITLE)));
            } catch(Exception e){Log.d("Error: ",e.getMessage() );}


        Log.d("Read method", "Read method operation has been done "+String.valueOf(count));
        return tour;
    }

}

这是我的MainActivity类:

package com.google.site.android_developer_sqlite;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;

import com.google.site.sqlHelper.Sql_openHelper;
import com.google.site.sqlHelper.UIHelper;
import com.google.site.sqlHelper.tour;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Sql_openHelper helper=new Sql_openHelper(this);
        tour tour=new tour();
        tour.setTitle("Happy birthday");
        tour.setDescription("you are the best one that i love in this world");
        tour.setImage("you don't need image of u're self");
        tour.setPrice("25$");
        tour.setTours("no need");
        helper.InsertDb(tour);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    public void btnclick(View view)
    {
        Sql_openHelper helper=new Sql_openHelper(this);
        tour tour=new tour();
        tour=helper.Readdb(tour);
        UIHelper.displayText(this, R.id.textview, tour.getTitle() + " " + tour.getDescription());
    }

}

主要问题是cursor.getCount();返回0所以它意味着我的方法插入方法不起作用,但它不知道为什么它没有通过错误!!!

2 个答案:

答案 0 :(得分:3)

db.query(TABLE_TOURS, // a. table
         tableColumns, // b. column names
         COLUMN_TITLE+ " like '"+tour.getTitle()+"'", // c. selections
         new String[] { COLUMN_TITLE }, // d. selections args

选择参数仅在选择中实际具有参数时才起作用:

db.query(...
         COLUMN_TITLE+ " like ?", // c. selections
         new String[] { tour.getTitle() }, // d. selections args

答案 1 :(得分:2)

像这样重写btnclick

public void btnclick(View view)
{
    Sql_openHelper helper=new Sql_openHelper(this);
    tour tour=new tour();
    tour.setTitle("Happy");
    tour=helper.Readdb(tour);
    UIHelper.displayText(this, R.id.textview, tour.getTitle() + " " + tour.getDescription());
}