从我的数据库中获取记录的数量并将其存储在整数中

时间:2022-04-21 01:14:15

I want to get the count of the records from the database onto my app in an integer then display it... how do i do that???

我想从数据库中将记录的计数记录到我的应用程序中,然后显示它...我该怎么做?

i am not able to do so

我无法这样做

here is the database code

这是数据库代码

public int counttable()
{
    int count=0;
    openOrCreateDatabase();

    count=db.execSQL("select count(*) from "+TableNameis+";");

    return count;

}

i know that the datatypes dont match...can anyone please suggest how do i do that?? how do i store the count value in an integer variable.

我知道数据类型不匹配...任何人都可以建议我该怎么做?如何将计数值存储在整数变量中。

Dashboard.java

Dashboard.java

public class Dashboard extends Activity {

EditText edt_pending, edt_completed,edt_synched;

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.dashboard);

    edt_pending=(EditText)findViewById(R.id.editpending);
    edt_completed=(EditText)findViewById(R.id.editcompleted);
    edt_synched=(EditText)findViewById(R.id.editsynched);

    WayDataBase way=new WayDataBase(Dashboard.this);
    int count=way.counttable();

    edt_completed.setText(count);


}

}

}

logcat

logcat的

04-08 07:15:02.216: E/AndroidRuntime(16026): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.lthomepage/com.android.lthomepage.Dashboard}: android.content.res.Resources$NotFoundException: String resource ID #0x1 04-08 07:15:02.216: E/AndroidRuntime(16026): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 04-08 07:15:02.216: E/AndroidRuntime(16026): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 04-08 07:15:02.216: E/AndroidRuntime(16026): at android.app.ActivityThread.access$600(ActivityThread.java:141) 04-08 07:15:02.216: E/AndroidRuntime(16026): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 04-08 07:15:02.216: E/AndroidRuntime(16026): at android.os.Handler.dispatchMessage(Handler.java:99) 04-08 07:15:02.216: E/AndroidRuntime(16026): at android.os.Looper.loop(Looper.java:137) 04-08 07:15:02.216: E/AndroidRuntime(16026): at android.app.ActivityThread.main(ActivityThread.java:5039) 04-08 07:15:02.216: E/AndroidRuntime(16026): at java.lang.reflect.Method.invokeNative(Native Method) 04-08 07:15:02.216: E/AndroidRuntime(16026): at java.lang.reflect.Method.invoke(Method.java:511) 04-08 07:15:02.216: E/AndroidRuntime(16026): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 04-08 07:15:02.216: E/AndroidRuntime(16026): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 04-08 07:15:02.216: E/AndroidRuntime(16026): at dalvik.system.NativeStart.main(Native Method) 04-08 07:15:02.216: E/AndroidRuntime(16026): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1 04-08 07:15:02.216: E/AndroidRuntime(16026): at android.content.res.Resources.getText(Resources.java:230) 04-08 07:15:02.216: E/AndroidRuntime(16026): at android.widget.TextView.setText(TextView.java:3640) 04-08 07:15:02.216: E/AndroidRuntime(16026): at com.android.lthomepage.Dashboard.onCreate(Dashboard.java:27) 04-08 07:15:02.216: E/AndroidRuntime(16026): at android.app.Activity.performCreate(Activity.java:5104) 04-08 07:15:02.216: E/AndroidRuntime(16026): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 04-08 07:15:02.216: E/AndroidRuntime(16026): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

