在Sqlite android中选择语句问题

时间:2011-06-16 06:26:50

标签: android sqlite

  

你好专家,
  我无法执行select语句你可以帮助我这里是我附加的代码

try
    {
    Insall_app_db i1 = new Insall_app_db();
    int s = i1.GetUserData(this, tableName);
    System.out.println("i1.app_id:::: "+s);
    Toast.makeText(getBaseContext(),"app_id"+s, Toast.LENGTH_LONG).show();
    }catch (Exception e) {
        e.printStackTrace();
    }


public int GetUserData(Context context,String tablename)
{
            c = sampleDB.rawQuery("SELECT * FROM " +
                tablename +
                "", null);

            if(c!=null)
            {
                if(c.moveToFirst())
                {
                    do
                    {
                        app_id=c.getInt(c.getColumnIndex("app_id"));
                        /*app_name=c.getString(c.getColumnIndex("app_name"));
                        pname=c.getString(c.getColumnIndex("pname"));
                        version_name=c.getString(c.getColumnIndex("version_name"));
                        versionCode=c.getString(c.getColumnIndex("versionCode"));
                        icon=c.getString(c.getColumnIndex("icon"));
                        app_name=c.getString(c.getColumnIndex("date"));*/




                    }while(c.moveToNext());



                    }   

                }       

            return app_id;
}

1 个答案:

答案 0 :(得分:3)

您好请尝试以下代码。


public int GetUserData(Context context,String tablename)
{
    int app_id=0;
    try {
        sampleDB = myDbHelper.openDataBase();
    } catch (SQLException sqle) {
        throw sqle;
    }
    sampleDB = myDbHelper.getWritableDatabase();
        c = sampleDB.rawQuery("SELECT * FROM " +
            tablename, null);

        if(c!=null)
        {
            if(c.moveToFirst())
            {
                do
                {
                    app_id=c.getInt(c.getColumnIndex("app_id"));
                }while(c.moveToNext());



                }   

            }       

        return app_id;
}