当我尝试从Android中的SQLiteDB获取数据时,应用程序正在关闭

时间:2016-02-03 05:19:14

标签: android sqlite

数据已成功插入DB.But当我尝试检索数据时,app force关闭。 因此,方案是用户在活动中输入日期并单击按钮,在DB中进行搜索。如果匹配存在,我将在新活动中显示该内容。

ViewByDate活动

// Ground
var groundMaterial = new BABYLON.StandardMaterial("ground", scene);
groundMaterial.emissiveTexture = new BABYLON.Texture("textures/earth.jpg", scene);

var ground = BABYLON.Mesh.CreateGroundFromHeightMap("ground", "textures/worldHeightMap.jpg", 200, 200, 250, 0, 10, scene, false,
    mesh => {
        // Cloned Ground
        var groundMaterial2 = new BABYLON.StandardMaterial("ground2", scene);
        groundMaterial2.emissiveColor = new BABYLON.Color3(1, 0, 0);
        groundMaterial2.alpha = 0.5;

        var ground2 = mesh.clone("ground2");
        ground2.position = new BABYLON.Vector3(0, 1, 0);
        ground2.material = groundMaterial2;
    });
ground.material = groundMaterial;

当点击按钮时,editext值作为字符串传递给下一个活动。一旦完成,我就开始新的活动

ViewDateResult活动

        ViewDateResult vb=new ViewDateResult();
        String s=et1.getText().toString();
        vb.givedate(s);
        Intent i=new Intent(ViewByDate.this,ViewDateResult.class);
        ViewByDate.this.startActivity(i);

My Helper类的插入和检索:

EditText t1,t2,t3,t4;
SQLiteDatabase db;
public static String que;
void givedate(String date) 
{
 que=date;
 }
private static final String SELECT_SQL = "SELECT * FROM updatedata where Date = '" + que + "'";
private Cursor c;
protected void show()

{

   String In=c.getString(0);
   String out=c.getString(1);
   String date=c.getString(2);
   String Total=c.getString(3);
   t1.setText(In);
   t2.setText(out);

   t3.setText(date);
   t4.setText(Total);
   }


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_date_result);
    t1=(EditText)findViewById(R.id.id1);
    t2=(EditText)findViewById(R.id.id2);
    t3=(EditText)findViewById(R.id.id3);
    t4=(EditText)findViewById(R.id.id4);
     Inserted_DB in=new Inserted_DB(this);
   SQLiteDatabase db1=in.get();

    c= db1.rawQuery(SELECT_SQL,null);

    show();
    c.close();
    db1.close();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_view_date_result, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

logcat的:

SQLiteDatabase db;
Context ctx;
public helper help;

public Inserted_DB(Context ctx)
{
    this.ctx=ctx;
    help=new helper(ctx);
}


public static class helper extends SQLiteOpenHelper
{
    public helper(Context ctx)

    {
        super(ctx,dbname,null,2);

    }
    @Override
    public void onCreate(SQLiteDatabase db) {

       try
       {
           String sql = "create table "+ table +" (InTime text,OutTime text,Date text,Differ text)";
           db.execSQL(sql);

       }
       catch(Exception e1)
        {
            e1.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        db.execSQL("DROP TABLE IF EXISTS " + table);
        onCreate(db);
    }
}
 public void update(String In1,String out1,String date1,String d1)
   {

  db=help.getWritableDatabase();
  ContentValues con=new ContentValues();
  con.put(in,In1);
  con.put(out,out1);
  con.put(date,date1);
  con.put(diff,d1);
  db.insert(table,null,con);
  db.close();
 }
  public SQLiteDatabase get()
  {

    db=help.getReadableDatabase();
    return db;
  }

使用建议的更改更新了日志和代码。但仍然是同一个问题。

帮我解决这个问题。 提前致谢

2 个答案:

答案 0 :(得分:1)

1. You need to initalize db object.

YourDatabaseHelperclass database = new YourDatabaseHelperclass(this);
SQLiteDatabase db = database.getReadableDatabase();

2. you need to add single qotes in where condition.
"SELECT * FROM makeway where Date= '"+ que+"'" ;

EditText t1,t2,t3,t4;
SQLiteDatabase db;
public static String que;
void givedate(String date) 
{
 que=date;
 }
 private static final String SELECT_SQL = "SELECT * FROM makeway where Date = '"+ que+"'" ;
private Cursor c;
protected void show()

{

   String In=c.getString(0);
   String out=c.getString(1);
   String date=c.getString(2);
   String Total=c.getString(3);
   t1.setText(In);
   t2.setText(out);

   t3.setText(date);
   t4.setText(Total);
   }


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_date_result);
    t1=(EditText)findViewById(R.id.id1);
    t2=(EditText)findViewById(R.id.id2);
    t3=(EditText)findViewById(R.id.id3);
    t4=(EditText)findViewById(R.id.id4);
YourDatabaseHelperclass database = new YourDatabaseHelperclass(this);
SQLiteDatabase db = database.getReadableDatabase();
    c= db.rawQuery(SELECT_SQL,null);

    show();
      c.close();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_view_date_result, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

答案 1 :(得分:0)

在课堂上添加

SQLiteDatabase db;

@Override
    protected void onCreate(Bundle savedInstanceState) {

   db = new DBAdapter(ChangePasswordActivity.this);

   db.open();
        //code here
        c= db.rawQuery(SELECT_SQL,null);
   db.close();
}

更改

private static final String SELECT_SQL = "SELECT * FROM makeway where Date="+ que ;

private static final String SELECT_SQL = "SELECT * From makeway where Date = '" + que + "'";