0

我完全被困住了。整天用头撞砖墙!

我有一堂课:

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 是正确的,但是那里有什么论据?

还是我完全走错了路?

感谢您的任何建议

4

1 回答 1

2

从你的帖子我明白你想要check whether Table exists or not

此代码将为您提供帮助。

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;
}

基于这个布尔值你可以做你的动作。

于 2013-02-08T08:05:51.173 回答