SQLiteDatabase在android中运行

时间:2013-11-11 07:56:26

标签: android sqlite

enter image description here嘿,我正在尝试构建一个应用程序,我将值插入数据库,并尝试使用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);

        }
    });

方法2:

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());
            }

        }
    });

2 个答案:

答案 0 :(得分:1)

如果你使用moveToLast()然后你必须移动到前一行并使用c.moveToPrevious,没有c.moveToNext()

答案 1 :(得分:0)

我不确定您是否理解正确使用Cursor。请阅读文档here

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

循环结果的正确构造如下:

while(c.moveToNext()) {
  //... fetch data from current row
}