尝试从数据库中获取数据时内存耗尽(android)

时间:2015-04-22 21:29:19

标签: java android database sqlite

我试图从我的数据库中获取一些信息。我是android的初学者。 我有一个数据库的创建类,名为"数据库"和一个数据库访问类,名为" Database_Acesso"。它们看起来像那样:

Database.java:     package workshopee.ct.ufrn.br.ssmonitor;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class Database extends SQLiteOpenHelper{
    private static final int versao_db = 1;

    private static final String nome_db = "ssmonitor_db";

    private static final String table1 = "phone";

    private static final String id = "_id";
    private static final String longitude = "longitude";
    private static final String latitude = "latitude";
    private static final String forca_torres = "qtdtorres";
    private static final String forca_dbm = "dbm";
    private static final String mcc = "mcc";
    private static final String mnc = "mnc";
    private static final String phone_type = "phone_type";
    private static final String operadora = "operadora";
    private static final String network_type = "networkType";
    private static final String cid = "cid";
    private static final String lac = "lac";

    public Database(Context context) {
        super(context, nome_db, null, versao_db);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        String criarTabela = "CREATE TABLE " + table1 + "("
                + id + " INTEGER PRIMARY KEY AUTOINCREMENT," + longitude + " REAL,"
                + latitude + " REAL," + forca_torres + " INTEGER," + forca_dbm + " REAL," + mcc + " INTEGER,"
                + mnc + " INTEGER," + phone_type + " TEXT," + operadora + " TEXT," + network_type + " INTEGER," + cid + " INTEGER,"
                + lac + " INTEGER )";
        db.execSQL(criarTabela);

               }
    @Override
    public void onUpgrade(SQLiteDatabase db, int versao_ant, int versao_nv) {
        Log.w(Database.class.getName(),
                "Atualizando o banco de dados da versão " + versao_ant + " para "
                        + versao_nv + ", isso apagará os dados antigos.");
        db.execSQL("DROP TABLE IF EXISTS " + table1 + ";");
        onCreate(db);

    }

    public void clear (SQLiteDatabase db) {
        Log.w(Database.class.getName(),
                "Apagando informações salvas anteriormente.");
        db.execSQL("DROP TABLE IF EXISTS " + table1 + ";");
        onCreate(db);
    }

}

Database_Acesso.java:

package workshopee.ct.ufrn.br.ssmonitor;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class Database_Acesso {
    private SQLiteDatabase db;

public Database_Acesso(Context context) {
    Database aux_db = new Database(context);
    db = aux_db.getWritableDatabase();
}
public void inserir_phone (Phone ph) {

    ContentValues valores = new ContentValues();
    valores.put("longitude",ph.getLongitude());
    valores.put("latitude", ph.getLatitude());
    valores.put("qtdtorres", ph.getTorres());
    valores.put("dbm", ph.getDbm());
    valores.put("mcc", ph.getMcc());
    valores.put("mnc", ph.getMnc());
    valores.put("phone_type", ph.getPhoneType());
    valores.put("operadora", ph.getOperadora());
    valores.put("cid",ph.getCid());
    valores.put("lac",ph.getLac());

    db.insert("phone", null, valores);
}


public List<Phone> buscar_phone () {
    List<Phone> lista = new ArrayList<Phone>();

    String[] colunas = new String[]{"_id", "longitude", "latitude", "qtdtorres", "dbm",
                        "mcc", "mnc", "phone_type", "operadora", "networkType", "cid", "lac"};

    Cursor cursor = db.query("phone", colunas, null, null, null, null,"_id ASC");
    if (cursor.getCount() > 0) {
        cursor.moveToFirst();
    }

   do {
       Phone p = new Phone();
       p.setId(cursor.getLong(0));
       p.setLongitude(cursor.getDouble(1));
       p.setLatitude(cursor.getDouble(2));
       p.setTorres(cursor.getInt(3));
       p.setDbm(cursor.getInt(4));
       p.setMcc(cursor.getInt(5));
       p.setMnc(cursor.getInt(6));
       p.setPhoneType(cursor.getString(7));
       p.setOperadora(cursor.getString(8));
       p.setNetWorkType(cursor.getString(9));
       p.setCid(cursor.getInt(10));
       p.setLac(cursor.getInt(11));
       lista.add(p);

   } while (!cursor.isLast());

    return lista;
  }

}

以下是我的MainActivity插入数据的部分:

    database_acesso.inserir_phone(cell);

其中database_acesso是Database_acesso的实例,cell是Phone的实例。

以下是我试图获取信息的方式:      TextView list_text_view =(TextView)rootView.findViewById(R.id.list_text_view);      List list = main.database_acesso.buscar_phone();      list_text_view.append(&#34; - &#34; + list.size());

我使用片段,所以&#34; main&#34;是MainActivity上的一个实例。 当我尝试执行它时,我收到以下错误:

java.lang.OutOfMemoryError: [memory exhausted]
        at dalvik.system.NativeStart.main(Native Method)

这是完整的堆栈跟踪。

解决这个问题的任何想法?

THX。

0 个答案:

没有答案