从SQLite数据库获取数据时出错

时间:2016-11-06 15:40:14

标签: android sqlite android-sqlite

我写了一个应用程序并将数据存储到数据库中 问题是,当我尝试从数据库中获取数据时,我得到一个空列表。

有什么问题?我会发布我的代码,如果有人有想法,请告诉我:))

这是我的数据库助手:

public class DatabaseHelpher extends SQLiteOpenHelper {
    Context context;
    private static final String DATABASE_NAME="TODO.DB";
    private static final int DATABASE_VERSION = 1;
    private static final String Todo_id= "Todo_id";
    private static final String COLMN_TODO_TITLE = "TODO_TITLE";
    private static final String COLUMN_Priority = "priority";
    private static final String TODO_TABLE = "TODO";

    private static final String createTodoTable = "CREATE TABLE " +TODO_TABLE +
            "(" +Todo_id+ " INTEGER PRIMARY KEY AUTOINCREMENT, " +COLMN_TODO_TITLE+
            " VARCHAR, " +COLUMN_Priority+
            " VARCHAR);";
    public DatabaseHelpher(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context = context;
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
            db.execSQL(createTodoTable);}
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    public void insertIntoDB(String Title,String Priority){
        Log.d("insert", "before insert");
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLMN_TODO_TITLE, Title);
        values.put(COLUMN_Priority, Priority);
        db.insert(TODO_TABLE, null, values);
        db.close();
    }


    public List<Data> getDataFromDB(){
        List<Data> modelList = new ArrayList<Data>();
        String query = "select * from "+ TODO_TABLE;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query,null);
        if (cursor.moveToFirst()){
            do {
                Data model = new Data();
                String Title = cursor.getString(cursor.getColumnIndex(COLMN_TODO_TITLE));
                String Description = cursor.getString(cursor.getColumnIndex(COLUMN_Priority));
                model.setTODO_TITLE(Title);
                model.setPriority(Description);

                modelList.add(model);
            }while (cursor.moveToNext());
        }
        return modelList;
    }
}

这就是我插入和获取数据的方式:

 public void getLocalData ()
{
    helpher = new DatabaseHelpher(this);
    dbList= new ArrayList<Data>();
    dbList.addAll(helpher.getDataFromDB());
}
 private void insert(String Title, String prority ) {
    db.insertIntoDB(Title, prority);
}

Data.class

public class Data  {

String TODO_TITLE ;
String priority;

public String getTODO_TITLE() {
    return TODO_TITLE;
}

public void setTODO_TITLE(String TODO_TITLE) {
    this.TODO_TITLE = TODO_TITLE;
}

public String getPriority() {
    return priority;
}

public void setPriority(String priority) {
    this.priority = priority;
}

1 个答案:

答案 0 :(得分:-1)

我可以从插件删除中提供完整的代码并从数据库中获取数据

package com.mssinfotech.dropjourney;

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;
import android.util.Log;

import java.util.ArrayList;

public class DBHelper extends SQLiteOpenHelper {

   public static final String DATABASE_NAME = "MyDBName.db";
   public static final String TABLE_NAME = "login";

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

   @Override
   public void onCreate(SQLiteDatabase db) {
      // TODO Auto-generated method stub
      db.execSQL(
      "CREATE TABLE IF NOT EXISTS " + TABLE_NAME +
      "(id integer PRIMARY KEY AUTOINCREMENT, phone text, avatar text, name text)"
      );
      Log.d("create", "table ceated");
   }

   @Override
   public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      // TODO Auto-generated method stub
      db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
      onCreate(db);
      Log.d("create", "table droped");
   }

   public boolean insertUser  (Integer id, String phone, String avatar,String name)
   {
      SQLiteDatabase db = this.getWritableDatabase();
      db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
      onCreate(db);
      ContentValues contentValues = new ContentValues();
      contentValues.put("id", id);
      contentValues.put("phone", phone);
      contentValues.put("avatar", avatar);
      contentValues.put("name", name);
      db.insert(TABLE_NAME, null, contentValues);
      Log.d("Inserted", "Record Inserted");
      return true;
   }
   public Cursor getUserData(int id){
      SQLiteDatabase db = this.getReadableDatabase();
      onCreate(db);
      Cursor res =  db.rawQuery( "select * from "+TABLE_NAME+" where id="+id+"", null );
      return res;
   }

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

   public boolean updateUser (Integer id, String phone, String avatar, String name)
   {
      SQLiteDatabase db = this.getWritableDatabase();
      onCreate(db);
      ContentValues contentValues = new ContentValues();
      contentValues.put("phone", phone);
      contentValues.put("avatar", avatar);
      contentValues.put("name", name);
      db.update(TABLE_NAME, contentValues, "id = ? ", new String[] { Integer.toString(id) } );
      return true;
   }
   public void deleteAllUser(){
      SQLiteDatabase db = this.getWritableDatabase();
      db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
      onCreate(db);
   }
   public Integer deleteUser (Integer id)
   {
      SQLiteDatabase db = this.getWritableDatabase();
      return db.delete(TABLE_NAME, 
      "id = ? ", 
      new String[] { Integer.toString(id) });
   }

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

      //hp = new HashMap();
      SQLiteDatabase db = this.getReadableDatabase();
      Cursor res =  db.rawQuery( "select * from "+TABLE_NAME, null );
      res.moveToFirst();

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

现在如何插入

DBHelper mydb = new DBHelper(this);
String avatar = "ImageURL";
String name = "name";
String mobileno = "mEmail";
Integer id=1;
mydb.insertUser(id, mobileno, avatar, name);

如何获取

DBHelper mydb = new DBHelper(this);
Integer c=mydb.numberOfRows();
if(c>0){
  // do your code
}

如何删除

DBHelper mydb = new DBHelper(this);
mydb.deleteAllUser();