1

在此处输入图像描述嘿,我正在尝试构建一个应用程序,我将值插入到数据库中,并尝试使用 4 种方法检索记录。方法:movetoFirst(), moveToLast(), moveToNext(), moveToPrevious()。方法 1 和 2 执行正常。但我面临下一个和上一个方法的问题。以下是我为上述查询附加的代码。

宣言。

package com.Rohit.taskforsqllite;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
EditText e1, e2, e3;
Cursor c;
SQLiteDatabase db;
int count;
TextView text;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    e1 = (EditText) findViewById(R.id.editText1);
    e2 = (EditText) findViewById(R.id.editText2);
    e3 = (EditText) findViewById(R.id.editText3);
    text = (TextView) findViewById(R.id.textviewcount);

    db = openOrCreateDatabase("Listname", MODE_PRIVATE, null);

    db.execSQL("create table if not exists list1(name varchar,location varchar,      phone number)");
    // Insert values into table
    db.execSQL("insert into list1 values('Rohit','London',12)");
    db.execSQL("insert into list1 values('Raj','Hyd',23)");
    db.execSQL("insert into list1 values('PRak','Canada',34)");
    db.execSQL("insert into list1 values('Sanj','Mad',45)");
    db.execSQL("insert into list1 values('Anna','Polland',56)");

    c = db.rawQuery("select * from list1", null);

    count = c.getCount();`import android.app.Activity;

方法1;

Button b1 = (Button) findViewById(R.id.startbutton);

    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            c.moveToFirst();

            String name = c.getString(c.getColumnIndex("name"));
            String location = c.getString((c.getColumnIndex("location")));
            String phone = c.getString((c.getColumnIndex("phone")));

            e1.setText(name);
            e2.setText(location);
            e3.setText(phone);
            Log.d("Record from db", name + "," + location + "," + phone);

        }
    });

方法二:

Button b2 = (Button) findViewById(R.id.finishbuton);
    b2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            // c = db.rawQuery("select * from list1", null);
            c.moveToLast();

            String name = c.getString(0);
            String location = c.getString(1);
            String phone = c.getString(2);

            e1.setText(name);
            e2.setText(location);
            e3.setText(phone);
            Log.d("Record from db", name + "," + location + "," + phone);

        }
    });

方法 3。如果我尝试使用 while 循环执行,这就是我面临的问题。它会执行到循环结束。但我不想这样做,我想在按下上一个按钮时一次显示一条记录。

Button b4 = (Button) findViewById(R.id.previousbutton);

    b4.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (c.moveToFirstt()) {
                do {
                    String name = c.getString(c.getColumnIndex("name"));
                    String location = c.getString(c
                            .getColumnIndex("location"));
                    String phone = c.getString(c.getColumnIndex("phone"));
                    e1.setText(name);
                    e2.setText(location);
                    e3.setText(phone);
                    Log.d("Record from db", name + "," + location + ","
                            + phone);

                } while (c.moveToNext());
            }

        }
    });
4

2 回答 2

1

如果你使用 moveToLast() 那么你必须移动到上一行并使用 c.moveToPrevious,没有 c.moveToNext()

于 2013-11-11T08:13:11.677 回答
0

我不确定你是否理解正确使用光标。请在此处阅读文档。

当您使用 moveToLast() 时,预计您只会循环 1 行...

循环遍历结果的正确结构如下:

while(c.moveToNext()) {
  //... fetch data from current row
}
于 2013-11-11T08:04:00.273 回答