从sqlite android中显示空指针异常的空表中获取值
我想从表中得到一个值。如果列不包含任何值,那么我将插入一个值 如果有一个值,我想通知用户已经插入了一个值。当我的表不包含任何内容时,它会将错误显示为从sqlite android中显示空指针异常的空表中获取值,android,android-sqlite,Android,Android Sqlite,我想从表中得到一个值。如果列不包含任何值,那么我将插入一个值 如果有一个值,我想通知用户已经插入了一个值。当我的表不包含任何内容时,它会将错误显示为Java空指针异常。我想从我的表中得到一个空值。请帮忙 String qu="select * from userinfos"; c=db.rawQuery(qu,null); int count = c.getCount();
Java空指针异常
。我想从我的表中得到一个空值。请帮忙
String qu="select * from userinfos";
c=db.rawQuery(qu,null);
int count = c.getCount();
String[] values = new String[count + 1];
int i = 0;
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
values[i] = c.getString(c.getColumnIndex("mail"));
Toast.makeText(FiveActivity.this, values[i],Toast.LENGTH_LONG).show();
i++;
}
i=0;
if(values[i].equals(null))
{
String Query = "insert into userinfos (mail,pass) values ('"+ mailuser+ "','" + password + "')";
db.execSQL(Query);
Toast.makeText(FiveActivity.this,"Values Inserted",Toast.LENGTH_LONG).show();
}
else
{
for(int j=0;j<3;j++)
Toast.makeText(FiveActivity.this,"Already one Email-Id:"+ values[i]+" "+"you given. if you want to insert new mail and password then go to Delete app",Toast.LENGTH_LONG).show();
}
String qu=“从用户信息中选择*”;
c=db.rawQuery(qu,null);
int count=c.getCount();
字符串[]值=新字符串[计数+1];
int i=0;
for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
值[i]=c.getString(c.getColumnIndex(“邮件”);
Toast.makeText(FiveActivity.this,值[i],Toast.LENGTH_LONG).show();
i++;
}
i=0;
if(值[i].等于(空))
{
String Query=“插入userinfos(mail,pass)值(“+mailuser+”,“+password+”)”;
execSQL(查询);
Toast.makeText(FiveActivity.this,“插入值”,Toast.LENGTH_LONG.show();
}
其他的
{
对于(int j=0;j您只需检查您的计数
值,如下所示:
c=db.rawQuery(qu,null);
int count = c.getCount();
if(count==0)
{
////// No data found
}
else
{
/////data available
}
getCount()
返回光标中的行数
这是一个管理Usertable或其他文件的简单功能:
/**
* Check if data exist in table
*
* @param table
* The table to check data
* @param whereArgs
* The conditions to check the data
* @return boolean Return True while data exist; False while data not exist
*/
public boolean isDataExist(String table, String column, String[] whereArgs) {
boolean result = false;
SQLiteDatabase db = this.getWritableDatabase();
String sql = "select * from " + table + " where " + column + " = ?";
Cursor cursor = db.rawQuery(sql, whereArgs);
if (cursor.getCount() > 0) {
result = true;
}
cursor.close();
db.close();
return result;
}
首先检查你的count
valueif(count==0){未找到数据}否则{数据可用}
Wow…非常感谢…@MDYa…它工作了…谢谢你