SQLite - android - 显示现有数据库中的数据

时间:2016-05-22 03:17:42

标签: android sqlite

我总是有这个问题,我无法在我的活动中显示DB的数据!

enter image description here

要开始,我尝试做这样的事情:我的班级 BDHelper

#include <conio.h>
#include <iostream>
#include <random>
#include <time.h>
#include <fstream>

using std::cout;
using std::endl;
using std::cin;

using namespace std;

int main()
{

    int a;

    srand(time(NULL));

    for (int i(0); i < 10; i++)
    {
        a = rand() % 1000 + 1;// diapason

        cout << "ID: " << i << " Random: " << a << endl;

        std::ofstream outfile("test.txt");

        outfile << a << std::endl;

        outfile.close();

    }


    _getch();
    return 0;
}

我正试图在主要活动中展示它,这就是我这样做的原因:

import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHelper extends SQLiteOpenHelper {

   public static final String DATABASE_NAME = "lm";
   public static final String EMP_TABLE = "contacts";
   public static final String _id = "id";
   public static final String E_NAME = "name";
   public static final String E_AGE = "age";
   public static final String E_DEPT = "dept";


   public DataBaseHelper(Context context)
   {
      super(context, DATABASE_NAME , null, 1);
   }

   @Override
   public void onCreate(SQLiteDatabase db) {
      // TODO Auto-generated method stub
      db.execSQL(
      "create table contacts " +
      "(id integer primary key, name text,phone text,email text, street text,place text)"
      );
   }

   @Override
   public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      // TODO Auto-generated method stub
      db.execSQL("DROP TABLE IF EXISTS contacts");
      onCreate(db);
   }

   public boolean insertContact  (String name, String age, String dept)
   {
      SQLiteDatabase db = this.getWritableDatabase();
      ContentValues contentValues = new ContentValues();
      contentValues.put("name", name);
      contentValues.put("age", age);    
      contentValues.put("dept", dept);
      db.insert("contacts", null, contentValues);
      return true;
   }

   public Cursor getData(int id){
      SQLiteDatabase db = this.getReadableDatabase();
      Cursor res =  db.rawQuery( "select * from contacts where id="+id+"", null );
      return res;
   }

   public int numberOfRows(){
      SQLiteDatabase db = this.getReadableDatabase();
      int numRows = (int) DatabaseUtils.queryNumEntries(db, EMP_TABLE);
      return numRows;
   }

   public boolean updateContact (Integer id, String name,String age, String dept)
   {
      SQLiteDatabase db = this.getWritableDatabase();
      ContentValues contentValues = new ContentValues();
      contentValues.put("name", name);
      contentValues.put("age", age);    
      contentValues.put("dept", dept);
      db.update("contacts", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
      return true;
   }

  /* public Integer deleteContact (Integer id)
   {
      SQLiteDatabase db = this.getWritableDatabase();
      return db.delete("contacts", 
      "id = ? ", 
      new String[] { Integer.toString(id) });
   }*/

   public ArrayList<String> getAllCotacts()
   {
      ArrayList<String> array_list = new ArrayList<String>();

      SQLiteDatabase db = this.getReadableDatabase();
      Cursor res =  db.rawQuery( "select * from contacts", null );
      res.moveToFirst();

      while(res.isAfterLast() == false){
         array_list.add(res.getString(res.getColumnIndex(EMP_TABLE)));
         res.moveToNext();
      }
   return array_list;
   } 

我总是得到相同的结果:一个空的活动。我尝试过其他方式,并查看其他类似问题的问题,我只是想在这里错过它!

3 个答案:

答案 0 :(得分:0)

以下列方式创建表格

db.execSQL(
          "create table if not exists contacts " +
          "(id integer primary key, name text, phone text, email text, street text, place text );   "
);

将您的getAllContacts更改为此类

public ArrayList<String> getAllCotacts()
   {
      ArrayList<String> array_list = new ArrayList<String>();

      SQLiteDatabase db = this.getReadableDatabase();
      Cursor res =  db.rawQuery( "select * from contacts", null );
     if (res.getCount() != 0) {
            for (res.moveToFirst(); !res.isAfterLast(); res.moveToNext()) {
         array_list.add(res.getString(res.getColumnIndex(EMP_TABLE)));
      } 
     }
   return array_list;
   } 

您需要将onCreate更改为以下内容:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mydb = new DataBaseHelper(this);
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,mydb.getAllContacts);

    obj = (ListView) findViewById(R.id.listView1);
    obj.setAdapter(arrayAdapter);
}

我没试过这个,但希望这会有所帮助:)

答案 1 :(得分:0)

首先 - 创建一个名为Contact.java的类,并为“”id“,name”,“age”和“dept”创建set和get方法。

第二步 - 转到你的DbAdapter类并将此代码放入getAllContacts()

static Properties properties = new Properties();
    static {
        properties.put("mail.smtp.host", "smtp.gmail.com");
        properties.put("mail.smtp.socketFactory.port", "465");
        properties.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.port", "587");

不要忘记按照列顺序使用cursor.get ..

希望有所帮助

答案 2 :(得分:-1)

尝试

    db.execSQL(
          "create table contacts " +
          "(id integer primary key, name text,phone text,email text, street text,place text)  ;   "
);

查看text)&amp;之间的分号。 "?那是因为你的创作永远不会结束。