04-08 07:15:02.216:E / AndroidRuntime(16026):java.lang.RuntimeException:无法启动活动ComponentInfo {com.android.lthomepage / com.android.lthomepage.Dashboard}:android.content.res.Resources $ NotFoundException:字符串资源ID#0x1 04-08 07:15:02.216:E / AndroidRuntime(16026):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)04-08 07:15:02.216:E / AndroidRuntime(16026):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)04-08 07:15:02.216:E / AndroidRuntime(16026):在android.app.ActivityThread.access $ 600(ActivityThread.java: 141)04-08 07:15:02.216:E / AndroidRuntime(16026):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)04-08 07:15:02.216:E / AndroidRuntime(16026) :在android.os.Handler.dispatchMessage(Handler.java:99)04-08 07:15:02.216:E / AndroidRuntime(16026):在android.os.Looper.loop(Looper.java:137)04-08 07:15:02.216:E / AndroidRuntime(16026):在android.app.ActivityThread.main(ActivityThread.j ava:5039)04-08 07:15:02.216:E / AndroidRuntime(16026):at java.lang.reflect.Method.invokeNative(Native Method)04-08 07:15:02.216:E / AndroidRuntime(16026): at java.lang.reflect.Method.invoke(Method.java:511)04-08 07:15:02.216:E / AndroidRuntime(16026):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit。 java:793)04-08 07:15:02.216:E / AndroidRuntime(16026):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)04-08 07:15:02.216:E / AndroidRuntime(16026):at dalvik.system.NativeStart.main(Native Method)04-08 07:15:02.216:E / AndroidRuntime(16026):引起:android.content.res.Resources $ NotFoundException:字符串资源ID# 0x1 04-08 07:15:02.216:E / AndroidRuntime(16026):在android.content.res.Resources.getText(Resources.java:230)04-08 07:15:02.216:E / AndroidRuntime(16026):在android.widget.TextView.setText(TextView.java:3640)04-08 07:15:02.216:E / AndroidRuntime(16026):at com.android.lthomepage.Dashboard.onCreate(Dashboard.java:27)04- 08 07:15:02.21 6:E / AndroidRuntime(16026):在android.app.Activity.performCreate(Activity.java:5104)04-08 07:15:02.216:E / AndroidRuntime(16026):at android.app.Instrumentation.callActivityOnCreate(Instrumentation) .java:1080)04-08 07:15:02.216:E / AndroidRuntime(16026):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

3 个解决方案

#1


1  

Try this code:

试试这段代码:

public int counttable()
{
int count=0;
openOrCreateDatabase();
Cursor c = db.rawQuery("select * from your_table_name",null);
count=c.getCount();
return count;
}

Also,

也,

edt_completed.setText(String.valueOf(count));

Using SELECT query is enough, then count the size of Cursor..

使用SELECT查询就足够了,然后计算Cursor的大小..

#2


1  

You can use the Cursor#getCount() to get the count of the no. of records.

您可以使用Cursor#getCount()来获取no的计数。的记录。

public int counttable()
{
    int count=0;
    openOrCreateDatabase();

    // count=db.execSQL("select * from "+TableNameis+";"); - This statement is invalid

    // Use this instead
    count=db.rawQuery(selectionQuery, null).getCount(); 

    return count;

}

#3


0  

  1. Create One Class for DatabaseCreate Like this..

    为DatabaseCreate创建一个类这样..

    public class WayDataBase extends SQLiteOpenHelper {

    公共类WayDataBase扩展SQLiteOpenHelper {

    private static String DATABASE_NAME = "Database Name";
    private SQLiteDatabase myDataBase;
    private Context myContext;
    private String DATABASE_PATH = "/data/data/"Package Name"/databases/";
    
    
    // Constructor  
    
    public WayDataBase (Context context) 
    {
    
        super(context, DATABASE_NAME, null, 1);
        this.myContext = context;
    }
    
    // Create DataBase 
    
    public void createDatabase() throws IOException
    {
    
        boolean dbExist = checkDataBase();
    
        if(dbExist)
        {
        }
        else
        {
    
            this.getReadableDatabase();
    
            try 
            {
                this.close();   
                copyDataBase();
            } 
            catch (IOException e) 
            {
                throw new Error("Error copying database");
            }
        }
    
    }
    
    // check Database Existing or not
    
    private boolean checkDataBase() 
    {
        SQLiteDatabase checkDB = null;
    
        try
        {
            String myPath = DATABASE_PATH + DATABASE_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
    
        }
        catch(SQLiteException e)
        {
        }
    
        if(checkDB != null)
        {
    
            checkDB.close();
    
        }
    
        return checkDB != null ? true : false;
    
    }
    
    // Copy DataBase from assets to SD card 
    
    private void copyDataBase() throws IOException
    {
    
        String outFileName = DATABASE_PATH + DATABASE_NAME;
    
    
        OutputStream myOutput = new FileOutputStream(outFileName);
        InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
    
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) 
        {
            myOutput.write(buffer, 0, length);
        }
    
        myInput.close();
        myOutput.flush();
        myOutput.close();
    }
    
    // Open DataBase
    
    public void openDatabase() throws SQLException
    {
    
        String myPath = DATABASE_PATH + DATABASE_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
    
    }
    public int counttable()
    {
        int count=0;
        openDatabase();
        Cursor c = myDataBase.rawQuery("SELECT * FROM table_name",null);
        return count;
    }
    

    }

    }

