看起来没有数据输入行

时间:2014-07-13 07:59:09

标签: android sqliteopenhelper

我是相对的菜鸟。当使用我的getAnimal()方法检索一行时,它失败了" CursorIndexOutOfBoundsexception索引0请求大小为0.然后我看了一下moveToFirst()并且它总是返回false ........

在数据库中插入行会返回递增索引。当我运行db.insert()方法时,getCount()返回递增的行数。因此该表似乎有列但行中没有数据。我是否错误地输入了行或者我还在做什么呢?

这是DatabaseHelper活动

package com.example.testdbrev2on071314;

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

public class DatabaseHandler extends SQLiteOpenHelper {



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

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

// Contacts table name
private static final String TABLE_ANIMAL = "strings";

// Contacts Table Columns names
private static final String KEY_ID= "id";
private static final String KEY_NAME = "name";
private static final String KEY_TYPE = "type";

public String name1="Spiders";
public String type1="Black Widow";      


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


// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_BIDS_TABLE = "CREATE TABLE " + TABLE_ANIMAL + "("+ KEY_ID + " INTEGER PRIMARY           KEY AUTOINCREMENT,"
            + KEY_NAME + " TEXT," + KEY_TYPE + " TEXT)";
    db.execSQL(CREATE_BIDS_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_ANIMAL);

    // Create tables again
    onCreate(db);
}


// Adding new contact
public long addBid() {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_NAME,name1); 
    values.put(KEY_TYPE,type1);
    long ret= db.insert(TABLE_ANIMAL, null, values);
    db.close(); 
    return ret;
}


     String getAnimal(int id) {
         String name;
     SQLiteDatabase db = this.getReadableDatabase();
     Cursor cursor = db.query(TABLE_ANIMAL, new String[] { KEY_ID,
             KEY_NAME,KEY_TYPE }, KEY_ID + "=?",
             new String[] { String.valueOf(id) }, null, null, null, null);
     boolean moved=cursor.moveToFirst();
     if(cursor != null) {name="Cursor OK";} 
     if(!moved){    name="Not at first";} else {name="at first";}
     return name;
 }

public int getCount() {
   String countQuery = "SELECT  * FROM " + TABLE_ANIMAL;
   SQLiteDatabase db = this.getReadableDatabase();
   Cursor cursor = db.rawQuery(countQuery, null);
   int count=cursor.getCount();
   cursor.close();
   return count;
   }    

}

MainActivity如下:

package com.example.testdbrev2on071314;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;


public class MainActivity extends Activity {
TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv=(TextView) findViewById(R.id.number);
    DatabaseHandler db = new DatabaseHandler(this);
    String  num=Integer.toString((int)db.addBid());
//  String num= db.getAnimal(0);

tv.setText(num);

}
}

搜索高低,疯狂,所以任何帮助都会非常感激

0 个答案:

没有答案