SQLITE检索数据错误

时间:2017-04-26 05:26:41

标签: android sqlite android-sqlite

我想在 SQLITE 数据库中保存一些数据。但这段代码似乎不起作用。

这是我的数据库助手代码:

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

import java.util.ArrayList;

/**
 * Created by ProgrammingKnowledge on 4/3/2015.
 */
public class saveClients extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "client.db";
    public static final String TABLE_NAME = "Client_Info";
    public  static  final int ver=1;
    public static int i=0;
    public static final String COL_1 = "ID";
    public static final String COL_2 = "NAME";
    public static final String COL_3 = "Email";
    public static final String COL_4 = "Balance";

    public saveClients(Context context) {
        super(context, DATABASE_NAME, null, ver);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_NAME +" (ID INTEGER,NAME TEXT,Email TEXT,Balance TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
        onCreate(db);
    }

    public boolean insertData(String ID,String Fname,String Email,String Balance) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_1,ID);
        contentValues.put(COL_2,Fname);
        contentValues.put(COL_3,Email);
        contentValues.put(COL_4,Balance);
        long result = db.insert(TABLE_NAME,null ,contentValues);
        if(result == -1)
            return false;
        else
            return true;
    }


  public boolean checkForTables(){
      SQLiteDatabase db = this.getWritableDatabase();
      boolean hasTables = false;
      db = this.getWritableDatabase();
      Cursor cursor = db.rawQuery("SELECT * FROM " +TABLE_NAME, null);

      if(cursor != null && cursor.getCount() > 0){
          hasTables=true;
          cursor.close();
      }

      return hasTables;
  }


    public void deleteData () {
        SQLiteDatabase db = this.getWritableDatabase();
         db.execSQL("delete * from"+ TABLE_NAME);
    }

    public ArrayList<String> getAllData() {
        ArrayList<String> list=new ArrayList<>();
        String QUERY = "SELECT  * FROM " + TABLE_NAME;

        SQLiteDatabase db = this.getWritableDatabase();

        Cursor cursor = db.rawQuery(QUERY, null);
        if(cursor!=null) {
            while (cursor.moveToNext()) {
                list.add(cursor.getString(i));
                i++;

            }
        }else{
            list.add("X");
        }
        return list;
    }


}

这是我的客户活动代码:下方:

 import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;

    import com.sdsmdg.tastytoast.TastyToast;

    import java.util.ArrayList;

    public class clientView extends AppCompatActivity {
        TextView Fname, Email, ID, Balance;
        final saveClients obj = new saveClients(this);

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_client_view);
            if (obj.checkForTables() == false) {
                String getingid = getIntent().getStringExtra("ID");
                String getingname = (getIntent().getStringExtra("Fname"));
                String getinmail = (getIntent().getStringExtra("Email"));
                String getingBalan = (getIntent().getStringExtra("Balance"));
                obj.insertData(getingid, getingname, getinmail, getingBalan);
                TastyToast.makeText(clientView.this, "Logged In", TastyToast.SUCCESS, TastyToast.LENGTH_LONG).show();
            }


            ArrayList<String> data = new ArrayList<>(obj.getAllData());
            // ---------------------------------------------------------------------------------------------
            // ---------------------------------------------------------------------------------------------
            if(data.get(0).equals("X")){
                Toast.makeText(this, "Database Error", Toast.LENGTH_SHORT).show();
            }
            Fname = (TextView) findViewById(R.id.Fname);
            Email = (TextView) findViewById(R.id.Email);
            ID = (TextView) findViewById(R.id.ID);
            Balance = (TextView) findViewById(R.id.Balance);
            Button button = (Button) findViewById(R.id.lgout);
            ID.setText(data.get(0));
            Fname.setText(data.get(1));
            ID.setText(data.get(2));
            Balance.setText(data.get(3));

            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Fname.setText("");
                    Email.setText("");
                    ID.setText("");
                    Balance.setText("");
                    obj.deleteData();
                    Intent pushback = new Intent(clientView.this, MainActivity.class);
                    startActivity(pushback);
                }
            });

            // ---------------------------------------------------------------------------------------------
            // ---------------------------------------------------------------------------------------------

        }
    }

我想通过 getAlldata()方法检索所有数据。但是它一直在崩溃。另外一个问题是可以使用sql查询来检索来自SQLITE数据库的任何数据。然后请分享我怎么能做到这一点。

1 个答案:

答案 0 :(得分:0)

你可以试试这个

 public ArrayList<String> getAllData()
 {
         ArrayList<String> resultList=new ArrayList<>();
         String QUERY = "SELECT  * FROM " + TABLE_NAME;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(QUERY, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst())
        {
            do {
                resultList.add((cursor.getInt(0))); // Fetch ID
                resultList.add((cursor.getString(1))); // Fetch FName

            } while (cursor.moveToNext());

            cursor.close();
            db.close();
        }

        // return list
        return resultList;
  }

然后卸载旧应用&amp;跑。