Now in your Activity

现在在您的活动中

public class Dashboard extends Activity {

公共类仪表板扩展活动{

private WayDataBase way;

私有WayDataBase方式;

EditText edt_pending, edt_completed,edt_synched;

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.dashboard);

    edt_pending=(EditText)findViewById(R.id.editpending);
    edt_completed=(EditText)findViewById(R.id.editcompleted);
    edt_synched=(EditText)findViewById(R.id.editsynched);


    way= new WayDataBase(this);
    try 
    {
        way.createDatabase();

    } catch (IOException ioe) 
    {
        throw new Error("Unable to create database");
    }


    int count=way.counttable();

    edt_completed.setText(count+"");


}

}

}

#1


1  

Try this code:

试试这段代码:

public int counttable()
{
int count=0;
openOrCreateDatabase();
Cursor c = db.rawQuery("select * from your_table_name",null);
count=c.getCount();
return count;
}

Also,

也,

edt_completed.setText(String.valueOf(count));

Using SELECT query is enough, then count the size of Cursor..

使用SELECT查询就足够了,然后计算Cursor的大小..

#2


1  

You can use the Cursor#getCount() to get the count of the no. of records.

您可以使用Cursor#getCount()来获取no的计数。的记录。

public int counttable()
{
    int count=0;
    openOrCreateDatabase();

    // count=db.execSQL("select * from "+TableNameis+";"); - This statement is invalid

    // Use this instead
    count=db.rawQuery(selectionQuery, null).getCount(); 

    return count;

}

#3


0  

  1. Create One Class for DatabaseCreate Like this..

    为DatabaseCreate创建一个类这样..

    public class WayDataBase extends SQLiteOpenHelper {

    公共类WayDataBase扩展SQLiteOpenHelper {

    private static String DATABASE_NAME = "Database Name";
    private SQLiteDatabase myDataBase;
    private Context myContext;
    private String DATABASE_PATH = "/data/data/"Package Name"/databases/";
    
    
    // Constructor  
    
    public WayDataBase (Context context) 
    {
    
        super(context, DATABASE_NAME, null, 1);
        this.myContext = context;
    }
    
    // Create DataBase 
    
    public void createDatabase() throws IOException
    {
    
        boolean dbExist = checkDataBase();
    
        if(dbExist)
        {
        }
        else
        {
    
            this.getReadableDatabase();
    
            try 
            {
                this.close();   
                copyDataBase();
            } 
            catch (IOException e) 
            {
                throw new Error("Error copying database");
            }
        }
    
    }
    
    // check Database Existing or not
    
    private boolean checkDataBase() 
    {
        SQLiteDatabase checkDB = null;
    
        try
        {
            String myPath = DATABASE_PATH + DATABASE_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
    
        }
        catch(SQLiteException e)
        {
        }
    
        if(checkDB != null)
        {
    
            checkDB.close();
    
        }
    
        return checkDB != null ? true : false;
    
    }
    
    // Copy DataBase from assets to SD card 
    
    private void copyDataBase() throws IOException
    {
    
        String outFileName = DATABASE_PATH + DATABASE_NAME;
    
    
        OutputStream myOutput = new FileOutputStream(outFileName);
        InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
    
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) 
        {
            myOutput.write(buffer, 0, length);
        }
    
        myInput.close();
        myOutput.flush();
        myOutput.close();
    }
    
    // Open DataBase
    
    public void openDatabase() throws SQLException
    {
    
        String myPath = DATABASE_PATH + DATABASE_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
    
    }
    public int counttable()
    {
        int count=0;
        openDatabase();
        Cursor c = myDataBase.rawQuery("SELECT * FROM table_name",null);
        return count;
    }
    

    }

    }

Now in your Activity

现在在您的活动中

public class Dashboard extends Activity {

公共类仪表板扩展活动{

private WayDataBase way;

私有WayDataBase方式;

EditText edt_pending, edt_completed,edt_synched;

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.dashboard);

    edt_pending=(EditText)findViewById(R.id.editpending);
    edt_completed=(EditText)findViewById(R.id.editcompleted);
    edt_synched=(EditText)findViewById(R.id.editsynched);


    way= new WayDataBase(this);
    try 
    {
        way.createDatabase();

    } catch (IOException ioe) 
    {
        throw new Error("Unable to create database");
    }


    int count=way.counttable();

    edt_completed.setText(count+"");


}

}

}