Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android单击按钮调用布尔函数检查表是否存在_Android_Sqlite - Fatal编程技术网

Android单击按钮调用布尔函数检查表是否存在

Android单击按钮调用布尔函数检查表是否存在,android,sqlite,Android,Sqlite,我完全卡住了。我整天都在用头撞砖墙 我有一门课: public class Main extends Activity. 这有几个按钮。 在其中两个按钮上,我想得到一个布尔函数的结果,该函数检查数据库表是否存在,以便我可以选择下一个操作 final Button reminderButton = (Button) findViewById(R.id.reminders); reminderButton.setOnClickListener(new View.OnClickListener(

我完全卡住了。我整天都在用头撞砖墙

我有一门课:

public class Main extends Activity. 
这有几个按钮。 在其中两个按钮上,我想得到一个布尔函数的结果,该函数检查数据库表是否存在,以便我可以选择下一个操作

final Button reminderButton = (Button) findViewById(R.id.reminders);

reminderButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v){

        //This is where I want to get the result from the boolean function
        // and then either provide a toast msg OR start a new activity
    }   
});
下面是布尔函数

public boolean isTableExists(SQLiteDatabase db, String tableName) {
    if (tableName == null || db == null || !db.isOpen()) 
    {
        return false;
    }
    Cursor cursor = db.rawQuery("SELECT COUNT(*) FROM sqlite_master WHERE type = ? 
                         AND name = ?",new String[] { "table", tableName });
    if (!cursor.moveToFirst())
    {
        return false;
    }
    int count = cursor.getInt(0);
    cursor.close();
    return count > 0;
}
就我的一生而言,我无法计算出放在onclick(视图v){}中的语法

我试过了

public void onClick(View v) 
{
    isTableExists(null, null);
}
但是我不认为null,null是正确的,但是里面有什么参数呢

还是我完全走错了路


感谢您在帖子中提供的建议,我知道您想检查表是否存在

此代码将帮助您

public boolean isTableExists(String tableName, boolean openDb) {
    if(openDb) {
        if(mDatabase == null || !mDatabase.isOpen()) {
            mDatabase = getReadableDatabase();
        }

        if(!mDatabase.isReadOnly()) {
            mDatabase.close();
            mDatabase = getReadableDatabase();
        }
    }

    Cursor cursor = mDatabase.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '"+tableName+"'", null);
    if(cursor!=null) {
        if(cursor.getCount()>0) {
                            cursor.close();
            return true;
        }
                    cursor.close();
    }
    return false;
}

基于此布尔值,您可以执行操作。

我认为您不知道需要什么)您应该将数据库和表名传递给isTableExists以检查它们是否存在。