【问题标题】:How can i view content from sqlite database如何查看 sqlite 数据库中的内容
【发布时间】:2015-05-05 09:14:23
【问题描述】:

我已经做了一个程序,用于从 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 

        }


}

【问题讨论】:

    标签: android android-sqlite data-retrieval


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 结束do/while时数据字符串为空?
      • 点击查看按钮时总是显示不幸停止工作错误
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-25
      • 2014-05-10
      • 2019-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-13
      相关资源
      最近更新 更多