android - 无法在索引处绑定(bind)参数,因为索引超出范围

标签 android sqlite android-activity android-sqlite sqliteopenhelper

我仍然对这个 sqlite 数据库有疑问。你知道我想在 sqlite 数据库中存储和检索数据。我有一个名为 tour 的表,有五个列。我的数据库已经创建,我可以通过文件管理器看到它,但问题是插入和读取方法都不起作用。我通过此命令获取行数来找到它 我有一个管理名为 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 所以这意味着我的方法插入方法不起作用但它不知道为什么它没有通过错误!!!

最佳答案

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

关于android - 无法在索引处绑定(bind)参数,因为索引超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24366934/

相关文章:

android - 检查是否已调用 Activity 以获得结果

java - Android - 找不到抛出 NullPointerException 的内容

SQL(sqlite)比较由另一个重复行分组的行的总和

mysql - 使用 SQL 根据结果排序操作中的位置批量更新列?

java - 覆盖在非 Activity 类中不起作用

android - 我可以在不放入 Activity 的情况下覆盖/处理 fragment 中的 keyDown 吗?

android - 将不同大小的字符串/文本应用于 TextView

android - 应该如何关闭 InputStream 和 OutputStream?

android - 如何模拟:active css pseudo class in android on non-link elements?

mysql - 从 django 检索 mysql 输出 'manage.py sql appname'