如何查看sqlite数据库中的内容

时间:2015-03-04 11:39:59

标签: android android-sqlite data-retrieval

我已经完成了从android sqlite数据库插入和检索数据的程序..

插入工作正常。但观看数据不会发生。 我的代码附在下面。请帮我处理以下代码

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class ViewActivity extends Activity  {
    SQLiteDatabase myDB = null;
    String TableName = "LIST";
    String Data = "";


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view);
    }
    public void show1(View v) { 

          Cursor c = myDB.rawQuery("SELECT * FROM  LIST", null);

            int Column1 = c.getColumnIndex("Name");
            int Column2 = c.getColumnIndex("Address");
            int Column3 = c.getColumnIndex("Phone");

            c.moveToFirst();

            if (c != null) {

                do {
                    String Name = c.getString(Column1);
                    String Address = c.getString(Column2);
                    String Phone = c.getString(Column3);
                    Data = Data + Name + "\t\t" + Address + "\n\n" + Phone + "\n\n";

                } while (c.moveToNext());
            }

            TextView tv = (TextView) findViewById(R.id.show1);; // creating Text View to show data in the app
           tv.setTextColor(Color.WHITE);
           tv.setBackgroundResource(R.drawable.b);
            tv.setTextSize(18F);
            tv.setText("\n"+"Name \t| Address \n \t| Phone \n ----------------------\n");
           setContentView(tv);  // set created text view as Content View 

        }


}

2 个答案:

答案 0 :(得分:0)

检查您的表名和列名,似乎始终是常见问题。一个问题,如果您将结果附加到数据字符串中,为什么不使用它来显示在您的活动上? do while block后数据字符串是否为空?

其次,您应该尝试使用数据访问对象模式http://en.wikipedia.org/wiki/Data_access_object,您的代码将更加可维护和整洁。

答案 1 :(得分:0)

得到了答案......看起来很简单

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class MainActivity extends Activity {
     SQLiteDatabase db;
      TextView tv;
      EditText et1,et2,et3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

      //initialize all view objects
        tv=(TextView)findViewById(R.id.textView1);
        et1=(EditText)findViewById(R.id.editText1);
        et2=(EditText)findViewById(R.id.editText2);
        et3=(EditText)findViewById(R.id.editText3);

      //create database if not already exist
        db= openOrCreateDatabase("Anoop", MODE_PRIVATE, null);
        //create new table if not already exist
        db.execSQL("create table if not exists mytable(name varchar, address varchar,phone varchar)");
        }
        //This method will call on when we click on insert button
        public void insert(View v)
        {
         String name=et1.getText().toString();
         String address=et2.getText().toString();
         String phone=et3.getText().toString();
         et1.setText("");
         et2.setText("");
         et3.setText("");
         //insert data into able
         db.execSQL("insert into mytable values('"+name+"','"+address+"','"+phone+"')");
        //display Toast
        Toast.makeText(this, "values inserted successfully.", Toast.LENGTH_LONG).show();
         }
        //This method will call when we click on display button
        public void display(View v)
        {
        //use cursor to keep all data
        //cursor can keep data of any data type
        Cursor c=db.rawQuery("select * from mytable", null);
        tv.setText("");
        //move cursor to first position
        c.moveToFirst();
        //fetch all data one by one
        do
        {
         //we can use c.getString(0) here
         //or we can get data using column index
         String name=c.getString(c.getColumnIndex("name"));
         /*String address=c.getString(1);
         String phone=c.getString(2);*/
         String address=c.getString(c.getColumnIndex("address"));
         String phone=c.getString(c.getColumnIndex("phone"));
         //display on text view
         tv.append("Name:"+name+"  Address:"+address+"   Phone:"+phone+"\n");
         //move next position until end of the data
        }while(c.moveToNext());
       }
     }