在Cursor cursor = mDb.query(TABLENAME, null, KEY_ID + "=" + uid, null, null,null, null, null);中加入KEY_ID + "=" + uid运行就会出现"the application has stropped unexpectedly,please try again";若将条件去掉,改为null,就能正常跳转到information页面,不知道代码哪里写错了,源代码如下:
public class Jtest extends Activity {

Button btn;
EditText txt1,txt2;
String uid,pwd;

private static final String DATABASENAME="Test.db";
private static final String TABLENAME="Ttable";
private static final int VERSION=1;

private static final String KEY_ID="userid";
private static final String KEY_PASSWORD="password";

private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;

private static String sql = "CREATE TABLE " + TABLENAME + " (" + KEY_ID
                          + " text not null, " + KEY_PASSWORD + " text not null " + ");";

private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASENAME, null, VERSION);
} @Override
public void onCreate(SQLiteDatabase db) {
   
db.execSQL(sql); } @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

db.execSQL("DROP TABLE IF EXISTS " + TABLENAME); 
onCreate(db);
}
} @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mDbHelper = new DatabaseHelper(this);

mDb= mDbHelper.getWritableDatabase();
String sql1 = "insert into " + TABLENAME + " (" + KEY_ID + ", " + KEY_PASSWORD
+ ") values('admin', 'admin');";

mDb.execSQL(sql1);

btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new Button.OnClickListener()  { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
     
txt1=(EditText)findViewById(R.id.userid);
txt2=(EditText)findViewById(R.id.password);

uid=txt1.getText().toString();
pwd=txt2.getText().toString();

mDb = mDbHelper.getReadableDatabase();
       Cursor cursor = mDb.query(TABLENAME, null, KEY_ID + "=" + uid, null, null,
           null, null, null);
     if(cursor.getCount()>0)
      {
                    Intent intent=new Intent();

intent.setClass(Jtest.this, Information.class);
startActivity(intent);
}
else  {
Toast.makeText(Jtest.this, "UserId or Password error!", Toast.LENGTH_LONG).show();

}
});
}}

解决方案 »

  1.   

    使用完数据库就关闭吧。cursor 也要关闭哇。
    还有,你把你的数据库封装的功能全一点啊。增删改查,关闭一个都不能少,反正以后都可以用的。
      

  2.   

    mDb.query(TABLENAME, null, KEY_ID + "=?", new String[]{uid}, null,null, null, null)
      

  3.   

    楼上万分感谢!!如果我要进行KEY_ID和KEY_PASSWORD判断呢?应该怎样写??mDb.query(TABLENAME, null,new String[] { KEY_ID + "=?", KEY_PASSWORD + "=?"}, new String[]{uid,pwd}, null,null, null, null);吗?
      

  4.   

    是这样的~ 多看api帮助文档~ 或者用rawQuery方法,写个带占位符的sql及占位符对应内容的String[]