无法从SQLite数据库中检索数据

时间:2014-12-10 11:22:07

标签: java android sqlite

我创建了一个sqlite数据库,并试图通过单击按钮查看输入到我的数据库中的数据。按钮单击将打开一个新活动并在textview中显示数据,但似乎没有任何事情发生。请告诉我我的代码有什么问题。感谢。

View.java

package com.firstproject.aditya;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class View extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view);
        TextView tv= (TextView)findViewById(R.id.tvSQLinfo);
        Database info= new Database(this);
        info.open();
        String data=info.getData();
        info.close();
        tv.setText(data);
    }



}

Activity2.java

package com.firstproject.aditya;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Activity2 extends Activity implements View.OnClickListener {
    Button startDate, targetDate, remDate, save, cancel;
    TextView goalInfo, title, des, stDate, tgDate,rdDate;
    EditText edTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act2);
        initialize();
        startDate.setOnClickListener(this);
        cancel.setOnClickListener(this);
        targetDate.setOnClickListener(this);
        save.setOnClickListener(this);
        remDate.setOnClickListener(this);

        /*
         * Bundle gotBasket = getIntent().getExtras(); String
         * gotBread1=gotBasket.getString("key"); edsd.setText(gotBread1);
         */

    }

    private void initialize() {
        // TODO Auto-generated method stub
        startDate = (Button) findViewById(R.id.bStart);
        targetDate = (Button) findViewById(R.id.bTarg);
        remDate = (Button) findViewById(R.id.brem);
        save = (Button) findViewById(R.id.bsave);
        cancel = (Button) findViewById(R.id.bcancel);
        goalInfo = (TextView) findViewById(R.id.tvGI);
        title = (TextView) findViewById(R.id.tvTitle);
        stDate = (TextView) findViewById(R.id.textViewStartDate);
        tgDate = (TextView) findViewById(R.id.textViewTargetDate);
        rdDate = (TextView) findViewById(R.id.textViewReminderDate);

        edTitle = (EditText) findViewById(R.id.editText1);


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        switch(requestCode){
        case (0):{
            if (resultCode == RESULT_OK) {
                Bundle bsket = data.getExtras();
                String s = bsket.getString("answer");
                stDate.setText(s);
        }
            break;
        }

        case (2):{
            if (resultCode == RESULT_OK) {
                Bundle bsket = data.getExtras();
                String s = bsket.getString("answer");
                tgDate.setText(s);
            }
            break;
        }

        case (1):{
            if (resultCode == RESULT_OK) {
                Bundle bsket = data.getExtras();
                String s = bsket.getString("answer");
                rdDate.setText(s);
            }
            break;
        }
        }


    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v == cancel) {
            Intent canc = new Intent("com.firstproject.aditya.ACTIVITY1");
            startActivity(canc);
        }

        if (v == startDate) {
            Intent sdate = new Intent(Activity2.this,DatePick.class);
            startActivityForResult(sdate,0);
        }

        if (v == targetDate) {
            Intent tdate = new Intent(Activity2.this,DatePick.class);
            startActivityForResult(tdate,2);
        }

        if (v == remDate) {
            Intent rdate = new Intent(Activity2.this,DatePick.class);
            startActivityForResult(rdate,1);
        }

    switch(v.getId()){
    case R.id.bsave:
                boolean didItWork=true;
            try{
                String gTitle=edTitle.getText().toString();
                String dateS=stDate.getText().toString();
                String dateT=tgDate.getText().toString();
                String dateR=rdDate.getText().toString();
                Database entry =new Database(Activity2.this);
                entry.open();
                entry.createEntry(gTitle,dateS,dateT,dateR);
                entry.close();
            }catch(Exception e){
                didItWork=false;
                String error=e.toString();
                Dialog d =new Dialog(this);
                d.setTitle("Dang it!");
                TextView txtV=new TextView(this);
                txtV.setText(error);
                d.setContentView(txtV);
                d.show();

            }finally{
                if(didItWork){
                    Dialog d =new Dialog(this);
                    d.setTitle("Heck Yeah!");
                    TextView txtV=new TextView(this);
                    txtV.setText("Success");
                    d.setContentView(txtV);
                    d.show();
                }
            }
            break;

    case R.id.bview:
        Intent i=new Intent("com.firstproject.aditya.VIEW");
        startActivity(i);
        break;
        }
    }

}

Database.java

package com.firstproject.aditya;

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

public class Database {

    public static final String KEY_ROWID = "_id";
    public static final String KEY_TITLE = "goals_title";
    public static final String KEY_STDATE = "start_date";
    public static final String KEY_TGDATE = "target_date";
    public static final String KEY_RDDATE = "reminder_date";

    private static final String DATABASE_NAME = "GoalInformation";
    private static final String DATABASE_TABLE = "GoalTable";
    private static final int DATABASE_VERSION = 1;


    private DbHelper ourHelper;
    private final Context ourContext;
    private android.database.sqlite.SQLiteDatabase ourDatabase;

    private static class DbHelper extends SQLiteOpenHelper {

        public DbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(android.database.sqlite.SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
                    + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_TITLE
                    + " TEXT NOT NULL, " + KEY_STDATE + " TEXT NOT NULL, "
                    + KEY_TGDATE + " TEXT NOT NULL, " + KEY_RDDATE
                    + " TEXT NOT NULL);"

            );
        }

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

    }

    public Database(Context c) {
        ourContext = c;
    }

    public Database open() throws SQLException {
        ourHelper = new DbHelper(ourContext);
        ourDatabase = ourHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        ourHelper.close();
    }

    public long createEntry(String gTitle, String dateS, String dateT,
            String dateR) {
        // TODO Auto-generated method stub
        ContentValues cv = new ContentValues();
        cv.put(KEY_TITLE, gTitle);
        cv.put(KEY_STDATE, dateS);
        cv.put(KEY_TGDATE, dateT);
        cv.put(KEY_RDDATE, dateR);
        return ourDatabase.insert(DATABASE_TABLE, null, cv);
    }

    public String getData() {
        // TODO Auto-generated method stub
        String[] columns = new String[] { KEY_ROWID, KEY_TITLE, KEY_STDATE,
                KEY_TGDATE, KEY_RDDATE };
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
                null, null);
        String result = "";

        int iRow = c.getColumnIndex(KEY_ROWID);
        int iTitle = c.getColumnIndex(KEY_TITLE);
        int iStdate = c.getColumnIndex(KEY_STDATE);
        int iTgdate = c.getColumnIndex(KEY_TGDATE);
        int iRddate = c.getColumnIndex(KEY_RDDATE);

        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
            result = result + c.getString(iRow) + " " + c.getString(iTitle)
                    + " " + c.getString(iStdate) + " " + c.getString(iTgdate)
                    + " " + c.getString(iRddate) + "\n";
        }

        return result;
    }
}

1 个答案:

答案 0 :(得分:0)

我不知道这个答案是否有用。 当我开始使用SQLite连接时,我尝试使用像你这样的"查询" 方法,但有几个问题(似乎有些probs与此类似)。 我开始使用 rawQuery 方法,现在一切正常。

可能我只是失败了,但这是解决我的问题的方法。